Not getting an xmlhttp response from my initial request.

f1.p183

New Member
Messages
15
Reaction score
0
Points
0
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.responseText;
}
}




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:

Submariner

New Member
Messages
44
Reaction score
1
Points
0
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.
 

vv.bbcc19

Community Advocate
Community Support
Messages
1,524
Reaction score
92
Points
48
I agree too..
An example of a xmlhttp request is put here
PHP:
<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==4 || xmlHttp.readyState=="complete") document.getElementById(n).innerHTML=xmlHttp.responseText }
  </script>
<body>
</html>
The code requires the correction.
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

New Member
Messages
15
Reaction score
0
Points
0
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 ----------

I agree too..
An example of a xmlhttp request is put here
PHP:
<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==4 || 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:

vv.bbcc19

Community Advocate
Community Support
Messages
1,524
Reaction score
92
Points
48
Ahah..
input output independently?
That requires 2 different modules right!!
 

f1.p183

New Member
Messages
15
Reaction score
0
Points
0
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:
Top