php value into javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

php value into javascript

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply