dongrandi8411 2015-08-20 13:06
浏览 63

连接json输出响应

I am using a AJAX /php form .

if (strlen($name) < 4) { // If length is less than 4 it will output JSON error.
    $output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"Naam" field contains invalid value')));
    die($output);
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { //email validation
    $output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"E-mail" field contains invalid value')));
    die($output);
}
if (strlen($subject) < 3) { //check emtpy message
    $output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"Onderwerp" field contains invalid value')));
    die($output);
}
if (strlen($message) < 3) { //check emtpy message
    $output = json_encode(array('type' => 'error', 'text' => '<br /> - ' . _t('"Vraag" field contains invalid value')));
    die($output);
}

The AJAX script :

$.post('contact_form', post_data, function (response) {
    if (response.type == 'error') { //load json data from server and output message     
        output = '<div class="error">' + response.text + '</div>';
    } else {
        output = '<div class="success">' + response.text + '</div>';
        //reset values in all input fields
        $("#contact_form  input[required=true], #contact_form textarea[required=true]").val('');
        $("#contact_form #contact_body").slideUp(); //hide form after success
    }
    $("#contact_form #contact_results").hide().html(output).slideDown();
}, 'json');

The above code it valides form and output if is there any error. My issue is that i receive 1 error only. How can i receive all the errors in a json object ?

  • 写回答

1条回答 默认 最新

  • doumian3780 2015-08-20 13:18
    关注

    Somehting like this will give all errors. It pushes every error into an array and returns the whole array instead of exiting after one error

    $output = array();
    
    if (strlen($name) < 4) { // If length is less than 4 it will output JSON error.
        $output[] = array('type' => 'error', 'text' => '<br /> - ' . _t('"Naam" field contains invalid value'));
    }
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { //email validation
        $output = array('type' => 'error', 'text' => '<br /> - ' . _t('"E-mail" field contains invalid value'));
    }
    if (strlen($subject) < 3) { //check emtpy message
        $output[] = (array('type' => 'error', 'text' => '<br /> - ' . _t('"Onderwerp" field contains invalid value'));
    }
    if (strlen($message) < 3) { //check emtpy message
        $output[] = array('type' => 'error', 'text' => '<br /> - ' . _t('"Vraag" field contains invalid value'));
    }
    
    if (count($output)) die(json_encode($output));
    

    You will also have to iterate through these errors and show them all in your JS

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。