javascript 使用ajax将数据发送到php页面并获得响应并显示在字段中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19204771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending data to php page using ajax and get response and show in fields
提问by Mubin
I've the following form which I want to post to page.php and want to get result calculted on page.php and want to show that data in the table boxes using AJAX or JQUERY, is this possible? If yes so let me know please. I don't want to refresh the page as I want to be filled all data on single page and want to display all results on that page(IN table cells as user input data in one table and it will update its response).
我有以下表单,我想将其发布到 page.php 并希望在 page.php 上计算结果并希望使用 AJAX 或 JQUERY 在表格框中显示该数据,这可能吗?如果是,请告诉我。我不想刷新页面,因为我想在单个页面上填充所有数据并希望在该页面上显示所有结果(IN 表格单元格作为一个表格中的用户输入数据,它将更新其响应)。
<form method = "post" action = "page.php">
<input type = "text" name = "fld1" id = "fld1" />
<input type = "text" name = "result1" id = "result1" value = "value_from_php_page" disabled />
...
</form>
回答by edisonthk
Yes it is possible. Take a look at this example.
对的,这是可能的。看看这个例子。
On your page.php
在你的页面上.php
<?php
echo $_POST["fld1"];
?>
On your myForm.html. event.preventDefault() is needed, otherwise submit will perform at default and page will be reload.
在您的 myForm.html 上。event.preventDefault() 是必需的,否则提交将默认执行并重新加载页面。
<script>
$(function(){
$("#submit").click(function(event){
event.preventDefault();
$.ajax({
type:"post",
url:"page.php"
data:$("#form").serialize(),
success:function(response){
alert(response);
}
});
});
});
</script>
<form id="form" method = "post" action = "page.php">
<input type = "text" name = "fld1" id = "fld1" />
<input type = "text" name = "result1" id = "result1" value = "value_from_php_page" disabled />
<input type="submit" value="Submit" id="submit">
</form>