+ Reply to Thread
Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Submariner
  • 1 Post By vv.bbcc19

Thread: Not getting an xmlhttp response from my initial request.

  1. #1
    f1.p183 is offline x10Hosting Member f1.p183 is an unknown quantity at this point
    Join Date
    May 2011
    Posts
    15

    Not getting an xmlhttp response from my initial request.

    Hello world.

    I'll try to be specific. I'm using a form and some javascript in my HTML page to make an xmlhttp request to a php file.

    The form has three inputs which are sent via an xmlhttp request.
    I intend to use the collected data in the php file to query a mysql database and send back a response that I can then use in the javascript.

    In the response onready function of the initial javascript I assign the response to a textbox so I can then do what I like with it.

    At the moment, I'm not worrying about the mysql syntax becuase I'm still having problems returning a response to the textbox. I've tried for example just using a single line php file with an echo 'here is a test response'; but I don't receive anything back.

    I've also tried using both POST and GET and if I run the php file directly and tag on some input to the query string then any echos in the file are output directly so it seems the problem is either the xmlhttp request or the on ready function in the javascript.

    For now - I've got both my html file and php file in the same directory.

    I tried all day yesterday to find out what the problem is without success. I'm hoping somebody here will identify something stupid I am doing.

    Here is the html file..

    <html>
    <head>
    <script>

    // ---------------------------------------------------------------------------------------------------------------------------------------

    function get2server() { var input1=document.testform.input1.value;
    var input2=document.testform.input2.value;
    var input3=document.testform.input3.value'


    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }


    else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }


    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // output server resonse ie: In this case confirmation variable has been set at the server
    document.testform.output1.value=xmlhttp.responseTe xt;
    }
    }




    xmlhttp.open("GET","serverget.php?input1="+input1+ "&input2="+input2+"&input3="+input3,true);
    xmlhttp.send();

    } // End function Serverget

    </script>

    </head>
    <body>
    <BR>
    <H2>Server Test</H4>
    <BR>
    <H4> Specify Command, Variable and Value</H3>
    <form NAME="testform">

    <INPUT TYPE="TEXT" NAME="input1" SIZE=16><BR>
    <INPUT TYPE="TEXT" NAME="input2" SIZE=16><BR>
    <INPUT TYPE="TEXT" NAME="input3" SIZE=16><BR>
    <BR>
    <INPUT TYPE="button" VALUE="- Send to server -" onClick="get2server()"><BR>
    <INPUT TYPE="TEXT" NAME="output1" SIZE=30>


    </body>
    </html>

    and here is the php file...

    <?php

    define('DB_NAME','xxxxxxxxx'); // I have got my actual details in the real one
    define('DB_USER','xxxxxxxxx');
    define('DB_PASSWORD','xxxxxxxxx');
    define('DB_HOST','localhost');


    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); // connect to Host computer

    if (!$link) { echo 'Error connecting'; }

    $db_selected = mysql_select_db(DB_NAME, $link); // Select database

    if (!$db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); }

    echo 'Connected successfully';

    $value1= $_GET['input1']; // command
    $value2= $_GET['input2']; // name // Retrieve input passed in from form
    $value3= $_GET['input3']; // value

    echo 'You got this far !';

    echo $value1;
    echo $value2;
    echo $value3;

    $result = mysql_query("SELECT * FROM value WHERE name ='$_POST[input2]'");

    if (!mysql_query($sql)) { die('Error: ' . mysql_error()); }


    $row = mysql_fetch_row($result);

    echo $row[0];
    echo $row[1];
    echo $row[2];



    $response=$row[0]+$row[2]+$row[3];
    mysql_close();
    ?>

    I really hope someone can point me in the right direction here becuase I'm lost to what I can try next. Thanks in advance for any replies.

    ---------- Post added at 04:21 PM ---------- Previous post was at 09:54 AM ----------

    Hi again,

    Well I feel a bit schizophrenic answering my own post but I thought I better record the fact that the problem is solved just in case anyone decides to go through the rather jumbled script I left here.

    I found a really minimalist example of the request and response and it works ! so I'm using it as a template and gradually building it up bit by bit until I've transfered everything that I want into it. At the moment I can't see what is causing the problem but I expect I will find out when I move over into to the new script whatever is causing it.

    Thanks for looking
    Last edited by f1.p183; 05-25-2011 at 05:01 AM.

  2. #2
    Submariner is offline x10Hosting Member Submariner is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    TN, USA
    Posts
    44

    Re: Not getting an xmlhttp response from my initial request.

    To me it looks like you never declared the variable 'xmlhttp'. Since you are using it in both functions, get2Server() and assigning the second function to the onreadystatechange event I would define it outside of the functions as a global variable.

    Have fun.
    f1.p183 likes this.

  3. #3
    vv.bbcc19's Avatar
    vv.bbcc19 is offline Community Advocate vv.bbcc19 is just really nice
    Join Date
    Jun 2010
    Location
    India
    Posts
    1,505

    Re: Not getting an xmlhttp response from my initial request.

    I agree too..
    An example of a xmlhttp request is put here
    PHP Code:
    <html>
    <
    body>
      <
    form name="form"
       
    action="file.php"
       
    enctype="multipart/form-data"
       
    method="post">
       <
    input type="file" id="file"/>
       <
    input type="submit" value="Upload File">
      </
    form>
      <
    script>
       
    xmlHttp=new XMLHttpRequest()
       
    xmlHttp.open("POST","file.php",true)
       
    xmlHttp.send(????)
       
    xmlHttp.setRequestHeader("label""value")
       
    xmlHttp.onreadystatechange=function{if (xmlHttp.readyState==|| xmlHttp.readyState=="complete"document.getElementById(n).innerHTML=xmlHttp.responseText }
      
    </script>
    <body>
    </html> 
    The code requires the correction.
    Quote Originally Posted by Submariner View Post
    To me it looks like you never declared the variable 'xmlhttp'. Since you are using it in both functions, get2Server() and assigning the second function to the onreadystatechange event I would define it outside of the functions as a global variable.

    Have fun.
    f1.p183 likes this.
    BCV | Community Support Representative
    █ x10Hosting - Giving Away Hosting Since 2004
    Premium Hosting | VPS Services

  4. #4
    f1.p183 is offline x10Hosting Member f1.p183 is an unknown quantity at this point
    Join Date
    May 2011
    Posts
    15

    Re: Not getting an xmlhttp response from my initial request.

    Quote Originally Posted by Submariner View Post
    To me it looks like you never declared the variable 'xmlhttp'. Since you are using it in both functions, get2Server() and assigning the second function to the onreadystatechange event I would define it outside of the functions as a global variable.

    Have fun.
    Well - my code is such a muddle I'll admit I wasn't looking in that particular area for the error. It would appear that you are correct. Thanks very much.
    As I said - it's working now but it's even better that I know what was causing it becuase I mistakingly thought I had tracked it down to using a wrong dom.

    Thanks again.

    ---------- Post added at 09:30 AM ---------- Previous post was at 09:18 AM ----------

    Quote Originally Posted by vv.bbcc19 View Post
    I agree too..
    An example of a xmlhttp request is put here
    PHP Code:
    <html>
    <
    body>
      <
    form name="form"
       
    action="file.php"
       
    enctype="multipart/form-data"
       
    method="post">
       <
    input type="file" id="file"/>
       <
    input type="submit" value="Upload File">
      </
    form>
      <
    script>
       
    xmlHttp=new XMLHttpRequest()
       
    xmlHttp.open("POST","file.php",true)
       
    xmlHttp.send(????)
       
    xmlHttp.setRequestHeader("label""value")
       
    xmlHttp.onreadystatechange=function{if (xmlHttp.readyState==|| xmlHttp.readyState=="complete"document.getElementById(n).innerHTML=xmlHttp.responseText }
      
    </script>
    <body>
    </html> 
    The code requires the correction.

    Thanks for this example vv.bbcc19. It's helpful. One day my code will be this concise although I deliberately used a separate function to submit the info becuase eventually I need other functions to handle the input/output independently of the user.
    Last edited by f1.p183; 05-26-2011 at 04:44 AM.

  5. #5
    vv.bbcc19's Avatar
    vv.bbcc19 is offline Community Advocate vv.bbcc19 is just really nice
    Join Date
    Jun 2010
    Location
    India
    Posts
    1,505

    Re: Not getting an xmlhttp response from my initial request.

    Ahah..
    input output independently?
    That requires 2 different modules right!!

  6. #6
    f1.p183 is offline x10Hosting Member f1.p183 is an unknown quantity at this point
    Join Date
    May 2011
    Posts
    15

    Re: Not getting an xmlhttp response from my initial request.

    Quote Originally Posted by vv.bbcc19 View Post
    Ahah..
    input output independently?
    That requires 2 different modules right!!
    Thanks for your new reply vv.bbcc19

    I'm not sure from your exclamation marks whether you're saying. Yes I require two modules or No ... I don't really need them

    Just to clarify, I'm sending off the data from a function becuase I want the script to eventually update the database while the user is doing other things without making a direct request. I want to do some background requests to the database so I can set some variables etc for multiuser use. I think it's possible to have the form and use the document.forms.submit method which I guess would allow me to do it without calling another function but I'm also more familiar with just calling a function.

    I guess it's a bit misleading as it is becuase I've got an output box as well but I thought what i would do is use an output box while debugging and then either hide or show the output boxes according to whether i am testing or going live.

    Thanks again for your help. I've been stuck now for hours on an empty query message - the one about not being a valid resource and I think I will have to post that here shortly !
    Last edited by f1.p183; 05-27-2011 at 04:38 AM.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers