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 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来