存储在数据库中的空值--Ajax Forms

时间:2016-04-28 13:51:17

标签: javascript php jquery ajax ajaxform

我正在使用AJAX构建表单并遇到问题:

每当我提交表单时,存储在数据库中的值都会变为空白...(仅供参考,当然页面没有刷新......这就是重点。)

Blank Values

这是我的文件 index.php

<!doctype html>
<html>
<head>
    <title>Form Practice</title>
</head>
<body>
    <div id="myForm">
        Name: <input name="username" type="radio" value="Sagar">Sagar</input>
              <input name="username" type="radio" value="Saransh">Saransh</input><br /><br />
        Profession: <input name="profession" type="radio" value="Coder">Coder</input>
                    <input name="profession" type="radio" value="Entrepreneur">Entrepreneur</input>
                    <input name="profession" type="radio" value="Blogger">Blogger</input><br /><br />
        <input type="submit" id="submit" value="Submit"></input>
    </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $.ajax({
        url: "data.php",
        type: "POST",
        async: false,
        data:{
            "username":$('input[name=username]:checked', '#myForm').val(),
            "profession": $('input[name=profession]:checked', '#myForm').val()
        }
    });
</script>
</body>
</html>

我的 Data.php

<?php 

require 'connect.php';

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
    die('cant use'. DB_NAME . ':'. mysql_error());
}

$username = $_POST['username'];
$profession = $_POST['profession'];

$sql = mysql_query("INSERT INTO info (Name, Profession) VALUES('{$username}', '{$profession}')");

if(!mysql_query($sql)){
    die('Some Error '. mysql_error());
}

mysql_close();
?>
<!doctype html>
<html>
<head>
    <title>Data</title>
</head>
<body>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

试试这个

{{1}}

答案 1 :(得分:0)

使用此

进行编辑
 <script>    
        $(document).ready(function(){
            $('#submit').on('click', function(){
               var username=$('input[name="username"]:checked').val();
               var profession=$('input[name="profession"]:checked').val();
                $.ajax({
                    type: 'POST',
                    async: false,
                    url: 'data.php',
                    data:"username=" + username+ "&profession="+ profession, 
                    success: function(response)
                    {       

                    }
                }).error(function(request, status, error){
                     console.log(e);
                });
         });

        });
    </script>