插入MySQL数据库,而无需刷新页面使用ajax无法正常工作 [英] insert into mysql database without refreshing page using ajax not working

查看:36
本文介绍了插入MySQL数据库,而无需刷新页面使用ajax无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在不刷新页面的情况下将一些数据插入到mysql数据库表中因此,我编写了此脚本以将数据发送到包含将通过单击提交"按钮执行的查询的php页面.成功函数已执行输入字段变为空",但php文件似乎未执行

i try to insert some data into a mysql db table without refreshing page so i write this script to send the data to a php page that contain the query that will be executed by clicking the submit button the success function is executed ""the input feilds become empty "" but the php file seem to be not executed

   <?php
    $idst=$_GET['idst'];
    $idusr = $_SESSION['user_session'];
    $auth_user->viewReservations($idst,$idusr);
    ?>
    <!--******* add reservation ******-->    
    <tr>     
      <form >
            <td><input required type="text" id="client" name="title"></td>
            <td><input required type="text" id="tel" name="tel"></td>
            <td><input required type="date" id="start" placeholder="yy-MM-dd" 
            name="start"></td>
            <td><input required type="date" id="end" placeholder="yy-MM-dd" 
            name="end"></td>  
            <td><input required type="number" id="prix" name="prix"></td>
            <td></td>
            <td><input type ="submit" id="submit_reservation" class="btn btn-
 info" 
            name="submit" value="ajouter" style="width: 70px"></td>
            <td></td>
       </form>
          </tr>
         </table>
    <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
    <script>
    $(document).ready(function(){
    $("#submit_reservation").click(function()
    {
    var client =$("#client").val();
    var tel =$("#tel").val();
    var start =$("#start").val();
    var end =$("#end").val();
    var prix =$("#prix").val();
    var idst=<?php echo json_encode($idst); ?>;
    var idusr=<?php echo json_encode($idusr); ?>;
    $. ajax(
    {
    url: "addreserv.php",
    type: "POST",
    cache: false,
    data: {
    "done": 1,
    "client" :client,
    "tel" : tel,
    "start": start,
    "end": end,
    "prix":prix,
    "idst":idst,
    "idusr":idusr
    },
    success: function(data)
    {
    $("#client").val('');
    $("#tel").val('');
    $("#start").val('');
    $("#end").val('');
    $("#prix").val('');
    }
    })
    });
    });
    </script>

这是使用该页面发送的数据执行查询的php文件

and this is the php file to execute the query using the data sended from this page

<?php
/*require_once("session.php");*/
require_once("class.user.php");
if (isset($_POST['done']))
{
$adder = new USER();
$client=$_POST['client'];
$tel=$_POST['tel'];
$start=$_POST['start'];
$end=$_POST['end'];
$prix=$_POST['prix'] ;
$idst=$_post['idst'];
$idusr = $_POST['idusr'];
$adder->addReservation($client,$tel,$start,$end,$prix,$idst,$idusr);
}
?>

推荐答案

向表单添加ID并阻止表单提交.

Add a id to the form and prevent the form submit.

<tr>     
  <form id="reservation_form">
        <td><input required type="text" id="client" name="title"></td>
        <td><input required type="text" id="tel" name="tel"></td>
        <td><input required type="date" id="start" placeholder="yy-MM-dd" 
        name="start"></td>
        <td><input required type="date" id="end" placeholder="yy-MM-dd" 
        name="end"></td>  
        <td><input required type="number" id="prix" name="prix"></td>
        <td></td>
        <td><input type ="submit" id="submit_reservation" class="btn btn-info" 
        name="submit" value="ajouter" style="width: 70px"></td>
        <td></td>
   </form>
</tr>

$("#reservation_form").submit(function(event){
  event.preventDefault();
... Your logic ... 

}

这篇关于插入MySQL数据库,而无需刷新页面使用ajax无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆