I really tried it myself, and I did read other questions asked here and elsewhere, but I just can't seem to get it right. And yes, I am still learning, so the code might appear confusing to you...
I have 1 page called index.php.
On that page I have a drop-down list, and based on the item chosen, a picture on the very same page should change:
<select name="animal-pictures" id = "animal" onchange="setImage(this)">
<option value="/images/placeholder.jpg">Choose animal</option>
<option value="/images/lion.jpg">Lion</option></select>
<img class="resized" src="/images/placeholder.jpg" name="image-swap" />
This gets me to the javascript:
function setImage(select){
var image = document.getElementsByName("image-swap")[0];
image.src = select.options[select.selectedIndex].value;}
What I now need, is to get select.options[select.selectedIndex].label (for example "Lion") out of the script so that I can use it somewhere else on the page.
What I have come up with so far (among many other versions):
function setImage(select){
var image = document.getElementsByName("image-swap")[0];
image.src = select.options[select.selectedIndex].value;
var data = select.options[select.selectedIndex].label;
var url = "index.php?id=" + data;
// alert(data);
// alert (url);
xmlhttp.open("GET",url,true);
xmlhttp.send();}
And how I try to retrieve that variable in php:
<?php
$name = $_GET['id'];
echo $name ?>
alert(data)
- gives me a window saying "lion"
alert (url)
- gives me a window saying "index.php?id=lion"
But I just can't get those information out of the script -.-
I'd really appreciate help. Please bear in mind that I'm still new to this - and so far, I'm just trying to get something "that's working" - even if there are 50 better ways to code it.