Jump to content

Archived

This topic is now archived and is closed to further replies.

ayok

[SOLVED] Confirm Box?

Recommended Posts

Hi there,

 

I have a question regarding the confirm box of javascript. I'd like to pop up a confirm box when the admin wants to delete an item.

So when the person click on "ok", the item will be delete, but if "cancel" is clicked, then it would stay on this page. But I cannot get it works.

 

I've tried this script:

<script type="text/javascript">
function disp_confirm(){
    var name=confirm("You're going to delete this item, would you like to process it?")
    if (name==true){
        window.location("delete.php?id=$data[id_cat];")
    }
}
</script>

 

And this code on the delete:

<a href='' onclick='disp_confirm()'>delete</a>

 

Could anybody help me with this?

 

Thank you,

ayok

Share this post


Link to post
Share on other sites

If you want to cancel the default event you need to return either true or false from the event handler.  I forget which value (true, false) is used to cancel the default action associated with the onclick event.  Just try one and then the other.

Share this post


Link to post
Share on other sites

Hi roopurt,

 

Sorry I don't understand your answer. Could you give me example?

 

ayok

Share this post


Link to post
Share on other sites

<script type="text/javascript">

function disp_confirm(){

    var name=confirm("You're going to delete this item, would you like to process it?")

    if (name==true){

        window.location("delete.php?id=$data[id_cat];")

    }else{

      return true; // change true to false if it doesn't work

    }

}

</script>

Share this post


Link to post
Share on other sites

<script type="text/javascript">

function disp_confirm(){

    var name=confirm("You're going to delete this item, would you like to process it?")

    if (name==true){

        window.location("delete.php?id=$data[id_cat];")

    }else{

      return true; // change true to false if it doesn't work

    }

}

</script>

 

Hi thanks roopurt,

 

It is return to the page when i click "cancel", but it doesn't delete when i click on "ok".

Is there something wrong with the window location?

 

ayok

Share this post


Link to post
Share on other sites

I think it's supposed to be:

 

window.location.href = <rvalue>;

Share this post


Link to post
Share on other sites

I think it's supposed to be:

 

window.location.href = <rvalue>;

 

Thanks roopurt,

It seems the javascript is working, but i can't get the url "delete.php?id=$data[id_cat];"

Is there any help to correct this?

 

ayok

Share this post


Link to post
Share on other sites

What does the URL look like after you click the delete link and then click OK to confirm?

Share this post


Link to post
Share on other sites

It goes to index.php.. its an admin folder.. www.myweb.com/admin/

 

ayok

Share this post


Link to post
Share on other sites

It would be hard to say what's going on without seeing some more of your code.

 

Or you can try changing the line:

window.location("delete.php?id=$data[id_cat];")

to

window.location.href="http://www.yahoo.com/"

 

Just to make sure it's redirecting correctly when you press OK.

Share this post


Link to post
Share on other sites

No.. it doesn't go to yahoo.com. So it goes to empty url.

Share this post


Link to post
Share on other sites

Hi,

So far my javascript is:

function disp_confirm(){
    var name=confirm("Do you really want to delete this?")
    if (name==true){
        window.location("delete.php?id=$data[id_cat]")
    }
    else{
	return true;
	}
}

 

and the link where to click:

<a href='' onclick='disp_confirm()'>delete</a>

 

The confirm box is popuped but if i click on ok or cancel it will open the index page. Anybody can help me?

 

Thank you,

ayok

Share this post


Link to post
Share on other sites

You don't call window.location as if it's a function.  Do you see any difference at all between the following?

 

// wrong!
window.location("delete.php?id=$data[id_cat];")

 

// correct!
window.location.href = "http://www.yahoo.com";

Share this post


Link to post
Share on other sites

Hi roopurt,

I have try the code

window.location.href = "http://www.yahoo.com";

. But it doesn't go to yahoo.com. It stays in the same page.

 

thank you,

ayok

Share this post


Link to post
Share on other sites

Try this:

 

function disp_confirm(){
    var msg = "Do you really want to delete this?";
    if (confirm(msg)){
        window.location("delete.php?id=$data[id_cat]");
        return false;
    }
    else{
	return true;
	}
}

Share this post


Link to post
Share on other sites

Hi roopurt,

 

It still doesn't work. I try in IE it gives an error: Object doesn't support this action.

 

thanks,

ayok

Share this post


Link to post
Share on other sites
<html>
<head>
<script type="text/javascript">
function disp_confirm(){
    var msg = "Do you really want to delete this?";
    if (confirm(msg)){
        window.location.href = "http://www.yahoo.com";
    }
    return false;
}
</script>
</head>
<body>
  <a href='' onclick='return disp_confirm();'>delete</a>
</body>
</html>

Share this post


Link to post
Share on other sites

Hi roopurt,

 

After I tried with html page, it works. So I think the problem is that I'm doing it in php script.

Is there anyway to use it in php script. Because the item that will be deleted is in php.

 

thank you,

ayok

Share this post


Link to post
Share on other sites
<?php
$show = mysql_query("SELECT service.*,category.name FROM service LEFT JOIN category on service.category=category.id_cat WHERE category='$id'") or die(mysql_error());
echo "<table border=0><tr bgcolor='#D0DCE0'><th>Service</th><th>Description</th><th>Duration (minutes)</th><th>Prices</th></tr>";


    while (($data = mysql_fetch_array($tampil)) && (mysql_num_rows($tampil) > 0))
    {
        echo "<tr>";
        echo "<td bgcolor='#D47FFF'>$data[name_serv]</td>";
        echo "<td bgcolor='#FF55FF'>$data[desc_serv]</td>";
        echo "<td bgcolor='#D47FFF'>$data[minutes]</td>";
        echo "<td bgcolor='#FF55FF'>$data[price]</td>";
        echo "<td><a href='' onclick='disp_confirm()'>delete</a><br><a href=edit_service.php?id=$data[id_serv]>edit</a></td></tr>";
    }
    echo "</table>";
}?>

<script type="text/javascript">
function disp_confirm(){
    var msg = "Do you really want to delete this?";
    if (confirm(msg)){
        window.location("delete.php?id=$datakategori[id_cat]");
    }
    return false;
}
</script>

Share this post


Link to post
Share on other sites

Sorry, I missed some lines above it

 

<?php
$show1 = mysql_query("SELECT * FROM category WHERE id_cat='$id'") or die(mysql_error());
$datakategori = mysql_fetch_array($show1);
echo "<H3>$datakategori[name]</H3><br><img align='left' src='../pics/$datakategori[pic]'<br><H5>$datakategori[description](<a href=edit_cat.php?id=$datakategori[id_cat]>edit kategori</a> | <a href=add_cat.php>add kategori</a>)<H5>";

$show = mysql_query("SELECT service.*,category.name FROM service LEFT JOIN category on service.category=category.id_cat WHERE category='$id'") or die(mysql_error());
echo "<table border=0><tr bgcolor='#D0DCE0'><th>Service</th><th>Description</th><th>Duration (minutes)</th><th>Prices</th></tr>";


    while (($data = mysql_fetch_array($tampil)) && (mysql_num_rows($tampil) > 0))
    {
        echo "<tr>";
        echo "<td bgcolor='#D47FFF'>$data[name_serv]</td>";
        echo "<td bgcolor='#FF55FF'>$data[desc_serv]</td>";
        echo "<td bgcolor='#D47FFF'>$data[minutes]</td>";
        echo "<td bgcolor='#FF55FF'>$data[price]</td>";
        echo "<td><a href='' onclick='disp_confirm()'>delete</a><br><a href=edit_service.php?id=$data[id_serv]>edit</a></td></tr>";
    }
    echo "</table>";
}?>

<script type="text/javascript">
function disp_confirm(){
    var msg = "Do you really want to delete this?";
    if (confirm(msg)){
        window.location("delete.php?id=$datakategori[id_cat]");
    }
    return false;
}
</script>

Share this post


Link to post
Share on other sites

The problem is that you're trying to set the $_GET parameter in the Javascript but you are not in PHP mode (because you've stepped out of PHP with the ?>).

 

<script type="text/javascript">
function disp_confirm(){
    var msg = "Do you really want to delete this?";
    if (confirm(msg)){
        window.location.href = "delete.php?id=<?php echo $datakategori['id_cat'];?>";
    }
    return false;
}
</script>

Share this post


Link to post
Share on other sites

Hi roopurt,

 

its the same. it doesnt go to delete.php.

The problem is, it results the same if I set the window location to yahoo.com, except I did it in html page.

 

thank you,

ayok

Share this post


Link to post
Share on other sites

Perhaps change the target location to an absolute URL?  I'm not sure why it wouldn't work at this point.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.