为什么在数据库中插入null?

时间:2013-07-15 22:34:43

标签: php jquery ajax database

我正在尝试创建一个用户正在选择一个选项的表单让我们说他会选择他的名字我使用ajax来调用另一个页面,在这个页面我创建连接并插入答案它插入的问题null 码 的index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyDataBase </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">   </script>

 $(document).ready(function(){
$("#mysubmit").click( function (){
    $.ajax({
        url:"Final.php",
        type:"POST",
        success: function(success_array)
        {   
            alert(" Well Done ");
        },
        error: function(xhr, ajaxOptions, thrownError)
        {
            alert( " Didn't work ! ");
        }
    });
 });
   });

 </script>  
  </head>
  <form method="post" id = "myform">
  <input type="radio" name="kname" value="X1" />X1<br  />
  <input type="radio" name="kname" value="Y1" />Y1<br  />
  <input type="radio" name="kname" value="A1" />A1<br  />
  <input type="radio" name="kname" value="G1" />G1<br  />
  <input type="submit" onclick="save()"  id="mysubmit"/>
  </form>
  <body>
  </body>
  </html>

Final.php

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Thanks</title>
        </head>

        <body>
        <?php
        $con = mysqli_connect("localhost","root","xyz","test");
        mysqli_query($con,"INSERT INTO name values ('$_POST[kname]')");
        ?>
        </body>
        </html>

2 个答案:

答案 0 :(得分:0)

您必须通过$_POST['kname']引用kname。

但请!永远逃避你的价值观!

答案 1 :(得分:0)

通过ajax提交/发布时,您需要添加数据。最简单的是serialize()

 var form = $('#myform');
 $.ajax({
    url:"Final.php",
    type:"POST",
    data: form.serialize(),
    ...

或者你可以得到一个字段 -

   data: {kname: $('#id_of_your_radio').val()},

确保在插入数据库之前清理用户数据。