Javascript confirm dialog & PHP problem


I'm trying to use a javascript alert dialog box. If the user press OK I want javascript to run a little PHP code.

I think this isn't working cuz' the page never updates...

I dont really know how to solve this. I'm not that good at Javascript.

Is there anyone who can guide me a little bit?

Thanks for advance!
<form name=imgDelete><input type=button value="Click here" onClick="if(confirm('Are you sure?')) <?
			if($go == 1){
			        echo "Yes!";
				echo "No!";

Open in new window

Who is Participating?
Ray PaseurConnect With a Mentor Commented:
This is not tested, but you can see the logic flow.  

HTH, ~Ray
<?php // drop_photo.php
if (!empty($_POST))
    if ('yes' == strtolower($_POST["confirm"]))
        /* PERFORM DELETION USING $_POST["id"] */
        echo "IT IS GONE";
<h2>CONFIRM DELETE: <?php echo $_GET["id"]; ?></h2>
<form method="POST">
<input type="hidden" name="id" value="<?php echo $_GET["id"]; ?>" />
TYPE 'yes' HERE:
<input type="text"   name="confirm" />
<input type="submit" name="_submit"		value="DELETE" />

Open in new window

Ray PaseurCommented:
I am not even sure where to start on this question, so I'll just tell you the right way to do this sort of thing.

If you want to run a PHP script after a client clicks a link, you can use AJAX.

If you are asking a client to confirm a change to the underlying data, such as deleting a record from the data base, you cannot use JavaScript.  Instead you must use the POST method.  That is a rule of the WWW, and violating it will cause you trouble.
Ray PaseurCommented:
This link should be helpful if the Javascript Confirm is what you're looking for.  If not, please post back here and perhaps we can help.  Best regards, ~Ray
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Matthew KellyConnect With a Mentor Commented:
Use "return confirm" such as below:
<a href="img_delete.php?id=1" onclick="return confirm('Are you sure?')">Click Here</a>.

Open in new window

Ray PaseurCommented:
@matthewstevenkelly: You're right about the JS part of this question.  The risk with this approach arises when a search engine spiders the web site and prefetches the pages that are linked.  It goes to this URI:


If the "img_delete.php" script does not force a separate round-trip through POST method confirmation, and just decided to delete the image with id=1, the images will disappear one day and nobody will have a clue why it happened.  

That's why I think our OP may have a deeper issue than just confirming something in JS - the question evinces a possible misunderstanding of the dangers that arise when you use GET strings to change the data model.

Best to all, ~Ray
GodDoesntExistAuthor Commented:
Thank you guys for great support!

You're right about my need. This confirmation will remove a picture from the server and some information from the database. But the client needs to be logged in to do this. Is there any risk with the search engine then?

Thanks again for really fast and great response!
Ray PaseurCommented:
There is much less risk if a login is required, but the "best practices" approach would be something like this:

1. take the photo key from the URL GET string.
2. put up a POST form saying, "are you sure"
3. when the POST form comes back with "yes" delete the photo.

That's it, ~Ray
GodDoesntExistAuthor Commented:

Does the user gets like a popup confirmation or will this be a new .php file?
GodDoesntExistAuthor Commented:
Thank you so much!
Ray PaseurCommented:
thanks for the points - it's a great question, ~Ray
GodDoesntExistAuthor Commented:
Thank you Ray!

This was really helpful! =)

You saved me a lot of time!

I'm really grateful!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.