dsfb20227 2017-12-01 12:11 采纳率: 0%
浏览 53
已采纳

Ajax表单验证Php插入查询

i'm trying to add data to database with jquery. My code is here, but its not working.

Using bootstrap form, with that form i'm trying to send input datas to specific page (in this situation, adduser.php) in that page, i'm trying to check this values to database for be sure there is no same data (i'm checking email adresses) Can you help me guys?

<script>
$(document).ready(function(){
    $('#adduser').click(function(){

        var add_name = $('#add_name').val();
        var add_surname = $('#add_surname').val();
        var add_email = $('#add_email').val();
        var add_password = $('#add_password').val();

        if(add_name == '' || add_surname == '' || add_email == '' || add_password == '' ){
            $('#add_user_error').html("<strong class='text-danger'>*** Please enter all details</strong>");
        }else{      

            $.ajax({
                url: "adduser.php",
                method: "post",
                data:{add_name:add_name,add_surname:add_surname,add_email:add_email,add_password:add_password},
                success: function(data){
                    if (data == 1) {
                $('#add_user_error').html("<strong class='text-danger'>This email have in database</strong>");              
            }else{
                $('#add_user_error').html("<strong class='text-success'>Success</strong>"); 
            }

            }               

                }); return false;       
        }
    });
});
</script>


<?php  
include ('setup.php'); //Database connection dbc


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

    $add_name = $_POST['add_name'];
    $add_surname = $_POST['add_surname'];
    $add_email = $_POST['add_email'];
    $add_password = $_POST['password'];


$q = "SELECT * FROM users WHERE email = '$add_email'";
$r = mysqli_query($dbc, $q);

$adduser = mysqli_fetch_assoc($r);


if($adduser['email'] !== $add_email){
    $q = "INSERT INTO users (name,surname,email,password) VALUES ('$add_name','$add_surname','$add_email','$add_password')";
    $r = mysqli_query($dbc, $q);    
}
}
?>  
  • 写回答

2条回答 默认 最新

  • douerqu2319 2017-12-01 12:22
    关注

    There could be any other mistake too but I think you need to send adduser from ajax. Then only $_POST['adduser'] will be true (it is false now, as it isn't set)

    Replace your data line with line below and try

    data:{add_name:add_name,add_surname:add_surname,add_email:add_email,add_password:add_password,adduser:1}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵