Jump to content

Get PHP variable with JavaScript


sseeley

Recommended Posts

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 );

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.