cliftonbazaar Posted June 16, 2009 Share Posted June 16, 2009 I have the following code <?PHP //Now let's remove the village $message = "Do you really wish to delete the village of ". $village_name."?"; echo "<TD align=center><B> <input type=button onclick=show_confirm($message) Value='Remove Village'> </B></TD>"; ?> but the show_confirm() will not accept the variable as an argument I have tried ('$message') and ("$message") but I can't get it too work. Note that if I put the word 'message' into show_confirm it displays the word message fine. Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/ Share on other sites More sharing options...
Zane Posted June 16, 2009 Share Posted June 16, 2009 Are you saying that the PHP is not being parsed....in that area? It should work just fine...the code you have. If you can echo the $message then you should definitely be able to do it as you are already. The problem may lie in your Javascript show_confirm() function. Have you tried using a simple alert($message) Show us your show_confirm() function Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856734 Share on other sites More sharing options...
cliftonbazaar Posted June 16, 2009 Author Share Posted June 16, 2009 This is the function as is <script type="text/javascript"> function show_confirm($message) { var r=confirm($message); if (r==true) { document.write("You pressed OK!"); //TODO change it } } </script> I can echo the variable correctly. Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856739 Share on other sites More sharing options...
haku Posted June 16, 2009 Share Posted June 16, 2009 <input type=button onclick=show_confirm(<?php echo $message; ?>) Value='Remove Village'> Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856753 Share on other sites More sharing options...
garethhall Posted June 16, 2009 Share Posted June 16, 2009 Please try this. I think you need to exit the echo to get the $message to work. Also when the page renders look in the source, What is in confirm()?? <?php $message = "Do you really wish to delete the village of ". $village_name."?"; echo "<TD align=center><B> <input type=button onclick=show_confirm(".$message.") Value='Remove Village'> </B></TD>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856754 Share on other sites More sharing options...
cliftonbazaar Posted June 16, 2009 Author Share Posted June 16, 2009 What I have done so far - I put show_confirm('message') and the word message came up in a pop up box - like it should, so I know the function works. In the page source it says onclick=show_confirm('message') . I change it to show_confirm('message me') and it doesn't work - so I have to assume that the problem is that it doesn't like the space. Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856834 Share on other sites More sharing options...
ict_ashley Posted June 16, 2009 Share Posted June 16, 2009 Indeed it doesn't ... it's effectively calling show_confirm(village name) which javascript won't understand. Try changing the line to onClick="show_confirm('".$message."')" and you should be OK. Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856835 Share on other sites More sharing options...
cliftonbazaar Posted June 16, 2009 Author Share Posted June 16, 2009 Indeed it doesn't ... it's effectively calling show_confirm(village name) which javascript won't understand. Try changing the line to onClick="show_confirm('".$message."')" and you should be OK. No, the page source comes up as this but it won't call the function onclick=show_confirm('Do you really wish to delete the village of Worcester?') If I change the variable to $message = "Do-you-really-wish-to-delete-the-village-of-". $villages_array[$i*5]."?"; then it works perfectly, the spaces are giving me the problems. Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856846 Share on other sites More sharing options...
ict_ashley Posted June 16, 2009 Share Posted June 16, 2009 Sorry, went a bit crazy with the old [bold] tag there Try the single quotes ... it really does work... <a href="anywhere.php" onclick="show_confirm('<?php print $message;?>');">here</a> Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856857 Share on other sites More sharing options...
cliftonbazaar Posted June 16, 2009 Author Share Posted June 16, 2009 Sorry, went a bit crazy with the old [bold] tag there Try the single quotes ... it really does work... <a href="anywhere.php" onclick="show_confirm('<?php print $message;?>');">here</a> Perfect! Spaces and all Quote Link to comment https://forums.phpfreaks.com/topic/162319-solved-putting-a-variable-into-a-confirm-box/#findComment-856900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.