需要将PHP响应文本返回给HTML div

时间:2017-02-24 20:39:18

标签: javascript php jquery ajax

无法将PHP响应文本返回到HTML div。我通过AJAX成功地将一个变量发送到PHP表单但是在我的生活中无法弄清楚如何在不使用Alert的情况下将其注入Success div。我究竟做错了什么?谢谢!

在此处表格:

<form name="codesubmit" id="codesubmit">
                        <div class="field">
                            <font color="black"><label for="codeentry">Unique Code:</label></font>
                            <input pattern=".{5,5}" class="form-control" type="text" id="codeentry" name="codeentry" maxlength="5" title="Enter 5-character unique code." required>
                        </div>
                      <div class="rsvp-button text-center"><button type="submit" class="hvr-sweep-to-right">Submit</button>
                      <div id="loding" style="display:none; padding-left:20px;"><img src="images/ajax-loader.gif" alt="Loader"></div></div>
                      </form>
                      <div id="Success" class="successalert" style=" display:none;"> <strong>Thank you</strong>.
                            <p>ENTER RETURN VAR INFO HERE.</p></div>
                      <div id="Error" class="errorelert" style="display:none;"> <strong>Something went wrong, contact Ben Taylor please.</strong></div>
JS在这里:

(function ($) {
    'use strict';
    $("#codesubmit").submit(function (event) {

        event.preventDefault();

        $("#loding").css("display", "inline-block");
        $.post("codesubmit.php", {
                UniqueCode: $("#codeentry").val()
            })
            .done(function (data) {
                if (data) {
                    $("#loding").hide();
                    $("#Success").slideDown("slow");
                    //document.write("Test");

                    $("#codesubmit")[0].reset();
                }
                else {
                    $("#loding").hide();
                    $("#Error").slideDown("slow");
                    setTimeout(function () {
                        $("#Error").slideUp("slow");
                    }, 30000);
                }
            });

    });
})(jQuery);

PHP在这里:

<?php

    // Only process POST reqeusts.
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        // Get the form fields and remove whitespace.
        $codeentry = trim($_POST["UniqueCode"]);

        // Testing: Send Back Code.

        echo $codeentry; ?>

1 个答案:

答案 0 :(得分:0)

在.done(...)回调中,您会看到&#34;数据&#34;参数,具有您从PHP回显的内容。我建议获取数据中的值并将其存储在&#34; p&#34;您在表单中使用的标记,如下所示:

    int step_1= fmax(a[0], a[1]);
    int step_2 = fmax(a[2], a[3]);
    int step_3 = fmax(step_1, a[4]);
    int step_final = fmax(step_3, step_2);
    cout<<step_final;

然后在JS:

<div id="Success" class="successalert" style=" display:none;">
<strong>Thank you</strong>.
<p id="some_id">ENTER RETURN VAR INFO HERE.</p>
</div>