ajax数据发布期间php行为的异常更改


unusual alteration in php behaviour during post of ajax data

到目前为止,我一直在使用以下代码(在相关部分)作为ajax运行的web表单的后端php。它工作得很好。

if(($ident) == "groupName") {
    $userInput = ucwords($_POST['groupName']);
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `groupName` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
    $report_groupName = array();
    if($userInput == NULL) {
        $report_groupName['errorText_groupName'] = "This field cannot be left blank";
        $report_groupName['resultImg_groupName'] = "<img src='"./gfx/form_boo.gif'" class='"resultImg'" alt='"&#10008;'" title='"&#10008;'">";
    } else {
        $report_groupName['errorText_groupName'] = NULL;
        $report_groupName['resultImg_groupName'] = "<img src='"./gfx/form_yay.gif'" class='"resultImg'" alt='"&#10004;'" title='"&#10004;'">";
    }
    echo json_encode($report_groupName);
}

它很好地保存了用户放入groupName文本字段中的任何内容。如果mysql字段为空,它也会很好地将该字段返回为NULL。这就是我想要它做的。

然而,为了让它发挥作用,我一直在使用:

$ident = $_GET['ident'];

值,从url传递。我想摆脱这种情况,让系统完全依赖发布的数据,并保持url的干净。所以我把它改成了这个:

if($_POST['groupName']) {
    $userInput = ucwords($_POST['groupName']);
    if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
    try {
        $stmt = $conn->prepare("UPDATE $database.app_$applicationKey SET `groupName` = :userinput, `lastModified` = :time WHERE `appID` = :appid");
        $stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
        $stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
        $stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
        $stmt->execute();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
    $report_groupName = array();
    if($userInput == NULL) {
        $report_groupName['errorText_groupName'] = "This field cannot be left blank";
        $report_groupName['resultImg_groupName'] = "<img src='"./gfx/form_boo.gif'" class='"resultImg'" alt='"&#10008;'" title='"&#10008;'">";
    } else {
        $report_groupName['errorText_groupName'] = NULL;
        $report_groupName['resultImg_groupName'] = "<img src='"./gfx/form_yay.gif'" class='"resultImg'" alt='"&#10004;'" title='"&#10004;'">";
    }
    echo json_encode($report_groupName);
}

这也很有效——差不多。如果有人输入文本,它会正常工作。如果他们在空格中输入,它会按照预期返回NULL。但如果他们清空文本字段,什么也不返回,什么也不会发生。上一个值保持不变。我不明白为什么会这样,我试着让它作为一个不变的帖子工作(删除$userInput=ucwords($_post['groupName'])周围的ucwords);但这并没有什么不同。Firebug告诉我,"groupName"确实是用一个nil值发送的,那么为什么它不会像早期版本那样接收它并相应地执行呢?

因为if($_POST['groupName'])不会被满足。

你可以试着用if (isset($_POST['groupName']))代替

我也会更换

if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }

带有

if (empty(trim($userInput))) {
   $userInput = null;
}

这样尝试,

 $userInput = trim($_POST['groupName']);