1

I want to declare a PHP variable (most preferably a session variable) inside a javascript function.

Here is my html

<form style="text-align:center">
      Service: 
         <select id="selected">
            <option value="">Select Service</option>
            <?php foreach($serves as $serv ) { 
          if($serv['ser'] =='1') { ?>
            <option value="Home Delivery">Home Delivery</option>
            <?php } elseif($serv['ser'] =='2') { ?>
            <option value="Take Away">Take away</option>
            <?php } else  { ?>
            <option value="DinIN">DinIN</option>
            <?php } ?>
            <?php } ?>
         </select>
      </form>

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

<script type="text/javascript">
    $("#selected").on("change", function () {
        <?php $this->session->data['desired_service'] ?> = this.value;
    });
</script>

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?

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
  • 4
    You can't declare a PHP variable in JS. PHP is processed first then JS. You could send an AJAX request to update a PHP session variable. PHP is server side; JS is on the browser/client. – chris85 Oct 28 '15 at 05:23
  • 1
    you cannot directly use JS to change session variables, u can use ajax to run a php script on server which intern change your session for you.. – Hirdesh Vishwdewa Oct 28 '15 at 05:25
  • So i'd have to use ajax? – Ali Zia Oct 28 '15 at 05:27
  • 1
    Yes, you'd need to use Ajax. Or you could have JavaScript write a cookie which will be read on the next request to PHP. Or you can submit a form. – Ultimater Oct 28 '15 at 05:29
  • Okay I'll use ajax then. Thankyou! – Ali Zia Oct 28 '15 at 05:31
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – chris85 Oct 28 '15 at 05:36

3 Answers3

2

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: "http://url.of/somepage/",
    data: { sessionvalue: this.value }
    })
});

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

$this->session->data['desired_service'] = $_POST["somepage.php];
Arpita
  • 1,386
  • 1
  • 15
  • 35
-1

try this code:-

<script type="text/javascript">
var <?php echo $this->session->data['desired_service'] ?>;
    $("#selected").on("change", function () {
        <?php echo $this->session->data['desired_service'] ?> = this.value;
    });
</script>
Gaurav Srivastava
  • 3,232
  • 3
  • 16
  • 36
-3
<script type="text/javascript">
    // boolean outputs "" if false, "1" if true
    var bool = "<?php echo $bool ?>"; 

    // numeric value, both with and without quotes
    var num = <?php echo $num ?>; // 7
    var str_num = "<?php echo $num ?>"; // "7" (a string)

    var str = "<?php echo $str ?>"; // "A string here"
</script>
Raja C
  • 139
  • 1
  • 12