Javascript confirm dialog & PHP problem

Hi!

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!";
			}
			else{
				echo "No!";
		}
?>;"></form>

Open in new window

LVL 3
GodDoesntExistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
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

http://www.tizag.com/javascriptT/javascriptconfirm.php
0
Matthew KellyCommented:
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

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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:

img_delete.php?id=1

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
0
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!
0
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
0
GodDoesntExistAuthor Commented:
Alright!

Does the user gets like a popup confirmation or will this be a new .php file?
0
Ray PaseurCommented:
This is not tested, but you can see the logic flow.  

HTH, ~Ray
<?php // drop_photo.php
 
// IF THERE IS SOMETHING IN THE POST ARRAY
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" />
</form>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GodDoesntExistAuthor Commented:
Thank you so much!
0
Ray PaseurCommented:
thanks for the points - it's a great question, ~Ray
0
GodDoesntExistAuthor Commented:
Thank you Ray!

This was really helpful! =)

You saved me a lot of time!

I'm really grateful!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.