IRHM
IRHM

Reputation: 1326

Create Dynamic List From Radio Button Select

I wonder whether someone can help me please.

I've put together the code below to try and allow a user to retrieve mySQL records via a radio button select, in turn populating a table on my html form.

Form

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

The problem I'm having is that I receive the following error and I'm not sure why: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/2/d333603417/htdocs/development/ajaxfunction.php on line 69 with line 69 being this line of my html form code: '<td><input type='radio' name='name' dateoftrip value='{$userid}' onchange="ajaxFunction(this.value)"/></td>\n'

I've been working on this for a while and so far I've unsuccessful at resolving it. I just wondered whether someone could take a look at this please and let me know where I'm going wrong.

Many thanks

Upvotes: 1

Views: 1080

Answers (3)

Michael Berkowski
Michael Berkowski

Reputation: 270677

Your quoting is messed up on that line. Use double-quotes for the outer string, so variables get interpolated, and escape characters like \n are not interpreted as literals.

    echo"\n"     . "\n" ."{$dateoftrip}\n" ."\n"; //------^^^^ Missing semicolon....

You can also accomplish this more cleanly with a HEREDOC statement, and avoid the quoting issue and linebreks entirely:

  echo <<<HTML
    <tr>
    <td><input type='radio' name='name' dateoftrip value='{$userid}' onchange='ajaxFunction(this.value)'/></td>
    <td><small>{$dateoftrip}</small><td>
    </tr>
HTML;

A further note: your script is vulnerable to SQL injection. Escape all your input values with 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...

Upvotes: 1

William Dixon
William Dixon

Reputation: 101

You need to escape those quotes and add a semi-colon at the end of the echo statement.

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

Upvotes: 1

riso
riso

Reputation: 232

change it to this

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

Upvotes: 0

Related Questions