简体   繁体   中英

How can I put a selected item in a php post array with javascript?

I'm trying to post a selected item from a dropdown list on one php page so that I can access in another php page. The page does not have a submit button so I am using javascript at the bottom of the page. Here's the page that should put the item in post.

 <!DOCTYPE html>
 <html>
 <head>
 <?php session_start();?>
 </head>
 <body>

     <select class="myList" name="Country" id="country-selector"  autofocus="autofocus" autocorrect="off" autocomplete="off">
      <option value="" selected="selected">Select Country</option>
       <option value="Belgium" data-alternative-spellings="BE België Belgie Belgien Belgique" data-relevancy-booster="1.5">Belgium</option>
      </select>

  <Script>

  $(function () {

  $("#country-selector").change(function(){

  var x = document.getElementById("country-selector").value;

   alert(x);
   $.post('page2.php', {'selectedCountry': x});
   $.post('page2.php','val='+$(this).val(),function (response){

  alert(response);
  }); 

  });

  });
   </script>

   </body>
   </html>

Then I want page2.php as the target page. I go to page2.php and try to view the post array like this:

   <?php
  session_start();

  echo $_POST['selectedCountry'];

    print_r($_POST);

  ?>

...but the selectedCountry item is not in the $_POST array. How can I put a selected item in a php post array with javascript?
Thanks.

 $("#country-selector").change(function(){

  var x = document.getElementById("country-selector").value;

   $.post('page2.php', {'selectedCountry': x}).done(function(response) {
      alert(response);
  });

  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM