开发者

Not able to send data from JS to PHP

开发者 https://www.devze.com 2023-02-19 14:31 出处:网络
I have a html page from which I want to send information to a PHP script. look at the html page here: <html>开发者_运维百科

I have a html page from which I want to send information to a PHP script. look at the html page here:

<html>开发者_运维百科
<head>

<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
//frm.submit();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","infor.php",true);
xmlhttp.send();
}
</script>

<body bgcolor="white">
<table style="position: absolute; left: 00px; top: 00px;">
<tr><td width="1000" height="400" style="background:#666666">
<table style="position: absolute; left: 40px; top: 40px;">
<tr><td width="900" height="300" style="background:#C8C8C8">
<form name="frm" action="info.php" method="post">




State Name<select name="t1" id="list1" size="1" single onchange="loadXMLDoc();">
<option value="ANDHRA PRADESH">ANDHRA PRADESH</option>
<option value="ASSAM">ASSAM</option>
<option value="BIHAR">BIHAR</option>
<option value="CHATTISGARH">CHATTISGARH</option>
<option value="DELHI">DELHI</option>
<option value="GOA">GOA</option>
<option value="GUJRAT">GUJRAT</option>
<option value="HARYANA">HARYANA</option>
<option value="KERLA">KERLA</option>
<option value="MANIPUR">MANIPUR</option>
<option value="ORISSA">ORISSA</option>
<option value="PANJAB">PANJAB</option>
<option value="TAMILNADU">TAMILNADU</option>
<option value="WEST BANGAL">WEST BANGAL</option>
</select>
<div id="myDiv">
</div>
</form>
<input type="button" value="Close this window" onclick="self.close()">
</td></tr>
</table>
</body>
</head>
</html>

and here is my PHP script:

<?php
print $_GET["t1"];
?>

But don't know why it's not working; I am not able to fetch data from the database. Looks like $_GET["t1"] isn't working for me. Can anyone help me to fix this error?


The problem seems pretty simple; you're not sending anything to the form.

It seems like you're assuming that the AJAX code sends the form, but it doesn't. You'll have to manually throw in the variables into the URL.

I've never done AJAX the way you're doing them (more on that below), but instead of...

xmlhttp.open("GET", "infor.php", true);

...you'll have to pass the variables to 'infor.php'. See this for a demonstration of what the URL string should look like:

xmlhttp.open("GET", "infor.php?var1=value1&var2=value2", true);

...although in your particular case, since your variables are not hard-coded but from the form, you can reference the form variable like this:

xmlhttp.open("GET", "infor.php?t1=" + document.getElementById("list1").value, true);

...and finally, you should URL-encode the data like so:

xmlhttp.open("GET", "infor.php?t1=" + encodeURIComponent(document.getElementById("list1").value), true);

That should work, but it's neither pretty nor handy. You really should be using some library to do AJAX instead of doing all the nitty-gritty work yourself. jQuery and Prototype both offer excellent implementations of AJAX functionality.

A tutorial of either will show you how to do what you're trying to do so that you don't have to spoon-feed the URL variables to the request yourself.

You can find jQuery here: http://jquery.com/

And you can find Prototype here: http://www.prototypejs.org/

It doesn't really matter which one you use for AJAX, they work very similarly in that regard.


You should create parameters Like

var parms = "t1="+document.getElementById('list1').value;

and on xmlhttp.open("GET","infor.php?"+parms,true);

this will send your select box t1 to that php page.

on the php page you can retrive the parameter by printing $_GET['t1']

0

精彩评论

暂无评论...
验证码 换一张
取 消