Wednesday, 28 December 2016

JAVASCRIPT CODE FOR CONFIRMATION BEFORE DELTE A RECORD

00:55

NOW  LEARN HOW TO USE  YES NO CONFIRMATION CODE OF JAVASCRIPT  WITH PHP SCRIPT TO ASK FOR CONFIRMATION BEFORE DELTE A RECORD :
========================================================================
(1)create   a  database   ajax  and then create a table users  :

SQL>

CREATE TABLE  users (
user_id INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
first_name VARCHAR( 25 ) NOT NULL ,
last_name VARCHAR( 25 ) NOT NULL ,
user_city VARCHAR( 45 ) NOT NULL

);

insert at least  5 records into your table and then 

(2)then write code  for  index.php file:

<html>
<head>
<script type="text/javascript">
function delete_id(id)
{
     if(confirm('Sure To Remove This Record ?'))
     {
        window.location.href='index.php?delete_id='+id;
     }
}
</script>
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("ajax");
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
?>
<table align="center">
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>City</th>
    <th>Delete</th>
    </tr>
    <?php
 if(mysql_num_rows($result_set)>0)
 {
  while($row=mysql_fetch_row($result_set))
  {
   ?>
            <tr>
            <td><?php echo $row[1]; ?></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            <td align="center"><a href="javascript:delete_id(<?php echo $row[0]; ?>)"><img src="b_drop.png" alt="Delete" /></a></td>
            </tr>
            <?php
  }
 }
 else
 {
  ?>
        <tr>
        <th colspan="4">There's No data found !!!</th>
        </tr>
        <?php
 }
 ?>
 </table>
 <?php

 if(isset($_GET['delete_id']))
{
     $sql_query="DELETE FROM users WHERE user_id=".$_GET['delete_id'];
     mysql_query($sql_query);
     header("Location: index.php");
}


?>


now get understanding about above code:


following is the anchor link that call the below javascript function.

<href="javascript:delete_id(<?php echo $row[0]; ?>)">Delete</a>

put the following javascript just above your closing </head> tag
<script type="text/javascript">
function delete_id(id)
{
     if(confirm('Sure To Remove This Record ?'))
     {
        window.location.href='index.php?delete_id='+id;
     }
}
</script>


window.location.href='index.php?delete_id='+id; This javascript object creates the QueryString like index.php?delete_id=any_value
and the rest of work is php if the querystring is set the selected row will be deleted using following php condition.

if(isset($_GET['delete_id']))
{
     $sql_query="DELETE FROM users WHERE user_id=".$_GET['delete_id'];
     mysql_query($sql_query);
     header("Location: index.php");
}


NOW  LEARN HOW TO USE  YES NO CONFIRMATION CODE OF JAVASCRIPT  WITH PHP SCRIPT TO ASK FOR CONFIRMATION BEFORE DELTE A RECORD :
========================================================================
(1)create   a  database   ajax  and then create a table users  :

SQL>

CREATE TABLE  users (
user_id INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
first_name VARCHAR( 25 ) NOT NULL ,
last_name VARCHAR( 25 ) NOT NULL ,
user_city VARCHAR( 45 ) NOT NULL

);

<html>
<head>
<script type="text/javascript">
function delete_id(id)
{
     if(confirm('Sure To Remove This Record ?'))
     {
        window.location.href='index.php?delete_id='+id;
     }
}
</script>
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("ajax");
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
?>
<table align="center">
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>City</th>
    <th>Delete</th>
    </tr>
    <?php
 if(mysql_num_rows($result_set)>0)
 {
  while($row=mysql_fetch_array($result_set))
  {
   ?>
            <tr>
            <td><?php echo $row['user_id']; ?></td>
            <td><?php echo $row['first_name']; ?></td>
            <td><?php echo $row['last_name']; ?></td>
            <td align="center"><a href="javascript:delete_id(<?php echo $row['user_id']; ?>)"><img src="b_drop.png" alt="Delete" /></a></td>
            </tr>
            <?php
  }
 }
 else
 {
  ?>
        <tr>
        <th colspan="4">There's No data found !!!</th>
        </tr>
        <?php
 }
 ?>
 </table>
 <?php

 if(isset($_GET['delete_id']))
{
     $sql_query="DELETE FROM users WHERE user_id=".$_GET['delete_id'];
     mysql_query($sql_query);
     header("Location: index.php");
}


?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. Your concepts were easy to understand that I wondered why I never looked at it before. This information is definitely useful for everyone..
    you also visit here php training in delhi

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top