I am a complete newbie in ajax, but I read that Ajax is the only way to store a variable from jQuery and send it back to PHP to use it.
As you can see in this example, I have a drop down list populate from a MySQL database:
$query = "SELECT * FROM my_gallery";
$execute = mysqli_query($link, $query);
$results = mysqli_num_rows($execute);
if ($results!=0) {
echo '<label>The galleries are: ';
echo '<select id="galleries" name="galleries">';
echo '<option value=""></option>';
for ($i=0; $i<$results; $i++) {
$row = mysqli_fetch_array($execute);
$name = htmlspecialchars($row['galleryName']);
echo '<option value="' .$name. '">' .$name. '</option>';
}
echo '</select>';
echo '</label>';
}
With jQuery I add the selected attribute:
$('#page').change(function(e) {
e.preventDefault();
var selectedOption = $(this).find('option:selected');
$('#page option').removeAttr('selected');
$(selectedOption).attr('selected','selected');
var selectedOptionValue = $(selectedOption).val();
var selectedOptionText = $(selectedOption).text();
alert("You selected " + selectedOptionText + " Value: " + selectedOptionValue);
});
How can I store the selected option in a variable and send it back to PHP? Never used ajax, so please be as much detailed as possible and patience! :)