Results 1 to 2 of 2

Thread: deleting using javascript

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

    Default deleting using javascript

    hi all,
    i want javascript function to delete the selected records for the given php program below....
    also i am not getting field values in correct order one field is more than its field...
    kindly tell me how to do it......
    Code:
    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="";     // Mysql password
    $db_name="test"; // Database name
    $tbl_name="emp"; // Table name
    
    // Connect to server and select databse.
    mysql_connect($host, $username, $password)or die("cannot connect");
    mysql_select_db($db_name)or die("cannot select DB");
    $sql="SELECT * FROM emp";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td align="center" bgcolor="white"><strong>EmpNo</strong></td>
    <td align="center" bgcolor="white"><strong>EmpName</strong></td>
    <td align="center" bgcolor="white"><strong>Desig</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result, MYSQL_ASSOC))
    {
    
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['empno']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['empname']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['desig']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?php
    // Check if delete button active, start this
    if($_POST['delete'])
    {
    //print_r($_POST);
    //exit;
    for($i=0;$i<count($_POST['checkbox']);$i++)
    {
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $result = mysql_query($sql);
    }
    
    // if successful redirect to delete_multiple5.php
    if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";
    }
    }
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    javacript works on the user's computer; it cannot perform any actions whatsoever on your server.

    you might use javascript to submit a html form to specify which records you want to delete, but the deletion must be handled by a server-side script (e.g., in your php script).

    ------------------------------

    As a side note, to clarify any possible confusion:

    Many people just learning php think that it is simply "another scripting language, like html or javascript." This is not true (in fact, html is not a scripting language at all, but that's another conversation).

    once you are using php, then your entire page becomes a php script, even if only one line is php.

    php runs on the webserver, before the user's browser is sent any code. the basic purpose of php is to write html (and javascript, etc.). after php execution ends, the output (including any plain html you had on the page) is sent to the browser. from this point, everything works just like a normal, static webpage does.

    this is the reason that, among other things, javascript cannot interact with php: there would need to be another http request to send new info to the php scripts on your server.

    ------------------------------------------
    One thing that really helps to reinforce this concept (and make your php better in the process) is to separate your php from your html markup. For example, the part of your php script that processes the form should be way at the top (or even on a separate page), completely removed from any of the markup. for example:

    PHP Code:
    <?php
    if($_POST['someform']){
      
    /* process form */
      /* save the result to a variable to print later */
      
    $formResults '<p>whatever the result is</p>';
    }
    /* do other php stuff, for example, you might pull some database records */
    $result mysql_query("SELECT `col1`,`col2` FROM `whatever`");
    $DBrecords '<h1>Databse Records:</h1>';
    if(
    $result){
        while(
    $r mysql_fetch_assoc($result)){ 
            
    $DBrecords .= "<p>col1: {$r['col1']}, col2: {$r['col2']} </p>";  
        }
    }else{ 
    $Dbrecords .= '<p>No database records.</p>'; }

    /* then, after your done with what you need php to do, build the page: */
    ?>
    <!DOCTYPE html>
    <head>
       <meta charset="UTF-8">
       <title>my page</title>
    </head>
    <body>
       <!--etc., etc., etc...
       ...say you wanted the db results to show up here-->

       <?php print $DBrecords?>

       <!-- and so on...
       ... the results from your form submission, or the form itself, etc.
       -->
       <h2>This is what happened when you submitted the form</h2>
       <?php print $formResults?>

    </body>
    </html><!--and you're done-->

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
  •