我在 php 中創(chuàng)建了一個(gè)簡單的注冊(cè)表單并對(duì)用戶輸入進(jìn)行了驗(yàn)證。我給每個(gè)$error[]數(shù)組一個(gè)字符串索引。在示例中,它是這樣的:$error['fn']我這樣做是因?yàn)槲蚁胧褂?Ajax json 數(shù)據(jù)類型在用戶輸入旁邊/下方顯示每個(gè)驗(yàn)證錯(cuò)誤。但由于某種原因,只有一個(gè)數(shù)組輸出顯示,而另一個(gè)輸出則不顯示。它應(yīng)該顯示用戶名輸入下方的其他輸出。如何顯示其他輸出以及我在哪里犯了錯(cuò)誤?<?phprequire('../includes/config.php');if(isset($_POST['fullname'])){ //fullname validation $fullname = $_POST['fullname']; if (empty($_POST['fullname'])) { $error['fn'] = "Please fill this field"; echo json_encode($error); } if (! $user->isValidFullname($fullname)){ $error['fn'] = 'Your name must be alphabetical characters'; echo json_encode($error); } }if(isset($_POST['username'])){ //username validation $username = $_POST['username']; if (empty($_POST['username'])) { $error['un'] = "Please fill this field"; echo json_encode($error); } if (! $user->isValidUsername($username)){ $error['un'] = 'Your username must be at least 3 alphanumeric characters'; echo json_encode($error); } if (! $user->isUsernameAlreadyinUse($username)){ $error['un'] = 'This username already in use'; echo json_encode($error); } }?><script type="text/javascript"> $(document).ready(function() { $("#register-form").on("submit", function(e) { e.preventDefault(); var fullname = $("#fullname").val(); var username = $("#username").val(); $.ajax({ type: "POST", url: "registercontrol.php", data: { fullname: fullname, username: username }, dataType: "json", success: function(result) { $("#vfullname").html(result['fn']); $("#vusername").html(result['un']); } }); }); });</script>謝謝。
1 回答

慕碼人8056858
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
成功后功能請(qǐng)?zhí)砑?/p>
data = JSON.parse(result);
$("#vfullname").html(data.fn);
$("#vusername").html(data.un);
- 1 回答
- 0 關(guān)注
- 147 瀏覽
添加回答
舉報(bào)
0/150
提交
取消