PHP コードに ajax を適用しようとしています。しかし、ボタンをクリックしても反応がありません。これは、宣言した ajax 関数が呼び出されていないことを意味しonclick
ます。
<?php
$s_name= $_POST["submit"];
mysql_connect("localhost","root","");//database connection
mysql_select_db("itcompanylist");
$query = "SELECT s_id FROM states WHERE `state_name` = '$s_name'";
$result1 = mysql_query($query);
$row = mysql_fetch_array($result1);
$result2 = mysql_query("SELECT city_name FROM `city` WHERE s_id ='".$row['s_id']."'");
$i = 0;
echo "<form method='post' name='myForm'><table border='1' ><tr>";
while ($row = mysql_fetch_row($result2)){
echo '<td><input type="submit" name="submit" onclick="ajaxFunction()" value="'.$row['0'].'"></td>';
if ($i++ == 2)
{
echo "</tr><tr>";
$i=0;
}
}
echo "</tr></table></form>";
echo "<div id='ajaxDiv'>Your result will display here</div>";
?>
ajax コード:
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.value = ajaxRequest.responseText;
}
}
var s1 = document.getElementById('submit').value;
var queryString = "?submit=" + s1 ;
ajaxRequest.open("GET", "" +
queryString, true);
ajaxRequest.send(null);
}
//-->
</script>