Jump to content


Photo

PHP variables to JavaScript.

javascript php

Best Answer Barand, 19 May 2013 - 04:49 PM

function process($test) {

echo "
<script>

var jsvar;
jsvar = \"$test\";

alert(jsvar)

</script> ";

} 
Go to the full post


  • Please log in to reply
2 replies to this topic

#1 IshtarUK

IshtarUK

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 19 May 2013 - 03:47 PM

Hello All

 

I am having a problem trying to transfer variables from PHP to Javascript. 

 

My demonstration program:

// PHP Demonstration Functions
function test {
$test = "HELLO WORLD";
process($test);
}


function process($test) {

echo "
<script>

var jsvar;
jsvar = '<?php echo $test;?>'

alert(jsvar)

</script> ";

} 

The code above does not show the words HELLO WORLD instead the result is:

 

<?php echo HELLO WORLD; ?>

 

 If I remove the single quotes it generates a javascript error. 

 

 

This problem has been driving me nuts for the last few hours. I appreciate any help offered.

 

Many thanks 

Nikki

 



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,268 posts
  • LocationCheshire, UK

Posted 19 May 2013 - 04:49 PM   Best Answer

function process($test) {

echo "
<script>

var jsvar;
jsvar = \"$test\";

alert(jsvar)

</script> ";

} 

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 IshtarUK

IshtarUK

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 19 May 2013 - 05:04 PM

Fantastic, works perfectly.

 

I appreciate you help! 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com