这很简单,但我似乎无法在Codeigniter中实现。
使用简单的php应该是:
<form id="volume" method="post" action="<?php echo $PHP_SELF;?>">
<label>length(cm):</label>
<input type="text" name="length">
<label>width(cm):</label>
<input type="text" name="width">
<label>height(cm):</label>
<input type="text" name="height">
<button type="submit">Submit</button>
</form>
<?php
$length = $_POST["length"];
$width = $_POST["width"];
$height = $_POST["height"];
$variable = 5000;
$sum_total = ($length * $width * $height) / $variable;
printf ("%01.2f" , $sum_total);
?> kg
我把表单在我的视图,php在我的控制器,但我希望它返回结果在同一页,而不是重定向我。
提前感谢大家的回复。
把类似的jQuery放到你的提交按钮页面上。
$(document).ready(function(){
$('#volume').submit(function(event){
event.preventDefault();
var post_array = {
length: $('input[name=length]').val();
width: $('input[name=width]').val();
// etc
};
$.post('path/to/controller/method', post_array, function(data){
alert(data); // do stuff;
}
return false; //to stop the form from submitting
});
});
将计算结果的PHP放在'path/to/controller/method'中,并命名为return $result;
。
当用户单击提交按钮时,它将向您的新视图提交ajax请求,并将结果返回给jQuery,然后您可以更改页面或您想要做的事情。
是$_SERVER["PHP_SELF"]
。请记住,对于一个帖子(除非使用ajax调用),需要刷新页面。