php - 无法使用php jquery和ajax在mysql中输入数据

标签 php jquery mysql ajax

我的错误 friend 们...下面是完整的代码 我正在编写一个 jquery php 基础寄存器表单代码。看起来代码工作正常,但我无法在 mysql 数据库中输入数据。请让我知道我哪里出错了。表格如下:

        <!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" type="text/css" media="all" href="home_style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>



<script>
   $(document).ready(function(){
      $("#ajax-contact-form").submit(function(){
      var str = $(this).serialize();
          $.ajax(
         {
         type: "POST",
        url:"contact.php",
        data: str,
        success:function(result)
                {
                $("#div1").html(result);
                }
         });
     return false;
     });
 });
</script>


</head>
<body>

<div id="contact_form">  
<form id="ajax-contact-form" name="contact" action=""> 
<fieldset>
    <div class="field_container">First Name:</label>
<input type="text" name="cust_firstname" id="firstname"  maxlength="50" onblur="fnamevalidation()" style="width: 250px; height: 30px"; />



<div class="field_container">Last Name:</label>
<input type="text" name="cust_lastname" id="lastname" maxlength="50" onblur="lnamevalidation()" style="width: 250px; height: 30px"; />


<div class="sex_check" >
<input type="radio" name="cust_sex" type="radio" value="M" /> Male
<input type="radio" name="cust_sex" type="radio" value="F" /> Female


<hr class="line_break">

<div class="field_container">Email:</label>
<input type="text" name="cust_email" id="email"  maxlength="100" onblur="emvalidation()" style="width: 250px; height: 30px"; />



<div class="field_container">Password:</label>
<input type='password' name='cust_password' id='password'  maxlength="12" onblur="pwordvalidation()" style="width: 250px; height: 30px"; />



<div class="field_container">Confirm Password:</label>
<input type='password' name='cust_password2' id='confirmpassword'  maxlength="12" onblur="cpwordvalidation()" style="width: 250px; height: 30px"; />




<INPUT class="button" type="submit" name="submit" value="Register">

 </fieldset>  
</form>
</div> 

<div id="div1">
</div> 


</body>
</html>

而 contact.php 是

<?php
ob_start();

    session_start();

    require_once("config.php");




    $cust_firstname = stripslashes($_POST['cust_firstname']);

        $cust_lastname=stripslashes($_POST['cust_lastname']);

        $cust_sex=$_POST['cust_sex'];

    $cust_email=stripslashes($_POST['cust_email']);

    $cust_password=stripslashes($_POST['cust_password']);

    $cust_password2=stripslashes($_POST['cust_password2']);






// Get values from form



    if(isset($_POST['Submit'])) 

{

    // Insert data into mysql 

        $res = mysql_query("SELECT * FROM register WHERE cust_email = '$cust_email'");

            if (mysql_num_rows($res)>0)

                    {

                    echo 'That email is already registered';

                    exit;

                    }

            mysql_query("INSERT INTO register (`cust_firstname`,`cust_lastname`, `cust_sex`, `cust_email`, `cust_password`) 

            VALUES

            ('$cust_firstname', '$cust_lastname', '$cust_sex', '$cust_email', '$cust_password')");

            $_SESSION['valid_user'] = $cust_email;


    }




    echo $cust_firstname;

    echo $cust_lastname;

    echo $cust_sex;

    echo $cust_email;

    echo $cust_password;

    echo $cust_password2;



    ?>

最佳答案

您应该删除 PHP 文件中的 if(isset($_POST['Submit'])) 代码。其一,您的按钮名称是“提交”(带有小写的 s),其二,该变量不会发送,因此它永远不会为真。那么它应该可以工作。但我建议查看您的 HTML 标记。这是完全无效的。

关于php - 无法使用php jquery和ajax在mysql中输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18160931/

相关文章:

php - 获取Youtube视频标题,说明和缩略图时出错

PHP - 通过对象数组中的键查找对象,更新其值

php - PDO 查询返回结果,但我无法获取它

javascript - 如何获取和设置 Bootstrap 多选下拉列表值?

javascript - 如何在多步 javascript 表单中添加单选按钮、复选框和选择字段?

mysql - 如何修复 'Selecting rows without null or blank value in each column'?

mysql - 获取特定类别的所有帖子

php - 如何在 PHP 中返回文件

javascript - 回调函数 promise ajax

php - mysql中如何插入多个字段