gpt4 book ai didi

javascript - 根据条件检查数据库结果

转载 作者:行者123 更新时间:2023-11-29 07:17:45 25 4
gpt4 key购买 nike

我的问题如下。我想要 javascript 检查数据库中是否存在某个值。我有一个 javascript 函数,它检查数据库中是否存在电子邮件地址。那个工作正常,所以我复制了它并更改它以匹配不同的输入字段,这应该检查值(woonplaats)是否存在。现在,它总是显示消息“plaatsnaam bestaat niet”,即使它确实存在。 HTML、PHP 代码以及检查数据库的 SQL 和 javascript 都在同一个 PHP 文件中。我知道 PHP 检查 echo,但我只想让 javascript 执行检查。

在我的代码下面:

HTML:

<form action="" onSubmit="return !!checkMailStatus() & checkCityStatus();" method="post" id="registration">
<input type="email" name="email" id="email" placeholder="E-mailadres" required/>
<input type="text" name="keyword" id="keyword" placeholder="Woonplaats" value="">
<button type="submit" name="register" class="btn-red">Registreren!</button>
</form>

PHP:

if(isset($_POST['register'])) {

$email = $_POST['email'];
$keyword = $_POST['keyword'];

$check_d = $db->query("SELECT id FROM users WHERE email='".$email."'");
$check_d = $check_d->num_rows;
if($check_d == 1) {
echo 'E-mailadres bestaat al';
}

$check_pnl = ("SELECT woonplaats
FROM `locatie`
WHERE woonplaats='".$keyword."'");
$check_pnl = $check_pnl->num_rows;
if($check_pnl == 0) {
echo 'Plaatsnaam Bestaat Niet';
}

Javascript:

<script>
function checkMailStatus(){
//alert("came");
var email=$("#email").val();
$.ajax({
type:'post',
url:'',
data:{email: email},
success:function(msg){
alert('Gebruik een ander e-mail adres');
return false; //prevent submit from submitting
}

});
}
</script>
<script>
function checkCityStatus(){
//alert("came");
var keyword=$("#keyword").val();
$.ajax({
type:'post',
url:'',
data:{keyword: keyword},
success:function(msg){
alert('Plaatsnaam bestaat niet');
return false; //prevent submit from submitting
}

});
}
</script>

根据 Pioz 和 RAJDEEP PAUL 的 AWNSERS,我更改了以下内容:

在index.php中我将javascript代码更改为:

<script>

var regForm = $("#registration");

regForm.submit(function(evt){
// PREVENT FORM FROM DEFAULT BEHAVIOUR: SUBMITTING...
evt.preventDefault();

// IF BOTH CONDITIONS ARE SATISFIED... SUBMIT THE FORM MANUALLY...
if(checkMailStatus() && checkCityStatus()){
// SHIP THE FORM
$(this).submit();
}

});


function checkMailStatus(){
var email = $("#email").val();
var returnVal = 0;
$.ajax({
type:'post',
url:'check.php',
data:{email: email, checkMail: true},

success:function(msg){
if(msg == "0"){
alert('Gebruik een ander e-mail adres');
}else{
returnVal = 1;
}
}

});
return returnVal;
}

function checkCityStatus(){
var keyword = $("#keyword").val();
var returnVal = 0;
$.ajax({
type:'post',
url:'check.php',
data:{keyword: keyword, checkCity: true},

success:function(msg){
if(msg == "0"){
alert('Plaatsnaam bestaat niet');
}else{
returnVal = 1;
}
}
});
return returnVal;
}
</script>

我还创建了 CHECK.PHP:

if($_POST['checkMail'] == "true"){ 
$check_d = $db->query("SELECT id FROM users WHERE email='".$email."'");
$check_d = $check_d->num_rows;
if($check_d == 0) {
echo "0";
}else{
// ECHO A FLAG TO SHOW THAT EVERYTHING WORKS FINE: ZERO ISSUE WAS FOUND
echo "1";
}
}

if($_POST['checkCity'] == "true"){
$check_pnl = ("SELECT woonplaats
FROM `locatie`
WHERE woonplaats='".$keyword."'");
$check_pnl = $check_pnl->num_rows;
if($check_pnl == 0) {
echo "1";
}else{
// ECHO A FLAG TO SHOW THAT EVERYTHING WORKS FINE: ZERO ISSUE WAS FOUND
echo "0";
}
}

最佳答案

您的代码中需要更改一些内容,例如:

  • 正如我在 my comment 中所说,您必须将 AJAX 请求发送到不同的页面并在那里进行处理。因此,创建一个 check.php 页面来处理您的 AJAX 请求。
  • 不要创建两个不同的函数,即 checkMailStatus()checkCityStatus(),而只使用一个函数,例如 checkStatus() 函数验证您的输入字段。
  • dataType 设置添加到您的 AJAX 请求中,并发送 json 对象作为服务器的响应。 dataType 是您期望服务器响应的数据类型。
  • 使用prepared statements ,它将防止您的数据库受到任何类型的 SQL 注入(inject)攻击。 See this SO Q/A .

我假设 E-mailadresWoonplaats 都是必填字段。

因此,在 index.php 页面上,您的 HTMLjQuery 脚本应如下所示:

HTML:

<form action="" onSubmit="return checkStatus(this);" method="post" id="registration">
<input type="email" name="email" id="email" placeholder="E-mailadres" value="" required>
<input type="text" name="keyword" id="keyword" placeholder="Woonplaats" value="" required>
<button type="submit" name="register" class="btn-red">Registreren!</button>
</form>

jQuery:

<script>
function checkStatus(f){
var email=$("#email").val();
var keyword=$("#keyword").val();

$.ajax({
type:'post',
url:'check.php',
data:{email: email, keyword: keyword},
dataType: 'json',
success:function(data){
if(data.status == "success"){
f.submit();
}else{
alert(data.msg);
}
}
});
return false;
}
</script>

check.php 页面上,按如下方式处理您的 AJAX 请求:

<?php

// Your connection code
$conn = new mysqli("localhost", "USERNAME", "PASSWORD", "DATABASE_NAME");

if(isset($_POST['email'], $_POST['keyword'])){
$email = $_POST['email'];
$keyword = $_POST['keyword'];

$stmt = $conn->prepare("SELECT id FROM users WHERE email=?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows == 0){

$stmt = $conn->prepare("SELECT woonplaats FROM `locatie` WHERE woonplaats=?");
$stmt->bind_param("s", $keyword);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows){
echo json_encode(array('status' => 'success', 'msg' => 'no error'));
}else{
echo json_encode(array('status' => 'error', 'msg' => 'Place name does not exist'));
}

}else{
echo json_encode(array('status' => 'error', 'msg' => 'Use a different email address'));
}
}else{
echo json_encode(array('status' => 'error', 'msg' => 'missing fields'));
}

?>
<小时/>

不要忘记更改此行 $conn = new mysqli("localhost", "USERNAME", "PASSWORD", "DATABASE_NAME"); check.php页面。

关于javascript - 根据条件检查数据库结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com