Page 1 of 1

php value into javascript

Posted: Tue Aug 21, 2007 4:48 am
by bouncer
hi,

i'm trying to assign a value to a variable in php, and then pass that value to a variable inside a script but i dont have any idea how to do that.

Code: Select all

<?
if ( isset( $resultConfig ) && ! empty( $resultConfig ) ) {
	$sepID = 'config';
} else {
	$sepID = 'car';
}
?>

<script language="javascript">

var oldsep = <? echo $sepID; ?>;

</script>
this is just a example of what i want to do.

EDITED:

i've got a example from a page where they give this code, but i cant make it work :roll:

Code: Select all

//In either way, the PHP script gets the passed values like 
$lat = $_REQUEST['lat']; 

//After processing these values in PHP, you can pass the processed values to Javascript like 
var new_lat = <? echo $new_lat; ?>;

thanks in advance

Posted: Tue Aug 21, 2007 6:57 am
by CoderGoblin

Code: Select all

<?
if ( isset( $resultConfig ) && ! empty( $resultConfig ) ) {
        $sepID = 'config';
} else {
        $sepID = 'car';
}
?>

<script language="javascript">

var oldsep = <? echo $sepID; ?>;

</script>
This makes use of short tags which may not be defined to be used in your php.ini. Try replacing all <? with <?php and see if that helps. It is considered by many bad practice to use short tags.