开发者

Problem related to communication between two PHP pages

Here is my first page where I use a combobox to submit the query:

<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&开发者_如何学运维lt;/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 goes my second PHP page where I use $_POST to get the variable but it's not working:

<?php
print "<br><br>D.Name<select name='t2' size='1' single>";
$conn=mysql_connect("localhost","root","");
mysql_select_db("mysql",$conn);
$str="select * from ".$_POST["t1"];
$rs=mysql_query($str);
while($row=mysql_fetch_array($rs))
{
    print "<option value=ASSAM>".$row['dname']."</option>";
}


mysql_close($conn);
print "</select>";
?>


Before you do anything, first do the following:

  1. Get an established Javascript library (don't bother writing the AJAX calls yourself when there are so many browser specifics to check for that might break your call). Easy to get something like jQuery and just go (use their documentation to put your call into play easily).
  2. Have proper PHP escape / checking / validation, you are currently doing a SQL call with the BARE POST variable. HUGE no-no.


The only thing you have to understand:

There are no 2 php pages.

There are one HTML/JS page and one php page.

It's very important to distinguish HTML/JS code running in the browser from PHP code running on the server.

as for your particular problem, it's very easy.

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

it clearly says that GET method is used. Why do you expect any data in the $_POST array then? ;)


You do this

$str="select * from ".$_POST["t1"];

Unless you actually have tables corresponding to values like

<option value="ANDHRA PRADESH">ANDHRA PRADESH</option>
<option value="ASSAM">ASSAM</option>
<option value="BIHAR">BIHAR</option>

You probably wanted to do something like

$str = "SELECT * FROM someTable WHERE someColumn = ".mysql_real_escape_string($_POST['t1']);

Additionally, after

$rs=mysql_query($str);

Add

if(!$rs) {
  trigger_error (mysql_error().'SQL: '.$str);
  exit();
}

You will find it easier to debug if you check if what you're doing is working. If you still get nothing returned on form submission (which is what I assume is happening), check your Javascript.


he is not sending any parameters in the GET request as well guys

you need to form the parameters like

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

remember to wrap url encode for this.

on the php page you should use url_decode($_GET['t1'])

I have answered t in your other post in javascript tag,

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜