0

I have this html select form, I am using the onchange method to send and xmlhttp request.

<select class="form-control form-control-sm" id="sizeselector<?php echo $row["id"]?>" name="sizes" onchange="showColors(<?php echo $row["id"];?>, item, size)">
<?php while($row1 = $result1->fetch_assoc()){?>
<option value="<?php echo $row1["size"]?>"><?php echo $row1["size"]?></option>
<?php}?></select>
<div id="colorselector<?php echo $row["id"]?>">
    <p>Text</p>
</div>

The above code works, but when I change it to get the actual values from the rows...

onchange="showColors(<?php echo $row["id"];?>, <?php echo $row["name"]?>, this.value)"

It doesn't work at all, I have tried everything I can think of. I get no errors.

function showColors(id, item, size) {
if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        document.getElementById("colorselector"+id).innerHTML = this.responseText;
    }
};
try{
    xmlhttp.open("GET","getColor.php?s=size&c=size",true);
    xmlhttp.send();
}catch(err){
    document.getElementById("colorselector").innerHTML = err.message;
}

}

Updated: This is the output I get from the source code.

<select class="form-control form-control-sm" id="sizeselector40" name="sizes"onchange='showColors(40,'Women's Unicorn', this.value)'>

with using...

onchange='showColors(<?php echo $row["id"];?>, <?php echo $row["name"]?>, this.value)'

Update 2: Even when I manually add the values to the onchange, nothing happens..

onchange="showColors(27, t, a)"

function showColors(id, item, size) {
if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        document.getElementById("colorselector"+id).innerHTML = this.responseText;
    }
};
xmlhttp.open("GET","getColor.php?s="+item+"&c="+size,true);
xmlhttp.send();

}

I can run the getColor.php in another browser window, and get the result.

1
  • Updated post with new code. Commented Apr 6, 2018 at 20:18

3 Answers 3

1

You should use different quotes for javascript code and php index otherwise using the same you formed a wrong string

 onchange='showColors(<?php echo $row["id"];?> , <?php echo $row["name"]?>, this.value)'

or try

 echo "onchange='showColors(".$row["id"] .",'". $row["name"] . "', this.value)'";

and check with ctrl+U if the porper code is formed in you page source code

or if the quotes are to complex use some var

    echo ' var my_id = ' . $row["id"] . ';';
    echo ' var my_name = "' . $row["name"] . '";';

    echo "onchange='showColors( my_id ,my_name, this.value)'";

and do the present of single quote in name you could try using

onchange= "showColors(<?php echo $row['id'];?>, 
                    <?php echo '"'.  $row['name'] . '"' ?>, this.value)"
Sign up to request clarification or add additional context in comments.

7 Comments

I had tried that, it didn't work, Nothing had changed.
asnwer updated .. check if the code is properly created using CTRL + U
The problem is most likely that the name value has to be quoted in the list of arguments
as suggest by @PatrickQ i have added quote for the name values . (echo solution) hope is useful
anyway i have added a 3 suggestion .. hope ius useful
|
0
<select id="mySelect" onchange="myFunction()">
<option value="Audi">Audi
<option value="BMW">BMW
<option value="Mercedes">Mercedes
<option value="Volvo">Volvo
</select>



<script>
function myFunction() {
    var x = document.getElementById("mySelect").value;
    alert(x);
}
</script>

Comments

0

I guess you missed the semicolon after <?php echo $row["name"]?>.

Comments

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.