从ajax成功请求将数据插入数据库

时间:2019-08-20 21:01:42

标签: php mysql ajax

我想从成功的ajax请求div中将数据插入数据库中

我想从成功的ajax请求div中将数据插入数据库中 我已经使用数据库中生成的ID调用了ajax,现在我要做的就是使用ajax成功变量将数据提交到表中。

<form action="" method="post" enctype="multipart/form-data">
                <input type="hidden" name="clientID" value="<?php echo $clientID;?>">
                <div class="form-group has-feedback">
                  <input type="text" class="form-control" autocomplete="off" name="name" required="" placeholder="RIDER NAME">
                  <span class="glyphicon glyphicon-user form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback">
                  <input type="text" class="form-control" name="phone_number" placeholder="PHONE NUMBER" autocomplete="off">
                  <span class="glyphicon glyphicon-phone-alt form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback">
                  <input type="text" class="form-control" name="IMEI" placeholder="IMEI NUMBER" autocomplete="off">
                  <span class="glyphicon glyphicon-phone form-control-feedback"></span>
                </div>
                <div class="row">
                  <div class="col-xs-1"></div>
                  <div class="col-xs-6">
                  </div>
                  <!-- /.col -->
                  <div class="col-xs-4">
                    <button type="submit" class="btn btn-primary btn-block btn-flat" name="register_rider">Register Rider</button>
                    <br/>
                  </div>
                  <!-- /.col -->
                </div>
              </form>

这会执行ajax请求

<script>
        $(document).ready(function(){

         $(document).on('click', '#getUser', function(e){

          e.preventDefault();

    var uid = $(this).attr("data-id");   // it will get id of clicked row

    $('#debtor').html(''); // leave it blank before ajax call
    $('#mySidenav').show();      // load ajax loader

    $.ajax({
      url: 'getClientDetails.php',
      type: 'POST',
      data: 'id='+uid,
      dataType: 'html'
    })
    .done(function(data){
      console.log(data);  
      $('#debtor').html('');    
      $('#debtor').html(data); // load response 
      $('#modal-loader').hide();      // hide ajax loader 
    })
    .fail(function(){
      $('#debtor').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
      $('#modal-loader').hide();
    });

  });

       });

     </script>

//这是getClientDetails.php

<?php
include_once('config/dbconfig.php');



$sql = "SELECT * FROM clients WHERE id=:id";
//Prepare your SELECT statement.
$statement = $pdo->prepare($sql);
//The Primary Key of the row that we want to select.
$id = intval($_REQUEST['id']);
//I highly recomment not to use $_REQUEST, use $_GET or even better $_POST

//Bind our value to the paramater :id.
$statement->bindValue(':id', $id);

//Execute our SELECT statement.
$statement->execute();

//Fetch the row.
$row = $statement->fetch(PDO::FETCH_ASSOC);

$full_name= $row['full_name'];
$user_name = $row['user_name'];
$phone = $row['phone'];
$IMEI = $row['IMEI'];
$latitude = $row['latitude'];
$longitude = $row['longitude'];
$email = $row['email'];
$clientID = $row['id'];



if (!empty($_POST['register_rider'])){
    $clientID = trim($_POST['clientID']);
    $clientID = strip_tags($clientID);
    $clientID = htmlspecialchars($clientID);

    $name = trim($_POST['name']);
    $name = strip_tags($name);
    $name = htmlspecialchars($name);

    $phone_number = trim($_POST['phone_number']);
    $phone_number = strip_tags($phone_number);
    $phone_number = htmlspecialchars($phone_number);

    $IMEI = trim($_POST['IMEI']);
    $IMEI = strip_tags($IMEI);
    $IMEI = htmlspecialchars($IMEI);




 $stmt = $pdo->prepare('INSERT INTO drivers (clientID, name, phone_number, IMEI)VALUES(:clientID, :name, :phone_number, :IMEI)');

 $stmt->bindParam(':clientID', $clientID);
 $stmt->bindParam(':name', $name);
 $stmt->bindParam(':phone_number', $phone_number);
 $stmt->bindParam(':IMEI', $IMEI);





 if($stmt->execute()){
    ?>
    <script>
      alert("Rider Added successfully");
    </script>
    <?php
    header( "refresh:5;url=clies.php" );
  }
  else{
    ?>
    <script>
      alert("Couldn't add rider");
    </script>
    <?php
  }



}

?>

我想做的是将数据插入数据库,但是尽管isset post有效,但是也没有任何作用...

1 个答案:

答案 0 :(得分:0)

您需要在dataType行下的jQuery ajax代码块中添加成功子句。

        success: function(data)
        {   

        }

从那里开始,可能最简单的是对PHP页面执行第二个Ajax请求,这将为您添加必要的数据。