user6358161 user6358161 - 9 months ago 24
PHP Question

AJAX not passing values out to PHP

I am using Ajax to automatically update a $_SESSION value when a select option is used. Specifically, if a user selects a language from a dropdown list, $_SESSION['language'] must be update. This not happening with my implementation but I am not seeing why.

1) Select:

<select id="lang" name="lang">
<option value="en" data-reactid=".1.$en">English</option>
<option value="es" data-reactid=".1.$es">EspaƱol</option>
<option value="it" data-reactid=".1.$it">Italiano</option>
<option value="sv" data-reactid=".1.$sv">Svenska</option>

2) lang is passed to js and Ajax (post_language.js):

$(document).ready(function() {
$('#lang').change(function() {
var language_selection = $(this).val();
type: 'POST',
url: '../includes/ajax/language_ajax.php',
data: {lang: language_selection},
alert('This was sent back: ' + language_selection);
console.log('error: '+msg);}


At this point, the ALERT message appears correctly, HOWEVER, 'lang' never reaches $_SESSION echoed from within the url: language_ajax.php. In fact, the url is never even called by ajax.

3) language_ajax.php


$lang = $_POST['lang'];
echo "Language " . $lang;

I searched the forums for a few hours with no success. All solutions seem to not apply to the use of 'change' with a select list.

Can anyone help? thanks.


Text node data : "Language " . $lang echoed from php is not a valid child element of <select> element.

See <select>

Permitted content Zero or more <option> or <optgroup> elements.