jQuery 驗證遠端方法來檢查使用者名稱可用性
使用jQuery Validate,您可以使用遠端驗證資料庫中是否已存在使用者名稱方法。這是一個範例:
jQuery:
$("#signupForm").validate({ rules: { username: { required: true, minlength: 3, remote: { url: "check-username.php", type: "post" } } }, messages: { username: { remote: "This username is already taken! Try another." } } });
check-username.php:
<?php header('Content-type: application/json'); $username = $_REQUEST['username']; $query = mysql_query("SELECT * FROM mmh_user_info WHERE username ='$username'"); $result = mysql_num_rows($query); if ($result == 0) { $valid = 'true'; } else { $valid = 'false'; } echo $valid; ?>
check-username.php:
$check_for_username = mysql_query("SELECT username FROM mmh_user_info WHERE username='$name'");
$stmt = $mysqli->prepare("SELECT username FROM mmh_user_info WHERE username=?"); $stmt->bind_param("s", $name);
execute()) { $stmt->store_result(); if ($stmt->num_rows() > 0) { $output = true; } else { $output = false; } } else { $output = false; } $stmt->close(); $mysqli->close(); echo json_encode($output); ?>
以上是如何使用 jQuery Validate 的遠端方法檢查使用者名稱可用性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!