0

我正在尝试创建一个用户正在选择一个选项的表单假设他将选择他的名字我使用 ajax 来调用另一个页面,在这个页面中我创建连接并插入答案它正在插入空代码 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>

最终的.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>
4

2 回答 2

0

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

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

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

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

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

于 2013-07-15T23:03:44.973 回答
0

您必须通过$_POST['kname'].

但是,求求您!永远逃避你的价值观!

于 2013-07-15T22:37:03.087 回答