【问题标题】:Getting data from PHP page on form submit从表单提交的 PHP 页面获取数据
【发布时间】:2013-08-27 14:41:43
【问题描述】:

我有一个带有表单的 index.php。当它被提交时,我希望 process.php 的结果显示在 index.php 的结果 div 中。很确定我需要某种 AJAX,但我不确定......

index.php

<div id="result"></div>

<form action="" id="form" method="get">
    <input type="text" id="q" name="q" maxlength="16">
</form>

process.php

<?php

$result = $_GET['q'];

if($result == "Pancakes") {
    echo 'Result is Pancakes';
}

else {
    echo 'Result is something else';
}

?>

【问题讨论】:

  • 如果您对 AJAX(这是实现它的方法)非常确定,那么请阅读一些关于它的内容。至少谷歌AJAX :)
  • 您为 AJAX 尝试过什么?尝试使用 AJAX,然后告诉我们问题所在。
  • 我不想把它作为一个答案,因为它是微不足道的,但如果你读到这个 ​​api.jquery.com/load 你将能够在一行中完成(如果你愿意,也可以是 2-3 行点击它)
  • @Dexa 考虑到 OP 的往绩记录,可能无论如何都没有关系,从未接受任何答案。所以你的和其他人一样好。

标签: php jquery ajax forms get


【解决方案1】:

您真的不需要为此“需要”AJAX,因为您可以将其提交给自己并包含流程文件:

index.php

<div id="result">
    <?php include('process.php'); ?>
</div>

<form action="index.php" id="form" method="get">
    <input type="text" id="q" name="q" maxlength="16">
    <input type="submit" name="submit" value="Submit">
</form>

process.php

<?php
// Check if form was submitted
if(isset($_GET['submit'])){

    $result = $_GET['q'];

    if($result == "Pancakes") {
        echo 'Result is Pancakes';
    }

    else {
        echo 'Result is something else';
    }
}
?>

实施 AJAX 将使事情变得更加用户友好,但它肯定会使您的代码复杂化。不管你走哪条路,祝你好运!

这是一个 jquery Ajax 示例,

<script>
//wait for page load to initialize script
$(document).ready(function(){
    //listen for form submission
    $('form').on('submit', function(e){
      //prevent form from submitting and leaving page
      e.preventDefault();

      // AJAX goodness!
      $.ajax({
            type: "GET", //type of submit
            cache: false, //important or else you might get wrong data returned to you
            url: "process.php", //destination
            datatype: "html", //expected data format from process.php
            data: $('form').serialize(), //target your form's data and serialize for a POST
            success: function(data) { // data is the var which holds the output of your process.php

                // locate the div with #result and fill it with returned data from process.php
                $('#result').html(data);
            }
        });
    });
});
</script>

【讨论】:

  • 您的 if(isset($_POST['submit'])){ 不起作用。我将其更改为if(isset($_GET['submit'])){,现在它可以工作了。
  • 您的 type: "POST", 很可能需要在您的 Ajax 方法中设置为 type: "GET",
  • 现在,如果有人输入“pancakes”而不是“Pancakes”怎么办? ;-)
  • 哈哈,我需要更多的咖啡!
  • 这是一个很大的 "10-4"(不需要额外的字符);-)
【解决方案2】:

这是 jquery Ajax 示例,

  $.ajax({
        type: "POST",
        url: "somescript.php",
        datatype: "html",
        data: dataString,
        success: function(data) {
            doSomething(data);
        }
    });

【讨论】:

    【解决方案3】:

    如何在 index.php 中执行此操作:

    &lt;div id="result"&gt;&lt;?php include "process.php"?&gt;&lt;/div&gt;

    【讨论】:

      【解决方案4】:

      有两种方法。

      要么使用 ajax 调用你的 process.php(我推荐jQuery——发送 ajax 调用并根据结果执行操作非常容易。)然后使用 javascript 更改表单。

      或者让创建表单的php代码与表单提交的php代码相同,然后根据是否有get参数输出不同的东西。 (编辑:MonkeyZeus 为您详细说明了如何执行此操作。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-08
        • 1970-01-01
        • 2014-08-02
        • 1970-01-01
        相关资源
        最近更新 更多