Advanced Search

Results 1 to 2 of 2

Thread: deleting using js

  1. #1
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default deleting using js

    here i have written to delete using javascript......
    it is "del.php"
    Code:
    <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"
    Code:
    <?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.....

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    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:
    Code:
    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:
    Code:
    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •