我的表格与上面的表格类似:
<form id="calculator">
<input type="text" name="height" id="height"/>
<input type="text" name="inferiorPanel" id="inferiorPanel" readonly />
<button type="button" onclick="calculatePanels();">
</form>
Jquery脚本是下一个:
function calculatePanels() {
var Height = document.forms["calculator"]["height"].value;
$.ajax({
url: "includes/inferiorPanel.php",
success: function(result){
$("#inferiorPanel").val(result);
}});
}
来自inferiorPanel.php的PHP代码是:
<?php
$height = ????;
switch ($height) {
case ($height > 0 && $height <= 1845):
echo 615;
break;
case ($height > 1845 && $height <= 1980):
echo 495;
break;
case ($height > 1980 && $height <= 2100):
echo 495;
break;
case ($height > 2100 && $height <= 2220):
echo 495;
break;
case ($height > 2220 && $height <= 2340):
echo 615;
break;
case ($height > 2340 && $height <= 2460):
echo 615;
break;
case ($height > 2460 && $height <= 2475):
echo 495;
break;
case ($height > 2475 && $height <= 2595):
echo 615;
break;
case ($height > 2595 && $height <= 2715):
echo 615;
break;
case ($height > 2715 && $height <= 2835):
echo 615;
break;
case ($height > 2835 && $height <= 2955):
echo 615;
break;
case ($height > 2955 && $height <= 3000):
echo 615;
break;
}
我的问题是:
如何在不使用Ajax上的height
的情况下在PHP中传递变量POST
,以便在输入inferiorPanel
中显示结果?
我不想使用<input type="submit" />
因为会刷新页面
答案 0 :(得分:1)
页面刷新不是因为您使用$_POST
,而是因为您的代码不完整。
这里你需要做什么(你有jquery
,所以使用它):
从onclick
移除button
,移除<button>
,添加submit
:
<form id="calculator">
<input type="text" name="height" id="height"/>
<input type="text" name="inferiorPanel" id="inferiorPanel" readonly />
<input type="submit" name="Name" value="Value">
</form>
绑定处理程序以提交表单事件
$( "#calculator" ).submit(function() {
var Height = $( "#height" ).val();
$.ajax({
url: "includes/inferiorPanel.php",
data: { height: Height},
type: "POST",
success: function(result){
$("#inferiorPanel").val(result);
}
});
// simple way to prevent default action of form submission:
return false;
} );
在服务器端:
<?php
$height = $_POST['height'];
switch ($height) {
// run your switch here
}
?>
答案 1 :(得分:0)
您可以在不刷新页面的情况下使用POST
$.ajax({
url: "includes/inferiorPanel.php",
method: "POST",
data : {'height' : Height},
success: function(result){
$("#inferiorPanel").val(result);
}
});
并在php中:
<?php
$height = $_POST['height'];
?>
答案 2 :(得分:0)
您可以在Ajax调用中指定GET,并在URL中具有高度,如下所示
var height = $('#height').val();
$.ajax({
type : 'GET',
url : 'inferiorPanel.php?height=' + height,
在你的php中你可以说if(isset($ _ GET ['height'])$ height = $ _GET ['height'];