I have a php page with two hyperlinks. I one to send email to certain addresses and the other to also send emails to another address and also delete their records from the database.
I don't want a case where the user accidentally clicks on the link takes the wrong action.
i thougth about javascript confirm function. so i tried combining the javascript confirm function and php but i don't seem to get the two working.
i need the results from the confirm function which is either true or false so i can take appropriate action.
Is there an alternative?does php provide similar alert functions? i really need help.
Javascript is client side (executes on the browser) and PHP is server side (executes on the server).
The confirm box is a function on the browser, so you cannot create a browser confirm box with php unless you use PHP to write out some javascript that will be executed on the browser to display your cofnirm box. (see khanobly's reply)
The only way you can communicate with the php on the server side via javascript is to send the server a new HTTP request (a new page). (exception being xmlHTTPRequest).
In your case, the confirmation box should work just like a normal click on a link (you dont need the javascript to communicate with the php).
eg:
<a href="task.php?option=delete">Delete</a>
Lets say the above will invoke the php script task.php and let it know you want to delete something from the database.
We can use javascript to make sure the user doesn't delete by accident.
<a href="task.php?option=delete" onclick="return (confirm('do you really want to delete the stuff?'));>Delete</a>
When the user clicks the link, the onclick event handler in javascript is triggered. The onclick handler overides the loading of the url in the href attribute but if the onclick function returns true (or null) the browser will then load the url in the href attribute. If the onclick function returns false the url will not load.
confirm, returns true or false depending on what the user chooses (yes|no). So we just return the return from the confirm function to the onclick event trigger and thus the link will be followed or not depending on the users choice.