2021年3月6日星期六

Problem getting PHP session variable in next pahe if it is assigned by HTML select Tag

I need help in transferring the session variable from one page to another while choosing the session variable from the SELECT tag. I have given my code below. I want to show the selected option in the page1.php and when the option is selected and after that when user clicks or submit the form then the value of session variable transferred to page2.php. If I don't redirect my page to page2.php then after clicking the submit button I can get the value of the selected session option on the same page. However, for this to achieve I had the click submit. So I found another solution and with jQuery, I got the selected value in pahe1.php one option is selected without submitting the form. Now when I redirect to page2.php then my session variable does not change according to the selected value. Whatever value show it first remain unchanged.

I am new, it may be a silly question, but I am not able to fix it. I appreciate if I can get help/hint to fix this issue, thank

page1.php    <?PHP  session_start();  if (isset($_POST['submit'])) {      $city = $_POST['city'];      $_SESSION['city'] = $city;  }  ?>      <div><?php echo $_SESSION['city']; ?></div>      <form method="post" action="page2.php">                    <select name="city" id="city>              <option value="" selected>Please select city</option>              <option value="London">London</option>              <option value="Sydney">Sydney</option>                         </select>                        <input type="submit" name="submit" id="submit" value="Next">          <br>          <p id="city"></p>         </form>       *************************      <script>          $('select[name="city"]').on('change', function () {              var option = $('select[name="city"]:checked');              if (this.value == 'London') {                  $("#city").html("London");              } else if (this.value == 'Sydney') {                  $("#city").html("Sydney");              }          });      </script>     ************************   ************************   page2.php    <?PHP  session_start();  ?>    <div><?php echo $_SESSION['city'];  ?></div>  
https://stackoverflow.com/questions/66512705/problem-getting-php-session-variable-in-next-pahe-if-it-is-assigned-by-html-sele March 07, 2021 at 10:38AM

没有评论:

发表评论