到目前为止,我已经使用以下代码(在相关部分中)作为我的后端php用于ajax运行webform。它运作得很好。
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=\"✘\" title=\"✘\">";
} else {
$report_groupName['errorText_groupName'] = NULL;
$report_groupName['resultImg_groupName'] = "<img src=\"./gfx/form_yay.gif\" class=\"resultImg\" alt=\"✔\" title=\"✔\">";
}
echo json_encode($report_groupName);
}
它可以很好地保存用户放置在groupName文本字段中的任何内容。如果字段为空,它也很好地将mysql字段返回NULL。这就是我想要的。
然而,为了实现这一点,我一直在使用:
$ident = $_GET['ident'];
值,从网址传递。我想摆脱这个并使系统完全依赖发布的数据并保持网址清洁。所以我改成了这个:
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=\"✘\" title=\"✘\">";
} else {
$report_groupName['errorText_groupName'] = NULL;
$report_groupName['resultImg_groupName'] = "<img src=\"./gfx/form_yay.gif\" class=\"resultImg\" alt=\"✔\" title=\"✔\">";
}
echo json_encode($report_groupName);
}
这几乎也适用。如果有人输入文本,它将正常运行。如果他们进入一个空格,它将按原样返回NULL。但如果他们清空文本字段并且什么都不返回,则没有任何反应。之前的值保持不变。我不明白为什么会这样,并试图让它作为一个未改变的帖子工作(删除$ userInput = ucwords($ _ POST ['groupName'])周围的ucwords();但它没有任何区别.Firebug告诉我认为“groupName”确实是以零值发送的,那么为什么不会像早期版本那样选择它并采取相应的行为呢?
答案 0 :(得分:0)
尝试这样,
$userInput = trim($_POST['groupName']);
答案 1 :(得分:0)
因为不会满足if($_POST['groupName'])
。
您可以尝试使用if (isset($_POST['groupName']))
代替
我也会替换
if(($userInput == "") || ($userInput == " ") || ($userInput == NULL)) { $userInput = NULL; }
与
if (empty(trim($userInput))) {
$userInput = null;
}