使用 ajax 将数据发送到 php 页面并获得响应并显示在字段中


Sending data to php page using ajax and get response and show in fields

>我有以下表单,我想发布到页面.php并希望在页面上计算结果.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>

是的,这是可能的。看看这个例子。

在您的页面上.php

<?php
    echo $_POST["fld1"];
?>

在你的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>