jkkenzie Posted May 8, 2008 Share Posted May 8, 2008 I would like to know how i can echo the below javascript in php; function validate ( form ) { if(form.name.value == ""){ alert('Please fill in the field'); form.name.focus(); return false; } return true; } Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/ Share on other sites More sharing options...
phpcodec Posted May 8, 2008 Share Posted May 8, 2008 <? some code here ?> <script language="javascript"> function validate ( form ) { if(form.name.value == ""){ alert('Please fill in the field'); form.name.focus(); return false; } return true; } </script> <? more code here ?> Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/#findComment-535951 Share on other sites More sharing options...
JSHINER Posted May 8, 2008 Share Posted May 8, 2008 Is there anyway to echo variables in the JavaScript? (I know I'm not helping towards a solution here just curious while we're on the subject) Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/#findComment-535957 Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 OR <?php // Some code here echo <<<END <script language="javascript"> function validate ( form ) { if(form.name.value == ""){ alert('Please fill in the field'); form.name.focus(); return false; } return true; } </script> END; // more code here ?> Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/#findComment-535958 Share on other sites More sharing options...
947740 Posted May 8, 2008 Share Posted May 8, 2008 Yes, you can echo variables in the javascript. Just put your variable in the script, and it wall automatically replace it for you (just like it should). Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/#findComment-536007 Share on other sites More sharing options...
benphp Posted May 8, 2008 Share Posted May 8, 2008 <?php some code here echo " <script language=\"javascript\"> function validate ( form ) { if(form.name.value == \"$myVar\"){ alert('Please fill in the field'); form.name.focus(); return false; } return true; } </script>"; more code here ?> Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/#findComment-536045 Share on other sites More sharing options...
rhodesa Posted May 8, 2008 Share Posted May 8, 2008 There is no need to echo the WHOLE thing...i think it's cleaner this way: <?php //code $myVar = 'ABC'; ?> <script language="javascript"> function validate ( form ) { if(form.name.value == "<?php echo $myVar; ?>"){ alert('Please fill in the field'); form.name.focus(); return false; } return true; } </script> Link to comment https://forums.phpfreaks.com/topic/104717-echo-javascript/#findComment-536081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.