gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:41:32 24 4
gpt4 key购买 nike

我想将成功的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 帖子有效,也没有错误......

最佳答案

您需要将 success 子句添加到 dataType 行下的 jQuery ajax 代码块中。

        success: function(data)
{

}

从这里开始,最简单的方法可能是向 PHP 页面发出第二个 Ajax 请求,该请求将为您添加必要的数据。

关于php - 从ajax成功请求将数据插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com