Page 1 of 1

confirmation popup problem

Posted: Tue Sep 13, 2005 11:20 am
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?

Posted: Tue Sep 13, 2005 11:34 am
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;
}

Posted: Tue Sep 13, 2005 12:48 pm
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;
}

Posted: Tue Sep 13, 2005 12:57 pm
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>";

Posted: Tue Sep 13, 2005 12:59 pm
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>";

Posted: Tue Sep 13, 2005 1:23 pm
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;

Posted: Tue Sep 13, 2005 1:27 pm
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')"

Posted: Tue Sep 13, 2005 1:38 pm
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

Posted: Tue Sep 13, 2005 1:51 pm
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.