【问题标题】:Create Dynamic List From Radio Button Select从单选按钮选择创建动态列表
【发布时间】:2012-03-11 22:15:07
【问题描述】:

我想知道是否有人可以帮助我。

我将下面的代码放在一起尝试并允许用户通过单选按钮选择检索 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 表单代码的这一行: '&lt;td&gt;&lt;input type='radio' name='name' dateoftrip value='{$userid}' onchange="ajaxFunction(this.value)"/&gt;&lt;/td&gt;\n'

我已经为此工作了一段时间,但到目前为止我还没有成功解决它。我只是想知道是否有人可以看看这个,让我知道我哪里出错了。

非常感谢

【问题讨论】:

    标签: php html


    【解决方案1】:

    您需要转义这些引号并在 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";
    

    【讨论】:

    • 您好,非常感谢您抽出宝贵时间回复我的帖子。如上所述,幸运的是,我现在已经能够摆脱错误消息,但不幸的是,我无法使用检索到的结果创建表。亲切的问候
    【解决方案2】:

    您在该行的引用搞砸了。对外部字符串使用双引号,这样变量就会被插值,并且像 \n 这样的转义字符不会被解释为文字。

       回声“\n” . "\n" ."{$dateoftrip}\n" ."\n"; //------^^^^ 缺少分号....

    您还可以使用HEREDOC statement, 更干净地完成此操作,并完全避免引用问题和换行:

      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...
    

    【讨论】:

    • OP 的代码中该行还缺少一个分号。
    • @jprofitt 谢谢上面提到的。这就是为什么我提倡使用 HEREDOC 而不是多行连接......
    • 嗨,HEREDOCs 对我来说当然是新的,我得看看这些。并适当地说明了 SQL 注入。亲切的问候
    【解决方案3】:

    改成这个

     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";
    

    【讨论】:

    • 您好,非常感谢您抽出宝贵时间回复。我现在已经摆脱了错误消息,但不幸的是我无法使用检索到的记录创建表。你能告诉我请你有什么想法我可能会出错吗?非常感谢
    • 您在创建表格时遇到了什么错误?什么都不显示或?
    • 嗨,很遗憾我没有收到任何错误。我附上了一个指向我的页面的链接,以更好地突出这一点。 mapmyfinds.co.uk/development/ajaxfunction.php亲切的问候
    • 你有两次,删除第一次
    • 嗨,非常感谢。我已经进行了更改,但不幸的是我仍然无法从“getrecords.php”脚本中获取记录。亲切的问候
    猜你喜欢
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多