sseeley Posted June 5, 2009 Share Posted June 5, 2009 If I have a value $test in PHP how can I get this value using Java script, I would like to use this is a form validation? Many thanks in advance? Link to comment https://forums.phpfreaks.com/topic/161107-get-php-variable-with-javascript/ Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 When you generate the page from PHP, include this in the output: <?php echo sprintf( '<input type="hidden" name="hdnTest" id="hdnTest" value="%s" />', htmlentities( $test, ENT_QUOTES ) ); ?> If $test has 'Hello World' in it, then that should output: <input type="hidden" name="hdnTest" id="hdnTest" value="Hello World" /> Then in JavaScript you can: alert( document.getElementById( 'hdnTest' ).value ); Link to comment https://forums.phpfreaks.com/topic/161107-get-php-variable-with-javascript/#findComment-850165 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.