Reputation: 1941
I have been having problems returning results from my database using AJAX. I have tried to echo $diskspace
and $price
, both of which are returning undefined.
index.php
<form id="form" method="POST">
Diskspace:
<select id="Diskspace">
<option value="0 AND 1">$0 - 1GB</option>
<option value="1 AND 5">$1 - 5GB</option>
<option value="5 AND 10">$5 - 10GB</option>
</select></br></br>
Price:
<select id="Price">
<option value="0 AND 5">$0 - $5</option>
<option value="1 AND 5">$5 - $10</option>
<option value="10 AND 20">$10 - $20</option>
<option value="20 AND 40">$20 - $40</option>
<option value="40 and 500">>$40</option>
</select></br></br>
<input type="submit" id="submit" value="enter">
</form>
<div id="output"></div>
JS
$(document).ready(function(){
$("div#output").hide();
$("#submit").click(function(){
$.post('join.php', {
diskspace: $("#diskspace").val(),
price: $("#price").val()
},
function(data){
$("div#output").html(data);
$("div#output").show();
}
);
return false;
});
});
</script>
join.php
<?php
if (isset($_POST['diskspace'])){
mysql_connect("localhost","root","") or die('Could not connect');
mysql_select_db("webhost") or die ('Could not find DB');
$diskspace = $_POST['diskspace'];
$price =$_POST['price'];
$query = mysql_query("
SELECT * FROM data WHERE Diskspace BETWEEN $diskspace
AND Price BETWEEN $price
");
$count = mysql_num_rows($query);
if ($count == 0){
$output = "No such results, sorry.";
}else{
while($row = mysql_fetch_array($query)){
$diskspace = $row['Diskspace'];
$price = $row['Price'];
$host = $row['Provider'];
$output .= '<div>'.$host.' '.$diskspace.' '.$price.'</div>';
}
}
echo $output;
}
?>
Upvotes: 0
Views: 134
Reputation: 439
replace:
diskspace: $("#diskspace").val(),
with
diskspace: $("#Diskspace").val(),
Upvotes: 1
Reputation: 9351
Your select id is not same in your jquery. Change it like this:
<form id="form" method="POST">
Diskspace:
<select id="diskspace">
<option value="0 AND 1">$0 - 1GB</option>
<option value="1 AND 5">$1 - 5GB</option>
<option value="5 AND 10">$5 - 10GB</option>
</select></br></br>
Price:
<select id="price">
<option value="0 AND 5">$0 - $5</option>
<option value="1 AND 5">$5 - $10</option>
<option value="10 AND 20">$10 - $20</option>
<option value="20 AND 40">$20 - $40</option>
<option value="40 and 500">>$40</option>
</select></br></br>
<input type="submit" id="submit" value="enter">
</form>
<div id="output"></div>
Upvotes: 1
Reputation: 360572
You didn't give your <select>
s names:
<select id="Diskspace" name="Diskspace">
^^^^^^^^^^^^^^^^--- missing
No name, no form submission for that field. id
does NOT count - it's used purely for DOM operations, and is not relevant for form submissions.
Beyond that, you're vulnerable to SQL injection attacks.
Upvotes: -4
Reputation: 16828
Your HTML element has an id of Diskspace
and you're looking for an element of diskspace
.
Take the following for example:
<select id="Diskspace">
<option value="hello">hello</option>
</select>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
console.log( $('#diskspace').val() );
});
</script>
The result is undefined, and in new versions of jQuery, if the element cannot be found it will not pass the variable through ajax/post.
The same applies to your Price
element.
Upvotes: 4