No 0ne
No 0ne

Reputation: 61

How to change a PHP variable depending on a <select> HTML

I'm currently making an auto-signature in PHP & HTML.

The variable 'layout' is the variable I want to change. It contains the structure of the Html signature (Background, logo, Structure).

$layout = file_get_contents('./SIG/SAELEN.html', FILE_USE_INCLUDE_PATH);
$layout1 = file_get_contents('./SIG/GUIL.html', FILE_USE_INCLUDE_PATH);

The Structure is supposed to change, depending on this :

<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>

I wish to change the $layout value depending, on the user Selection without having to refresh the Page.

Thanks for taking the time to read, I'm looking forward to hearing your suggestions.

Upvotes: 0

Views: 2339

Answers (3)

No 0ne
No 0ne

Reputation: 61

   $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;
                                     } 

}

Upvotes: 0

Dinesh Chandra
Dinesh Chandra

Reputation: 672

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.

Upvotes: 3

Vinod Sai
Vinod Sai

Reputation: 2122

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

Upvotes: 1

Related Questions