I have a select on which I want it to execute a query onchange.
(I want to get certain price for the product, depending on the size selected)
The problem is that I'm new to Ajax, and I don't know how to send multiple data. The first variable is the value from the select, and the second var, is the ID from the page someone is visiting.
I need to send to the "getprice.php" page the variables $i, which is $_GET["hidden_id_product"] and $q, which is the value from the onchange select.
I'm stuck here, this is my first post, I couldn't find a response for this, hope someone could help me out.
Select code: (product.php)
<select name="size" id="size" class="form-control" onchange="sizeSelected(this.value)">
<?php
$size_query = "SELECT * FROM sizes WHERE id_product=".$_GET["hidden_id_product"];
$size_result= mysqli_query($connection, $size_query);
if($fetch = mysqli_fetch_array($size_result)){
echo "<option selected value='0' disabled> - SELECT A SIZE - </option>";
if($fetch["seven"] > 0){
echo "<option value='7' > 7us </option>";
}else{
// do nothing
}
if($fetch["sevenhalf"] > 0){
echo "<option value='7.5'> 7.5us </option>";
}else{
// do nothing
}
// and so on...
}else{
echo "<option value='0'>SOLD OUT</option>";
}
?>
</select>
Script Used: (product.php)
<script>
function sizeSelected(str) {
var div = document.getElementById("id_product");
var str2 = div.textContent;
if (str == "") {
document.getElementById("price").innerHTML = "";
return;
} else {
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("price").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getprice.php?q="+str+"&i="+str2,true);
xmlhttp.send();
}
}
</script>
Get price code: (getprice.php)
<?php
include("connection.php");
$q = intval($_GET['q']);
$i = $_GET['i'];
if($q = 7){
$qt = "seven";
}elseif($q = 7.5){
$qt = "sevenhalf";
}elseif($q = 8){
$qt = "eight";
}elseif($q = 8.5){
$qt = "eighthalf";
}elseif($q = 9){
$qt = "nine";
}elseif($q = 9.5){
$qt = "ninehalf";
}elseif($q = 10){
$qt = "ten";
}elseif($q = 10.5){
$qt = "tenhalf";
}elseif($q = 11){
$qt = "eleven";
}elseif($q = 11.5){
$qt = "elevenhalf";
}elseif($q = 12){
$qt = "twelve";
}elseif($q = 13){
$qt = "thirteen";
}
$sql="SELECT ".$qt." FROM prices WHERE id_product = ".$i;
$result = mysqli_query($connection,$sql);
while($row = mysqli_fetch_array($result)) {
echo $row[$qt];
}
?>
The full page:
<div>
<span><h2><?php echo $row["name"];?></h2></span>
<hr>
<span><h3>"<?php echo $row["nickname"];?>"</h3></span>
<hr>
<span>
<div id="id_product" style="display: none;">
<?php
$id = $_GET["hidden_id_product"];
echo htmlspecialchars($id);
?>
</div>
<h4>
<?php echo $row["id_product"]; ?>
| <?php echo $row["brand"]; ?>
| <?php echo $row["year"] ?>
</h4>
</span><br>
<span>
<select name="size" id="size" class="form-control" onchange="sizeSelected(this.value)">
<?php
$size_query = "SELECT * FROM sizes WHERE id_product=".$_GET["hidden_id_product"];
$size_result= mysqli_query($connection, $size_query);
if($fetch = mysqli_fetch_array($size_result)){
echo "<option selected value='0' disabled> - Seleccione una talla - </option>";
if($fetch["seven"] > 0){
echo "<option value='7' > 7us </option>";
}else{
}
if($fetch["sevenhalf"] > 0){
echo "<option value='7.5'> 7.5us </option>";
}else{
}
}else{
echo "<option value='0'>SOLD OUT</option>";
}
?>
</select><br>
</span>
<div id="price"></div>
</div>