doujiao7483 2012-10-30 15:45 采纳率: 0%
浏览 42
已采纳

jQuery AJAX返回变量+值可能吗?

I'm submitting a form via jQuery.ajax()

Now my PHP script is checking if a specific input field is empty, example:

$is_error = $user->is_error;

if($is_error !=0)
 { 
    echo $is_error;
 }

Back to my jQuery.ajax() , I'd like to check if the value of $error was true or not, within the sucess: part of the jQuery.ajax() call.

   jQuery.ajax({
           type: "POST",
           url: "edit.php", 
           data: jQuery("#idForm").serialize(),
           success: function(data)
       {   

        // show response from the php script if there is an error message
        // like:
        //         if(is_error) {show specific error message}
        //         else {show everything positive message}

       }
    });

Is it possible to check the PHP variable's value in there? Like if/else ?

Best regards!

  • 写回答

5条回答 默认 最新

  • duanchi5078 2012-10-30 15:47
    关注
    if($_POST['name'] == "") 
      {
        $error = 1;
      }
    else 
      {
        $error = 0;
      }
    echo $error;
    

    This code will echo the value.

     jQuery.ajax({
               type: "POST",
               url: "edit.php", 
               data: jQuery("#idForm").serialize(),
               success: function(data)
           {   
    
            // show response from the php script if $error == 0 or $error == 1.
            if(data==1)
    ....
           }
        });
    

    Then you check what is the returned value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错