2012-03-11 98 views
1

我想知道是否有人可以帮助我。从单选按钮创建动态列表选择

我把下面的代码放在一起,试图让用户通过单选按钮选择检索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'

我一直在这方面工作了一段时间,到目前为止,我没有成功解决它。我只是想知道是否有人可以看看这个请让我知道我要去哪里错了。

非常感谢

回答

0

它改变这种

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

嗨,非常感谢您花时间回复。我现在已经摆脱了错误信息,但不幸的是,我无法用检索到的记录创建表格。你能告诉我你有什么想法,我可能会出错吗?非常感谢 – IRHM 2012-03-11 14:36:28

+0

以及您在创建表格时出现了哪些错误?什么都没显示或? – riso 2012-03-11 14:46:39

+0

嗨,不幸的是我没有收到任何错误。我附上了一个链接到我的网页,以突出这一点。 http://www.mapmyfinds.co.uk/development/ajaxfunction.php亲切的问候 – IRHM 2012-03-11 14:54:57

1

你的报价在这条线上搞砸了。对外部字符串使用双引号,所以变量会被内插,并且像\n这样的转义字符不会被解释为文字。

echo“\ n” 。 “\ n” “\ n” 。“{$ dateoftrip} \ n” 。“\ n”; // ------ ^^^^缺少分号....

您也可以更干净了HEREDOC statement,做到这一点,避免报价的问题,linebreks完全:

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

在OP代码中,该行还有一个缺失的分号。 – jprofitt 2012-03-11 14:13:22

+0

@jprofitt感谢上面的评论。这就是为什么我主张HEREDOC而不是多线串接... – 2012-03-11 14:17:16

+0

嗨,HEREDOC对我来说肯定是新的,我不得不看看这些。并适时地注意关于SQL注入。亲切的问候 – IRHM 2012-03-11 14:39:07

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

嗨,非常感谢您采取时间来回复我的帖子。如上所述,幸好我现在已经能够摆脱错误信息,但很遗憾,我无法用检索结果创建表格。亲切的问候 – IRHM 2012-03-11 14:37:44