php variable to javascript variable???

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
mimilaw123
Forum Newbie
Posts: 3
Joined: Wed Sep 27, 2006 10:52 am

php variable to javascript variable???

Post by mimilaw123 »

Here the delete confirm function in javascript. I would like to past the barCode1 and newBarCode1 to javacript in PHP. However, it can't do it.....can anyone help?

Code: Select all

<script type="text/javascript">
<!--
function confirmDelete(barCode1, newBarCode1) {
	var answer = confirm("Would you like to delete?")
	if (answer)
    {

	window.location = "admin_delete.php?barCode="+barCode1+"&action=del&newBarCode="+newBarCode1;
	}
}
//-->
</script>


HTML & PHP
<input name="delete" type="button" value="Del" onClick="confirmDelete(<? echo $barCode;?>, <? echo $newBarCode;?>)">
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

the only way you can do it is by putting these values in hidden variables i don't think there is any other method available.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Assuming that you have the variable names correct and the php is being parsed then you should be fine with what you have. What is the HTML output of the php script? You should get something like:

Code: Select all

<input name="delete" type="button" value="Del" onClick="confirmDelete(10, 11)">
Depending on what the variables hold.

I would recommend processing the variables to ensure they contain no illegal characters also as you directly insert them into the URL and so should be encoded if they contain any non-standard characters.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here how you can do it.

This code works

Code: Select all

<?php $barCode = "999888999777";?>
<?php $newBarCode = "2334424554";?>
<script type="text/javascript">
function disp_confirm()
{
var name=confirm("Are You Sure?")
if (name==true)
{
window.location = 'admin_delete.php?barCode=<?php echo $barCode;?>&action=del&newBarCode=<?php echo $newBarCode;?>';
}
else
{
document.write("You pressed the Cancel button!")
}
}
</script>
<form>
<input type="button" onclick="disp_confirm()" value="Delete"></form>
kind regards
Ian


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is how u you was trying to do it.

Code: Select all

<?php $barCode = "999888999777";?>
<?php $newBarCode = "2334424554";?>
<script type="text/javascript">
function disp_confirm(barCode1, newBarCode1)
{

var name=confirm("Are You Sure?")
if (name==true)
{
window.location = 'admin_delete.php?barCode=<?php echo $barCode;?>&action=del&newBarCode=<?php echo $newBarCode;?>';
}
else
{
document.write("You pressed the Cancel button!")
}
}
</script>
<form>
<input type="button" onclick="disp_confirm(<?php echo $barCode;?>, <?php echo $newBarCode;?>)" value="Delete"></form>
This one also works


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Your original code was fine, just put single-quotes around the values in the function call:

Code: Select all

<input name="delete" type="button" value="Del" onClick="confirmDelete('<? echo $barCode;?>', '<? echo $newBarCode;?>')">
Javascript was interpreting those values as variable references, and not as strings.
Post Reply