0

I have the same problem here, but what I need is to get the value of selected option then save it in a php variable rather than a textbox. I spent almost 2days searching for my gold. Any help will do.thank you.

Edited Here is how I write the code.

<script>
$(document).ready(function () {
  $("#country").change(
   function () {
       var options = {
                url: "test.php",
               type: "post",
               dataType: "json",
               data: "country=" + $(this).val(), //build your data string here
             success: function (json) {
                 $("#textbox").val(json.country);
             }
           };
       $.ajax(options);
   }
 );
 });
</script>
<select id="country" name="country">
 <option value="Delhi" >India</option>
 <option value="manila" >phil</option>
  <option value="tokyo" >japan</option>
</select>
<?php

  @$d = $_POST['country'];
  echo $d;
  var_dump($d);


?>
1
  • 4
    you need to sent a request to sever for that probably using a ajax request Commented Jun 24, 2013 at 3:06

2 Answers 2

3

You can do like this. (jQuery Ajax example)

on dropdown selection:

<select id="city">
   <option value="New York">New York</option>
   <option value="London">London</option>
   <option value="Washington DC">Washington DC</option>
</select>

JS Code

$(document).ready(function() {
   $('city').onchange(function() {
      $.ajax({
        type: "GET",
        url: "some.php",
       data: { city: this.val() }
      }).done(function( msg ) {
        alert( "Data Saved: " + msg );
      });
   });
});

PHP

some.php

you can get the value by using GET or REQUEST methods.

$city = $_GET['city'];
$city = $_REQUEST['city'];
Sign up to request clarification or add additional context in comments.

Comments

1

I will take the same example and modify and give you an answer.

<script>
$(document).ready(function () {
   $("#country").change(
       function () {
           var options = {
                    url: "/path/to/your.php",
                   type: "post",
                   dataType: "json",
                   data: "country=" + $(this).val(), //build your data string here
                 success: function (json) {
                     $("#textbox").val(json.country);
                 }
               };
           $.ajax(options);
       }
   );
});
</script>
<select id="country" name="country">
  <option value="Delhi" >India</option>
  <option value="manila" >phil</option>
  <option value="tokyo" >japan</option>
</select>

in order to capture the value on the server side in your php file

 $var = $_POST['country'];
 $json = array('country' => $var);
 header("Content-Type: application/json");
 echo json_encode($json);

Updated code will return the country back to the script via json and write to a <input type=text field which has id=textbox

10 Comments

how can I catch the value?
$account = $_POST['country']; or $account = $_GET['country'];
my form has no submit. how will I check it in my url
use the console log if your using chrome
@user2446342 Just to ask the obvious question but you do know that php isn't like javascript correct? Php is done on the server and once a page is served to the client php can only be accessed via further server requests (via forms or ajax etc). So you can't do something like <?php $myvar ?> = $('#myselect').val();
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.