Page 1 of 1
php variable to javascript variable???
Posted: Mon Oct 30, 2006 5:08 am
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;?>)">
Posted: Mon Oct 30, 2006 5:29 am
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.
Posted: Mon Oct 30, 2006 5:57 am
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.
Posted: Mon Oct 30, 2006 6:21 am
by ianhull
feyd | Please use 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
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]
Posted: Mon Oct 30, 2006 6:29 am
by ianhull
feyd | Please use 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
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]
Posted: Mon Oct 30, 2006 6:56 am
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.