I’m building a restaurant menu application in PHP & JavaScript
And I can’t seem to get the buttons to append the text area when the ordering is being placed…(This is just test code at present) Here’s my code:
$test = mysql_query("SELECT * FROM main_stock");
while($row = mysql_fetch_array($test)){
echo "<div id='".$row["RCode"]."'><a href='javascript:addTo(".$row["Name"].",".$row["RCode"].")'>".$row["Name"]."</a></div>";}
And my javascript function is as follows:
function addTo(name, Rcode) {
document.getElementById('order').value += name;
}
And HTML Form is as follows:
<form id="OrderForm" name="OrderForm" method="post" action="">
<p>
<label>
<textarea name="order" id="order" cols="35" rows="20" readonly="readonly">test</textarea>
</label>
</p><table>
<tr><td>Subtotal:</td><td><input type="text" id="subtotal" readonly="readonly" value="2.00" /></td></tr>
<tr><td>Tax:</td><td><input type="text" id="tax" readonly="readonly" value="2.00"/></td></tr>
<tr><td>Total:</td><td><input type="text" id="total" readonly="readonly" value="4.00"/></td></tr>
<tr><td></td><td><input type="submit" id="submit" value="Send Order To Kitchen"/></td></tr>
</table>
Looks like the
echomay be resulting in invalid JavaScript syntax. E.g.:Rather than:
You’ll need to output additional quotes for JavaScript to use. And, since you’re already using
"for the PHP string and'for HTML attributes, this will require escaping — or encoding:Another option may be to use
json_encodesince JSON is based from JavaScript syntax: