语句返回一个空字符串

时间:2014-10-09 09:23:23

标签: php jquery ajax

我很困惑为什么当我对我的php脚本进行ajax调用时,它会返回一个空字符串。我相信我在php中检查这个,如果为空则应该触发notif消息。即使字符串返回空,它也会不断触发成功消息。在firebug的post部分中,我可以看到所有输入都使用正确的值发布,如果我提醒序列化数据,那么也正确地发布。

如何检查php是否正在接收数据?我尝试过print_r,var_dump,但控制台中没有显示任何内容。如果有人能指出我的错误,我将不胜感激。非常感谢

的jQuery

$(function() {

    $.validator.setDefaults({
        errorClass: 'form_error',
        errorElement: 'span',
        ignore: ":hidden:not(select)",
        errorPlacement: function(error, element) {
            error.appendTo(element.siblings('span'));
        }
    });

$("#USRrecycle").validate({
rules: {
    rcyrequested: {
        required: true
    },
    rcyservice: {
        required: true
    },
    rcydept: {
        required: true
    },
    rcyaddress: {
        required: true
    },
    rcyqty: {
        required: true,
        number: true
    },
    rcydatepicker: {
        required: true,
        date: true
    }

},
messages: {
    rcyrequested: {
        required: '* required: You must enter your name'
    },
    rcydept: {
        required: "* required: You must select a department"
    },
    rcyaddress: {
        required: "* required: You must select a customer address"
    },
    rcyservice: {
        required: "* required: You must select a service level"
    },
    rcyqty: {
        required: "* required: You must enter a quantity",
        number: "* Quantity amount must be a number. ie, 200"
    },
    rcydatepicker: {
        required: "* required: You must enter a delivery recycle date",
        date: "* You must enter a date only in this format: 07/10/2014"
    }
},

submitHandler: function() {

            if ($("#USRrecycle").valid() === true) {
                var data = $("#USRrecycle").serialize();
                alert(data);
                $.ajax({
                    type: "POST",
                    url: "rcy.php",
                    data: data,
                    dataType: 'json',
                    success: function(data) {


                        if (data.opp == 'error') {
                         console.log(data.qty);

                            notif({
                                msg: '<br />ERROR! ' + '<b><font color="red">' + data.qty + '</font></b>' + ' was NOT SUBMITTED.<br /><br /> You must enter an amout.<br /><br />' + 'Please correct this error and try again.' + 'Thank you.',
                                type: "boxDstrError",
                                position: "center",
                                width: 490,
                                height: 75,
                                multiline: true,
                                timeout: 8000,
                                opacity: 0.8,
                                fade: 10,
                            });
                        } else {
                            console.log(data.qty);
                            $("#USRrecycle").get(0).reset();
                            $("#rcydept").trigger("chosen:updated");
                            $("#rcyaddress").trigger("chosen:updated");

                            notif({
                                msg: "Your submission was successfull."  + '<br /><br />' + '<b><font color=\"black\">' + data.qty + '</font></b><br /><br />' + ' This entry was successfully submitted to the database.<br />Thank you.',
                                type: "boxdstrSuccess",
                                position: "center",
                                width: 490,
                                height: 75,
                                multiline: true,
                                timeout: 8000,
                                opacity: 0.8,
                                fade: 10,
                            });
                          }
                       }
                   });
                }
            }

       });
});

php - rcy.php

<?php
    session_start();

  // test vars from jquery form
  $status = mysql_real_escape_string($_POST['status']);
  $company = mysql_real_escape_string($_SESSION['kt_idcode_usr']);
  $requested = mysql_real_escape_string($_POST['rcyrequested']);
  $activity = mysql_real_escape_string($_POST['rcyactivity']);
  $address = mysql_real_escape_string($_POST['rcyaddress']);
  $service = mysql_real_escape_string($_POST['rcyservice']);
  $date = explode('/', $_POST['rcydatepicker']);
  $newdate = $date[2].'-'.$date[1].'-'.$date[0];
  $authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
  $dept = mysql_real_escape_string($_POST['rcydept']);

    $array = array();
    $array = $_POST['rcyqty'];

    $outString = ''; 


    foreach ($array as $qty) {
        $qty = mysql_real_escape_string($qty);

        // If $qty is empty, send output to outstring to display notif error msg
        if ($qty == " ") {

            $outString .= $qty . ' ';

    }   
    }
    if ($outString) { // if there are numbers in this string, then there are error Duplicates
        $error = array('opp' => "error", 'qty' => $outString); // qty will have the TEXT for notif msg
    // IMPORTANT, have changed JSON output to use an Opperation (opp) to indicate
    // in javascript if there was an error OR not
        $output = json_encode($error);
        echo $output;
        exit();
    }


    // Will Only execute the SUCCESS if $outString is empty (no exit();)

    foreach ($array as $qty) {
        $outString .= $qty . ' ';

        $qty = mysql_real_escape_string($qty);

    }

    $json = array('opp' => 'insert', 'qty' => $outString);
    $result = json_encode($json);
    echo $result;
?>

HTML

<div class="fieldset">
   <h1><span>Enter Quantity Reqd</span></h1>
      <p>
         <input id="rcyqty" name="rcyqty" type="text" required="required" style="width:250px; height:30px;" /><span></span>
         <a href="javascript:void(0)" class="removeclass"></a>
         <a style="margin-left: 14px;" href="javascript:void(0)" class="tooltip" title="Please enter the quantity of boxes/sacks that you require.">Help</a>
      </p>
</div>

0 个答案:

没有答案