dongxing9219 2018-06-09 15:12
浏览 37

使用php mysql和ajax不会将值提交到数据库表

I am using php, MySQL, and Ajax. When I submit the form data is not stored in the database table. But when put action="insert.php" data is submitted to the database table but the page gets refreshed. I am badly stuck here. Can anyone please advice me that how can I solve this problem?? Thanks in advance guys. codes are given below :

index.php

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        <form id="cForm" name="cForm" method="post">
            <label>Roll No : </label>
            <input type="text" name="roll" id="roll"><br>
            <label>Name : </label>
            <input type="text" name="name" id="name"><br>
            <label>Stream : </label>
            <select name="stream" id="stream">
                <option value="CSE">CSE</option>
                <option value="IT">IT</option>
                <option value="ECE">ECE</option>
                <option value="ME">ME</option>
            </select><br>
            <label>Age : </label>
            <input type="text" name="age" id="age"><br>
            <input type="submit" name="submit" value="Submit">
        </form>
        <script type="text/javascript">
    $(document).ready(function(){
        $('#cForm').on('submit', function(e){

            e.preventDefault();
            $.ajax({
                url:"insert.php",
                method:"POST",
                data:$('#cForm').serialize(),
                success:function(data)
                {
                    if(data == 'ok')
                    {
                        document.getElementById("cForm").reset();
                    }   
                }
            });
        });
    });
</script>
    </div>
</body>
</html>

insert.php

    <?php 
$con=mysqli_connect("localhost","root","","test");
if(isset($_POST['submit'])){
    $roll=$_POST['roll'];
    $name=$_POST['name'];
    $stream=$_POST['stream'];
    $age=$_POST['age'];
    $sql="INSERT INTO `student`(`name`, `stream`, `age`) VALUES ('$name','$stream','$age')";
    $result=mysqli_query($con,$sql);
    if(isset($result))
     {
      echo 'ok';
     }
}
 ?>
  • 写回答

1条回答 默认 最新

  • dongzhong1891 2018-06-09 15:35
    关注

    If you want your page doest not refresh. you need to remove form submission form jQuery because you have called on submit and it called the whole form and send it to next page and do it like this

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container">
            <form id="cForm" name="cForm" method="post">
                <label>Roll No : </label>
                <input type="text" name="roll" id="roll"><br>
                <label>Name : </label>
                <input type="text" name="name" id="name"><br>
                <label>Stream : </label>
                <select name="stream" id="stream">
                    <option value="CSE">CSE</option>
                    <option value="IT">IT</option>
                    <option value="ECE">ECE</option>
                    <option value="ME">ME</option>
                </select><br>
                <label>Age : </label>
                <input type="text" name="age" id="age"><br>
                <input type="button" class="btn-submit" value="Submit">
            </form>
        </div>
        <script>
            jQuery(document).ready(function() {
                $('.btn-submit').click(function() {
                    $.ajax({
                        url:"insert.php",
                        method:"POST",
                        dataType: "json",
                        data:$('#cForm').serialize(),
                        success:function(data)
                        {
                            if(data.status == 200)
                            {
                                document.getElementById("cForm").reset();
                            }   
                        }
                    });
                });
            });
        </script>
    </body>
    </html>
    

    And your insert.php page replace

    echo "ok";
    

    by

    echo json_encode(['status'=>200,'message'=>'success']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀