How to declare php variable in javascript

I want to declare a PHP variable [most preferably a session variable] inside a javascript function.

Here is my html


      Service: 
         
            Select Service
            
            Home Delivery
            
            Take away
            
            DinIN
            
            
         
      

and here if my javascript function but this is not working.


    $["#selected"].on["change", function [] {
         = this.value;
    }];

Basically on option change, I want a session variable to hold the value of option whichever is selected. Is there any way to do it?

asked Oct 28, 2015 at 5:21

6

You can not set php variable using javascript, use Ajax instead.

Actually php is a server side language and is executed until your webpage if fetching data from server as soon as your page gets completely loaded you loose the access to php variables.

You can achieve this only by AJAX

And to do it with ajax on your current page you can write a function like

$["#selected"].on["change", function [] {
    $.ajax[{
    method: "POST",
    url: "//url.of/somepage/",
    data: { sessionvalue: this.value }
    }]
}];

and then on //url.of/somepage/ you can do

$this->session->data['desired_service'] = $_POST["somepage.php];

answered Oct 28, 2015 at 5:28

ArpitaArpita

1,3541 gold badge15 silver badges35 bronze badges

1

try this code:-


var ;
    $["#selected"].on["change", function [] {
         = this.value;
    }];

answered Oct 28, 2015 at 5:22

4


    // boolean outputs "" if false, "1" if true
    var bool = ""; 

    // numeric value, both with and without quotes
    var num = ; // 7
    var str_num = ""; // "7" [a string]

    var str = ""; // "A string here"

answered Oct 28, 2015 at 5:27

Raja CRaja C

1391 silver badge12 bronze badges

1

Sometimes, you may need to declare dynamic JS variables using PHP.

One thing you can do is to manually write the JS code as following.

var x = "";

Enter fullscreen mode Exit fullscreen mode

But, we can create a function to do this easily with a better data types support.

function phpVarsToJs[$vars] {
    echo '';
    foreach [$vars as $key => $val] {
        echo "var $key =";
        if [is_int[$val]] echo $val;
        else if [is_bool[$val]] echo $val ? 'true' : 'false';
        else if [is_string[$val]] echo '"' . $val . '"';
        else if [is_array[$val]] echo json_encode[$val];
        echo ';';
    }
    echo '';
}

Enter fullscreen mode Exit fullscreen mode

This function requires an PHP array, which contains key/value pairs which are the Javascript variable name and the value. Also, this function automaically creates Javascript variables according to the data type of the PHP variable.

PHP Arrays will be converted into JSON format. If you don't need the '' tags, just remove those two echo statements.

Example




    My App


    This is my app!

    


Enter fullscreen mode Exit fullscreen mode

Thanks!

I previously published this post on PHP Group on my new website, Hyvor Groups.

How set PHP variable value in JavaScript?

Use this code to solve your problem. var abc= 'this is text';

Chủ Đề