confirmation popup problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
umbra
Forum Newbie
Posts: 21
Joined: Tue Sep 13, 2005 4:20 am

confirmation popup problem

Post by umbra »

I try to put a confirmation popup before I delete a record from my database.
That's why I wrote a function like this:

Code: Select all

<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Are you sure you want to delete this record?")
	if (answer){
		window.location = "delete.php?table_name=$entry_type&entry_no=$read[entry_no]";
	}
}
//-->
</script>
I try to call this function with this link:

Code: Select all

echo "<td align='center'><a href='JavaScript:confirmation()'> <font color='#FF0000'> delete </font></a></td></tr>";
However, I can't pass table_name and entry_no variables to delete.php. Then I tried to call the function with

Code: Select all

'JavaScript:confirmation($entry_type, $read[entry_no])'
with this function: function confirmation($entry_type, $read[entry_no]) { ..... }

but this time the popup window is not created. Does anyone has a suggestion about what can I do?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

First of all javascript can't use php variables, here's an example of passing a variable from php to javascript, you should be able to figure the rest out:

Code: Select all

$test = 'Hello world';
echo ('<a href="javascript:void" onClcik="validate(\'.$test.\')">click here to test</a>');

and the javascript function definition

Code: Select all

function validate( test ) {
     window.location='http://example.com/?test=' + test;
}
umbra
Forum Newbie
Posts: 21
Joined: Tue Sep 13, 2005 4:20 am

Post by umbra »

my javascript function definition:

Code: Select all

function confirmation(entry_type, entry_no) { 
var answer = confirm("Are you sure you want to delete this record?")

	if (answer){
		window.location = "delete.php?table_name=" + entry_type + "&entry_no=" + entry_no;
	}
}
and my call to the function from php is:

Code: Select all

echo "<td align='center'><a href='javascript:void(0)' onClick='confirmation(\'.$entry_type.\', \'.$entry_no.\')'> <font color='#FF0000'> delete </font></a></td></tr>";
It still doesn't work, the popup window is not created. Where am I making a mistake?

jshpro2 wrote:First of all javascript can't use php variables, here's an example of passing a variable from php to javascript, you should be able to figure the rest out:

Code: Select all

$test = 'Hello world';
echo ('<a href="javascript:void" onClcik="validate(\'.$test.\')">click here to test</a>');

and the javascript function definition

Code: Select all

function validate( test ) {
     window.location='http://example.com/?test=' + test;
}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Your mistake(s) are in

Code: Select all

echo "<td align='center'><a href='javascript:void(0)' onClick='confirmation(\'.$entry_type.\', \'.$entry_no.\')'> <font color='#FF0000'> delete </font></a></td></tr>";
First mistake:

onClick='confirmation(\'.$entry_type.\', \'.$entry_no.\')'

the reason I escaped those single quotes in my example was because my echo() was using single quotes so the variables wouldn't be expanded, just for informational purposes so when your variables were NULL in your script you would at least see javascript was doing it's job.


Now this 'mistake' is questionable:

javascript&#058void(0)

&#058 should be a colon, I don't know if the forum put that there for you (it replaced my colon) or if you actually copy pasted the 058 from my code.


Change your code to something like

Code: Select all

echo "<td align='center'><a href='javascript:void()' onClick=\"confirmation('$entry_type', '$entry_no')\"> <font color='#FF0000'> delete </font></a></td></tr>";
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

don't confuse "calling" your function with php and "calling" your function with JS.

you're actually calling the js function on the client side. You're only displaying the function and its "call" with php.

you're escaping your quotes and not concatting the rest of your vars to the string that you're echoing...use double quotes instead.

it should look something like this:

Code: Select all

echo "<td align=\"center\"><a href=\"javascript:void(0)\" onClick=\"confirmation('".$entry_type."', '".$entry_no."')'\"> <font color=\"#FF0000\"> delete </font></a></td></tr>";
umbra
Forum Newbie
Posts: 21
Joined: Tue Sep 13, 2005 4:20 am

Post by umbra »

jshpro2 wrote:Your mistake(s) are in

Code: Select all

echo "<td align='center'><a href='javascript:void(0)' onClick='confirmation(\'.$entry_type.\', \'.$entry_no.\')'> <font color='#FF0000'> delete </font></a></td></tr>";
First mistake:

onClick='confirmation(\'.$entry_type.\', \'.$entry_no.\')'

the reason I escaped those single quotes in my example was because my echo() was using single quotes so the variables wouldn't be expanded, just for informational purposes so when your variables were NULL in your script you would at least see javascript was doing it's job.


Now this 'mistake' is questionable:

javascript&#058void(0)

&#058 should be a colon, I don't know if the forum put that there for you (it replaced my colon) or if you actually copy pasted the 058 from my code.


Change your code to something like

Code: Select all

echo "<td align='center'><a href='javascript:void()' onClick="confirmation('$entry_type', '$entry_no')"> <font color='#FF0000'> delete </font></a></td></tr>";
&#058 is really a colon I don't know how the forum puts it that way.
I changed my code as you said and I also concatenated it. Now the popup window appears when I click the link but nothing is done as I click OK button.

I'm not familiar with js, so is that code is true if I want to move to another file (delete.php)

Code: Select all

window.location = "delete.php?table_name=" + entry_type + "&entry_no=" + entry_no;
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

in theory it should:

I have seen cases though where the void function takes precedence and overrides the location method.

you might try changing your href attribute to "javascript:confirmation('blah','bling')"
umbra
Forum Newbie
Posts: 21
Joined: Tue Sep 13, 2005 4:20 am

Post by umbra »

Burrito wrote:in theory it should:

I have seen cases though where the void function takes precedence and overrides the location method.

you might try changing your href attribute to "javascript:confirmation('blah','bling')"

Thank God it worked! Thank you to all those who helped me solving this
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Burrito wrote:you might try changing your href attribute to "javascript:confirmation('blah','bling')"
I like to use the void "trick" to hide javascript function names from the status bar in the browser during hovering on a link.. just a personal preference and I haven't really come across any compatability issues with it yet.
Post Reply