首页 文章

ajax表单提交多个值并获得响应

提问于
浏览
0

我正在使用表单并提交一个名字,姓氏,电子邮件,用户名作为字段的表单 . 当ajax返回时,它返回一串错误消息 . 如何分离错误消息?我正在使用此代码
$(document).ready(function(){$(":button[value=Done]").click(function(){data_first_name = $("#first_name").val(); data_last_name = $("#last_name").val(); data_user_name = $("#user_name") .val(); data_email = $("#email").val(); data_role = $("#role").val(); data_password = $("#password").val(); data_profile_image = $("#upload_admin_image").val(); $ .post("process/process_add_user.php",{first_name:data_first_name,last_name:data_last_name,email:data_email,role:data_role,password:data_password,user_name:data_user_name,profile_image:data_profile_image},function(resp){$("#first_name_error").html(resp); });});});

2 回答

  • 0

    要在网页上以文本形式输出AJAX响应,请先在HTML中创建一个新的 DIV

    <div id="response"></div>
    

    并使用以下脚本将Ajax文本响应写入 DIV . 您需要使用函数调用它 .

    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("response").innerHTML=xmlhttp.responseText;
    }
    

    有关Ajax响应的更多信息http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp

  • 0

    你可以使用response code . 代码应该代表实际的错误 .

    当ajax返回时,它返回一串错误消息 . 如何分离错误消息?

    您可以将错误消息集合作为JSON字符串发送,并在错误处理函数中根据需要处理这些错误 .

    在出现错误的情况下:

    <?php
    http_response_code(400);
    // maps a form field to an error message.
    $a = array('username' => 'no username', 'email' => 'no email');
    echo json_encode($a);
    

    在正常情况下:

    <?php
    http_response_code(200);
    echo "Success message";
    

    当您可以为各个状态代码设置事件处理程序甚至只是 successfail 回调时,此方案适用于许多ajax库 .

    jQuery的一个例子:

    $.ajax({
      statusCode: {
        400: function(jqXHR, textStatus, errorThrown) {
            var errors = JSON.parse(errorThrown);
            if (errors.username) {
                $('#username-error').text(errors.username);
            }
        },
        200: function(data) {
            // ...
        }
      }
    });
    

    使用vanilla JavaScript的示例:

    var handlers = {
       400: function (responseText) {
           var errors = JSON.parse(responseText);
           if (errors.username) {
               var name_error = document.querySelector('#username-error');
               name_error.textContent = errors.username;
           }
       },
       200: function (responseText) { /* ... */ }
    };
    
    function ajaxCall(url, handlers) {
        var r = new XMLHttpRequest(); 
        r.open("POST", url, true);
        r.onreadystatechange = function () {
            if (r.readyState === 4 && typeof handers[r.status] === 'function') {
                handers[r.status](r.responseText);
            }
        };
        r.send(/* ... */);
    }
    
    ajaxCall('userform_validation.php', handlers);
    

相关问题