最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript alert before php code - Stack Overflow

programmeradmin2浏览0评论

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 ;
}

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 ;
}
Share Improve this question edited May 10, 2013 at 10:07 Ankur Saxena asked May 10, 2013 at 9:55 Ankur SaxenaAnkur Saxena 6393 gold badges13 silver badges26 bronze badges 7
  • Just add a prompt in the confirmSubmit ? – JNDPNT Commented May 10, 2013 at 9:58
  • I suggest you read about AJAX, mysqli and PDO. – elclanrs Commented May 10, 2013 at 9:58
  • In confirmSubmit() function ? – Rikesh Commented May 10, 2013 at 9:58
  • in confirmSubmit() if confirmSubmit() returns true then link href is followed if not it stays on the same page. – Robert Commented May 10, 2013 at 9:58
  • it should work as is: if the user doesn't confirm confirmSubmit(), the link is not followed and the delete.php link is not followed – Jan Turoň Commented May 10, 2013 at 9:59
 |  Show 2 more ments

5 Answers 5

Reset to default 3

This line will show confirm dialogbox before redirect.

<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>
发布评论

评论列表(0)

  1. 暂无评论