PDA

View Full Version : deleting using js



ravi951
08-18-2011, 05:32 AM
here i have written to delete using javascript......
it is "del.php"


<script>
function fundel(sno)
{
rv=confirm("u want to delete");
if(rv==true)
{
location="delete.php?seno="+sno;
}
}
</script>
<table border="2">
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$data=mysql_query("select * from emp");
while($rec=mysql_fetch_row($data))
{
echo "<tr><td>$rec[0]<td>$rec[1]<td>$rec[2]</tr>
<input type="button" value="delete" onclick='fundel($rec[2])'>";
}
?>

also is the "delete.php"


<?php
$qs=$_REQUEST['seno'];
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("delete from emp where sno=$qs");
header("location:getrec.php");
?>

it is displaying the error as Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\del.php on line 19

tell me what went wrong....
whether it will delete the values.....

djr33
08-18-2011, 09:54 AM
When posting an error, please tell us which line is the corresponding line in the code. (But it also helps to see a few lines around it.)

Your last echo statement in the first code is the problem. You need to escape any quotes that are the same as the quotes used to create the string:

echo "<tr><td>$rec[0]<td>$rec[1]<td>$rec[2]</tr>
<input type=\"button\" value=\"delete\" onclick='fundel($rec[2])'>";

Note that this would all be easier if organized better with single quotes:

echo '<tr><td>$rec[0]<td>'.$rec[1].'<td>'.$rec[2].'</tr>
<input type="button" value="delete" onclick="fundel('.$rec[2].')">';
In this case, the single quotes around the onclick attribute were made into double quotes, but it's always best to be consistent.
The variables also must exist outside of the quotes in single quotes, but this is also, in fact, more clear, at least once you get used to it. Placing variables directly in strings is lazy and can cause problems sometimes.

For somewhat complicated reasons, it's best to use single quotes in PHP (they parse faster) and double quotes in HTML (they're standard). This isn't a requirement, but it will, if used consistently, make things easier.