How to change a PHP variable depending on a But since I'm not able to change the Php variable throw JS no point of doing it. function Change() { var dep = document.getElementById("Selection"); if (dep.value == 1) { //Change layout value }if (dep.value ==2 ) { alert("issou2"); //change layout2value } } – No 0ne Nov 25 '19 at 10:50

  • 1
    You have to understand that when Javascript runs then PHP has completed running and even if it was magically possible to change a variable on the server from the client it wouldn't change anything because PHP has completed execution. That's basically how the whole internet works. If you do something that doesn't require reloading the page then it is always javascript. How to accomplish your task is a design question. Do you want to put all possible values in the page and select it in JS, or do you provide an endpoint for AJAX requests that executes PHP and provides you with the result, or..... – Longoon12000 Nov 25 '19 at 10:54
  • "I wish to change the $layout value depending, on the user Selection without having to refresh the Page." ...this requires AJAX. Here's the basic concept: You handle the change event of the select box. Then you send the selected value to the server via AJAX. The Server receives that request, runs some PHP, reads the value, and sends the correct output back. Your JavaScript code receives the output, and then decides how to update the page in response to that output. – ADyson Nov 25 '19 at 10:59
  • try this function Change() { var layout = ''; var layout1 = ''; var dep = document.getElementById("Selection"); if (dep.value == 1) { // use layout as you want } else if (dep.value ==2 ) { // use layout1 as you want } } – Sagar Sainkar Nov 25 '19 at 10:59
  • 3 Answers3

    3

    There is no possibility to change php variable without refreshing the page. But as per your requirement you can use techniques to achieve your goal. I suggest below solution for this using ajax call.

    index.php

     <?php?>
    <!DOCTYPE html>
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
            <script>
                $(document).ready(function(){
                $('#sel1').change(function(){
                    var getValue = $(this).val();
                   // alert(getValue);
                    //Ajax call
                    $.post('do.php', { selectedValue: getValue }, function(data){
                        //alert('check response value:  '+data);
                    });
                });
            });
            </script>
        </head>
    <body>
        <select class="form-control" id="sel1" id="Name" name="Sender[department]">
           <option value="1">Choix1</option>
           <option value="2">Choix2</option>
           <option value="3">Choix3</option>
           <option value="4">Choix4</option>
           <option value="5">Choix5</option>
           <option value="6">Choix6</option>
        </select>
    
    </body>
    </html>
    

    do.php

    <?php
    // get posted value
    if ($_POST['selectedValue']){ 
         //echo "posted--". $_POST['selectedValue'];
        // do your logic here
        //$layout = file_get_contents('./SIG/SAELEN.html', FILE_USE_INCLUDE_PATH);
        //$layout1 = file_get_contents('./SIG/GUIL.html', FILE_USE_INCLUDE_PATH);
    }
    ?>
    

    I hope you can get an idea by using this way.

    Dinesh Chandra
    • 662
    • 3
    • 12
    • 23
    1

    we cannot change PHP variable from javascript. Php is backend and js is frontend.

    Solution is to call a ajax call when ever user changes the select input

    or

    you can store both layouts in two js variables and use them when user changes the select input

    Vinod Sai
    • 1,956
    • 3
    • 13
    • 23
    0
       $layout = file_get_contents('./SIG/1.html', FILE_USE_INCLUDE_PATH);
    
    
     $layout1 = file_get_contents('./SIG/2.html', FILE_USE_INCLUDE_PATH);
       $layout2 = file_get_contents('./SIG/3.html', FILE_USE_INCLUDE_PATH);
       $layout3 = file_get_contents('./SIG/4.html', FILE_USE_INCLUDE_PATH);
    
    
    if(isset($_POST['select']))
    
    {
        if($_POST['select'] == 'value2')
        {
            $layout = $layout1;
    
        }
        if($_POST['select'] == 'value3') {
        $layout = $layout2;
        }
    
        if($_POST['select'] == 'value4') {
        $layout = $layout3;
                                         } 
    

    }

    No 0ne
    • 61
    • 10