dtvfxzq3802 2012-07-02 02:19
浏览 182
已采纳

从PHP返回两个2 json对象

I have a small feedback form with two inputs. I want the error message to append to one another in the same div using JQuery. The error message works fine if there is only one error message, but when I return two json objects, both messages will not display at the same time. Is it possible to return 2 associative arrays with the same key to JQuery? How do I append the error messages to the same div? Everything works fine except how the error messages are displayed.

Note: This program is a JQuery Mobile Application

PHP code

if (isset($_POST['submit'])) {

    $email = $_POST['email'];
    $feedback = $_POST['feedback'];
    $errors = array();

    if (filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($email)) {
                 //Do something
    } else {
        $errors = "Invalid email.";
        echo json_encode(array("error" => "{$errors}"));

    }

    if (!empty($feedback)) {
                //Do something
    } else {
        $errors = "Feeback field can not be empty.";
        echo json_encode(array("error" => "{$errors}"));
    }

    if (empty($errors)) {
        //Database Insertion here
        echo json_encode(array("success" => "Feedback submission successful."));
    }
}  

JQuery Code

$(function() {
    $('#contact_form').bind('submit', function(event) {
        event.preventDefault();
        $.post('includes/form.php', $(this).serialize(), function(data) {
            if (data.error) {

                $('.error').slideDown(100).html(data.error);

            } else {
                $('.error').hide();
                $('.success').slideDown(100).html(data.success);
            }

        }, "json");
    });
}); 

HTML Form Code

         <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" id="contact_form" data-ajax="false">
            <label for="email">Email:</label>
            <input type="text" name="email" id="email" placeholder="Email">

            <label for="feedback">Feedback:</label>
            <textarea type="text" name="feedback" id="feedback" placeholder="Feedback"></textarea>              
            <input type="submit" name="submit" value="Submit Feedback" class="submit">

        </form>

Thank you in advance.

Edit: I feel so silly. I forgot to append the error messages in the php script haha. Thank you to all who answered.

  • 写回答

3条回答 默认 最新

  • ds355020 2012-07-02 06:49
    关注

    You can concatenate all the errors before you echo your output which is in json format

    E.g

     if (isset($_POST['submit'])) {
    
    $email = $_POST['email'];
    $feedback = $_POST['feedback'];
    $errors = "";
    
    if (filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($email)) {
                 //Do something
    } else {
        $errors .= "Invalid email.<br/>";
    
    
    }
    
    if (!empty($feedback)) {
                //Do something
    } else {
        $errors .= "Feeback field can not be empty.<br/>";
    }
    
    if ($errors != "") {
        echo json_encode(array("success" => "Feedback submission successful."));
    }else{
        echo json_encode(array("error" => $errors));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备