943,545 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 206065
  • PHP RSS
Nov 7th, 2005
0

how can i make php alert pop ups just like in javascript.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iketunde is offline Offline
13 posts
since Oct 2005
Nov 7th, 2005
0

Re: how can i make php alert pop ups just like in javascript.

In short you can't. But it's very easy to get the same confirmation that you would with a alert box.

1. Pass ID/Email of record along with the delete command, easiest to use a URL

e.g.[PHP] <a href="confirm_delete.php?id=<idofemailrecord>">Delete Me</a>[/PHP]

2. On your confirm_delete.php page create two URLs yes or no

e.g.

[PHP]Are you sure you want to delete:
<br>
<a href="delete.php?confirm=yes&id=".$_GET[id].">Yes</a>
<a href="delete.php?confirm=no&id=".$_GET[id].">No</a>[/PHP]

Then on the delete.php page you have some like the following:
[PHP]
if ( $_GET[confirm] == "yes" )
{
mysql_query( "DELETE FROM table WHERE id= '".$_GET[id]."'" );
} else if ( $_GET[confirm] == "no" )
{
header( "location: http://www.yourhomepage.com");
}[/PHP]

Of course this is extremely basic. You would really want to check the variables $_GET[confirm] and $_GET[id] to make sure a hacker wasn't trying to do a sql injection attack on your form. But that's for another discussion. Hope that helps.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msteudel is offline Offline
9 posts
since Jul 2005
Nov 9th, 2005
0

Re: how can i make php alert pop ups just like in javascript.

I'm sure I've seen a tutorial at hotscripts for that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiwimedia is offline Offline
19 posts
since Jul 2005
Oct 23rd, 2006
0

Re: how can i make php alert pop ups just like in javascript.

Maybe you can use:
<?php echo '<script language="javascript">confirm("Do you want this?")</script>;'; ?>
//Khanobly

Click to Expand / Collapse  Quote originally posted by iketunde ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khanobly is offline Offline
1 posts
since Oct 2006
Oct 24th, 2006
2

Re: how can i make php alert pop ups just like in javascript.

Click to Expand / Collapse  Quote originally posted by iketunde ...
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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jan 6th, 2009
0

Re: how can i make php alert pop ups just like in javascript.

Thank digital-ether for this part: onclick="return (confirm('do you really want to delete the stuff?'))

Tried it with a submit button in a form and it even works here. Perfect. Just what I was looking for.
Reputation Points: 34
Solved Threads: 52
Posting Whiz
colweb is offline Offline
316 posts
since Nov 2007
Sep 15th, 2009
0

Re: how can i make php alert pop ups just like in javascript.

I'm sorry! please ignore me... A lot of times I asked questions too fast before figuring it out... Great help! thanks for the codes!
Last edited by AdventDeo; Sep 15th, 2009 at 11:50 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
AdventDeo is offline Offline
38 posts
since Aug 2009
Sep 3rd, 2010
0
Re: how can i make php alert pop ups just like in javascript.
Good solution digital-ether >>>>> This is the way to show confirmation message before delete ..... I use this in web development .
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mohammed yaghi is offline Offline
9 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How do I get the top-of-the-tree directory?
Next Thread in PHP Forum Timeline: Can includes have a variable?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC