我正在尝试从php中的形式获取两个值,即体重和性别 使用jquery和ajax显示与该性别和体重相对应的价格
mysql表如下所示
gender range1 range2 price
male 0 100 20
male 101 200 30
female 0 100 25
female 101 199 46
php形式如下
<input type="text" name="gender" id="gender">
<input type="number" name="weight">
price <span id="result"></span>
<input type="button" herf="javascript:;" onclick="getprice" value="">
ajax函数如下所示
function getprice (gender, whieght) {
var str_num {
"gender" : gender,
"weight" : weight
};
$.ajax({ data: str_num,
url: 'selectprice.php',
type: 'post'
beforeSend: function() {
$("#result").html("in progress..");},
succes: function (reponse){
$("result").html(reponse);
})}}
selectprice.php看起来像这样
$gender = $_GET["gender"];
$weight = $_GET["weight"];
$fetch = "SELECT * FROM table where gender like $gender and weight
>range1 and <range2";
$result = mysqli_query($con, $fetch) or die("Ocurrio un error en la
consulta SQL");
while ($row = $resul->fetch_assoc()) {
echo "".$row["price"]."";
}
echo $result;
当我单击该按钮时,什么也没有发生,我真的可以使用一些帮助,在此先感谢
答案 0 :(得分:1)
您真的把错误打包了。
<input type="text" name="gender" id="gender">
<input type="number" name="weight" id="weight">
price
<input type="button" herf="javascript:;" onclick="getprice()" value="">
<script>
$(document).ready(function() {
});
function getprice () {
var str_num = {
"gender" : $('#gender').val(),
"weight" : $('#weight').val()
};
$.ajax({
data: str_num,
url: 'selectprice.php',
type: 'post',
beforeSend: function() {
$("#result").html("in progress..");
},
succes: function (reponse){
$("result").html(reponse);
}
})
};
</script>
PHP文件
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
// ini_set('log_errors', true);
var_dump($_POST); // Your doing a post not a get in the javaacript.
答案 1 :(得分:0)
您需要更新行:
url: 'selectprice.php?weight='+weight+'&gender='+gender,
或者您可以在data
之后通过url
url: 'selectprice.php',
data: {weight:weight, gender,gender},
答案 2 :(得分:0)
您在此处混合使用“ POST”和“ GET”。
如果您想使用“ GET”,请遵循Naveed的建议
如果要使用“ POST”,请在下面找到两种可能的解决方案:
1)简单的一种:将变量直接包含在数据中
$gender = $_GET["gender"]; $weight = $_GET["weight"];
selectprice.php:
替换
$gender = $_POST["gender"]; $weight = $_POST["weight"];
使用
function getprice (gender, weight) {
var data={
"gender" : gender,
"weight" : weight
};
var str_num = JSON.stringify(data);
$.ajax({ data: str_num,
url: 'selectprice.php',
type: 'post'
beforeSend: function() {
$("#result").html("in progress..");},
success: function (reponse){
$("result").html(reponse);
})}}
2)稍微复杂一点:使用stringify
$gender = $_GET["gender"]; $weight = $_GET["weight"];
selectprice.php:
替换
$json_data_from_js= json_decode($_POST['data']);
$gender = $json_data_from_js->gender ;
$weight = $json_data_from_js-> weight;
使用
scatter = plt.scatter(x, y)
ax = scatter.axes
ax.invert_xaxis()
ax.invert_yaxis()