0

on a index.php page, inside html i have a selectbox

<select name="selectLanguage" id="selectLanguage" onchange="OnlanguageChange()">
  <option value="">
    Select your Language
  </option>

  <option value="en">
    English
  </option>

  <option value="fr">
    Français
  </option>

  <option value="de">
    Deutsch
  </option>                                             

</select>

Wish to get the value of the select box onchange in my php code.

tried with ajax call :

$(document).ready(function(){ 
  $("#selectLanguage").change(function(){ 
    var SelectedLanguage = $(this).val(); 
    alert(SelectedLanguage);
    $.ajax({ 
      type: "POST", 
      url: "index.php", 
      data: SelectedLanguage, 
      success: function(result){ 

      }
    });

  });
});

2 Answers 2

1

$.ajax require data as an object...

$.ajax({
  type: "POST",
  url: "index.php",
  data: { language: SelectedLanguage },
  success: function(result) {

  }
});

In php access it as $_POST['language']

Sign up to request clarification or add additional context in comments.

14 Comments

Have you added data: { language: SelectedLanguage }, to your $.ajax function params?
Try using filter_input( INPUT_POST, 'language' ) to avoid error when not set...
that vanished the error. but the value is not getting sent to php page
@SalilLambay what do you see in Netwroks in Chrome DevTools? Are there "form data" and "language" in this request?
@ИванПшеницын data in networks cn be seen "language : en" but not able to still get data in print filter_input( INPUT_POST, 'language' ); or $_POST['language']
|
0
           var formData = {     
              //get the value of language selected
               'SelectedLanguage':$("select[name='selectLanguage'] option:selected").val(),
            };
            $.ajax({ 
            type: "POST", 
            url: "index.php",
            //send language to index.php
            data: formData , 
            success: function(result){ 

              }
        });

9 Comments

Please add some explenation to your answer. Only posting a bit of code doesn't provide a good answer.
Why $("select[name='selectLanguage'] option:selected")? $(this).val() is right code.
i suppose var SelectedLanguage = $(this).val(); is doing the same job that 'SelectedLanguage':$("select[name='selectLanguage'] option:selected").val() is doing... still thanks... a new way to do that
Even this gives me formdata in the networks of chrome dev tools, but in $_POST['language'] still not able to get data
update print "True"; to print "<div id='result'>True</div>" ; and add var res = language.split('\n')[1]; $("#result").html(res); to success: function(language) {
|

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.