简体   繁体   中英

javascript alert before php code

page:content display.php

<td width="100"><a onclick="return confirmSubmit()" href="delete.php?id=<?php echo $row['id']; ?>&table=labour_details&return=content_display.php"><img src="../images/delete.png" border="0" alt="delete"></a></td>

page:delete.php

if(isset($_GET['id']) && isset($_GET['table']))
    {
        $id=$_GET['id'];
        $tablename=$_GET['table'];
        $return=$_GET['return'];




        if($tablename=="labour_details")
        {
                    $sql=mysql_query("SELECT * FROM `labour_details` WHERE `id`='$id'");
                    $row_1=mysql_fetch_array($sql);
                    $labour_name= $row_1['labour_name'];

                    if($labour_name!="")
                    {
                        $str=mysql_query("DELETE FROM `labour_details` WHERE `id`='$id'");
                        if($str)
                        {
                            header("location:$return?msg=Record Deleted Successfully!&id=$id");
                        }else{
                            echo "Problem in deleting :";
                        }
                    }

        }

}

my problem is where to add alert so that before deleting record it show javascript massage if allow then it will delete record.

function confirmSubmit()
{
    var agree=confirm("Are you sure you wish to Delete this Entry?");
    if (agree)
        return true ;
    else
        return false ;
}

此行将显示重定向之前的确认对话框。

<a onclick="return confirm('Are You sure?')">...</a>

First you need to create the confirmSubmit() function

<script>
function confirmSubmit()
{
    if (confirm('Are you sure you want to delete this?'))
    {
        return true;
    }
    return false;
}
</script>

Then you attach that to your A tag, as you have done..

<a href="..." onclick="return confirmSubmit();">blah</a>

You're probably looking for confirm .

Then, the body of the confirmSubmit function would look something like:

function confirmSubmit() {
    return confirm ("Are you sure you want to do this?");    
}

This works due to how event handlers work in javascript. Confirm returns true when the user clicks OK and false when they click Cancel.

By returning a true / false value from an event handler function you tell the window whether to keep processing that event. Returning false (which happens when the user clicks Cancel) tells the browser to stop processing the event and not follow the link.

<td width="100"><a onclick="confirmSubmit(id)"><img src="../images/delete.png" border="0" alt="delete"></a></td>




function confirmSubmit(delete_id)
{
var r=confirm("Are you sure you want to delete?");

if(r==true)
{
         window.location.href = "delete.php?id="+delete_id"; ?>&table=labour_details&return=content_display.php"
    }
}
<a onclick="return confirm('Are you sure?')" href="delete.php">delete</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM