我想知道是否有人能帮我。
我把下面的代码放在一起,试图让用户通过单选按钮选择来检索mySQL记录,然后在我的html表单上填充一个表
表单
<html>
<head>
<script type="text/javascript">
function ajaxFunction(name)
{
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{// code for IE6, IE5
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("my_div").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getrecords.php?dateoftrip="+name,true);
xmlhttp.send();
}
function getquerystring() {
var form = document.forms['frm1'];
var word = form.word.value;
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring
return qstr;
}
</script>
<style type="text/css">
<!--
.style1 {
font-family: Calibri;
font-size: 14px;
}
-->
</style>
</head>
<body>
<form action="getrecords.php" method="get" name="frm1">
<table width="148" border="0">
<tr>
<td width="152"><p class="style1">Select a date from below</p>
<div align="center">
<?php
include("db.php");
$query="select userid, dateoftrip from finds group by dateoftrip";
$result=mysql_query($query);
while (list($userid, $dateoftrip) =
mysql_fetch_row($result))
{
echo"<tr>'n"
.
'<td><input type='radio' name='name' dateoftrip value='{$userid}' onchange="ajaxFunction(this.value)"/></td>'n'
.'<td><small>{$dateoftrip}</small><td>'n'
.'</tr>'n'
}
echo"<tr><td colspan='"3'">";
echo"<br>";
echo"</td></tr>";
echo'</table>';
?>
</div></td>
</tr>
</table>
</form>
<div id="my_div"></div>
</body>
</html>
getrecords.php
<?php
include("db.php");
$dateoftrip= $_GET['dateoftrip'];
$findname= $_GET['findname'];
$finddescription= $_GET['finddescription'];
$query="select * from finds where dateoftrip='$dateoftrip'";
echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>Find Name : </td>";
echo "<td>".$rows['findname']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Find Description : </td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
?>
我遇到的问题是,我收到了以下错误,我不知道为什么:Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/2/d333603417/htdocs/development/ajaxfunction.php on line 69
,第69行是我的html表单代码的这一行:'<td><input type='radio' name='name' dateoftrip value='{$userid}' onchange="ajaxFunction(this.value)"/></td>'n'
我已经做了一段时间了,到目前为止我还没有成功解决这个问题。我只是想知道是否有人能看看这个问题,并告诉我哪里出了问题。
非常感谢
您需要转义这些引号,并在echo语句的末尾添加一个分号。
echo "<tr>'n <td><input type='radio' name='name' dateoftrip value='$userid' onchange='"ajaxFunction(this.value)'"/></td>'n <td><small>$dateoftrip</small><td>'n</tr>'n";
你的报价在那一行搞砸了。外部字符串使用双引号,这样就可以对变量进行插值,并且像'n
这样的转义字符不会被解释为文字。
echo"''n"。"''n"."{$dateoftrip}''n"."''n";//------^^^^缺少分号。。。。
您还可以使用HEREDOC
语句更干净地完成此操作,并完全避免引用问题和换行:
echo <<<HTML
<tr>
<td><input type='radio' name='name' dateoftrip value='{$userid}' onchange='ajaxFunction(this.value)'/></td>
<td><small>{$dateoftrip}</small><td>
</tr>
HTML;
进一步注意:您的脚本容易受到SQL注入的攻击。使用mysql_real_escape_string()
:转义所有输入值
$dateoftrip = mysql_real_escape_string($_GET['dateoftrip']);
$findname = mysql_real_escape_string($_GET['findname']);
$finddescription = mysql_real_escape_string($_GET['finddescription']);
// etc...
将其更改为此
echo"<tr>'n"
.
"<td><input type='radio' name='name' dateoftrip value='{$userid}' onchange="."ajaxFunction(this.value)"."/></td>'n"
."<td><small>{$dateoftrip}</small><td>'n"
."</tr>'n";