我想要做的是使用jQuery / Ajax使用php和mysql pdo更新输出表中的行。我在发布数据然后将其返回时遇到问题。
如果有人能帮助我开展这项工作,我们将不胜感激!谢谢!
这是我在users.php页面中使用的表单。 (请注意,这是在一个echo语句中,这就是变量为'。$ var。'的原因)。
每行都会重复以下表格。
Users.php
<form action="users_manage" method="post" id="submit_form" name="users_manage">
<input hidden id="uid" name="uid" value="' . $id . '" />
<input hidden id="first_name" name="first_name" value="' . $first_name . '" />
<input hidden id="last_name" name="last_name" value="' . $last_name . '" />
<input hidden id="contact_email" name="contact_email" value="' . $contact_email . '" />
<button id="usertp" name="user_type" value="1" type="submit" class="btn btn-rounded btn-sm btn-success"><i class="fa fa-check"></i></button>
<button id="usertp" name="user_type" value="2" type="submit" class="btn btn-rounded btn-sm btn-danger"><i class="fa fa-times"></i> </button>
</form>
有关users.php的脚本
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script>
$(document).ready(function() {
$("#usertp").click(function () {
var user_type = $('#usertp').val();
var uid = $('#uid').val();
var first_name = $('#first_name').val();
var last_name = $('#last_name').val();
var contact_email = $('#contact_email').val();
if(user_type=='') {
alert("Please Fill All Fields");
} else {
$.ajax({
type: "POST",
url: "users_manage.php",
data: {
user_type: user_type,
uid: uid,
first_name: first_name,
contact_email: contact_email
},
success: function (msg) {
// alert(msg);
$('#result').html(msg);
},
error: function () {
alert('error');
}
});
}
});
});
</script>
users_manage.php
<?php
$db = new PDO('mysql:host=127.0.0.1;dbname=test', 'test_user', '1234');
$uid = $_POST['uid'];
//$user_type = $_POST['user_type'];
$user_type = 0;
$stmt = $db->prepare("SELECT id FROM users WHERE id=:uid");
$stmt->execute( [ ':uid' => $uid] );
$num1 = $stmt->rowCount(PDO::FETCH_NUM);
if($num1 == 1){
$stmt = $db->prepare("UPDATE users SET user_type=:user_type WHERE id=:uid");
$stmt->execute([ ':uid' => $uid, ':user_type' => $user_type]);
}
答案 0 :(得分:0)
@Harvey Connor您可以查看下面的代码。请在表格中更改价值。
Users.php
<form action="" method="post" id="submit_form" name="users_manage">
<input hidden id="uid" name="uid" value="1" />
<input hidden id="first_name" name="first_name" value="firstname" />
<input hidden id="last_name" name="last_name" value="lastname" />
<input hidden id="contact_email" name="contact_email" value="test@gmail.com" />
<input id="usertp" name="user_type" value="1" type="submit" class="btn btn-rounded btn-sm btn-success" />
<input id="usertp" name="user_type" value="2" type="submit" class="btn btn-rounded btn-sm btn-danger" />
</form>
<div id="result"></div>
users.php上的脚本
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script>
$(document).ready(function() {
$("#usertp").click(function () {
var user_type = $('#usertp').val();
if(user_type=='') {
alert("Please Fill All Fields");
return false;
} else {
$.ajax({
type: "POST",
url: "users_manage.php",
data: $("#submit_form").serialize(),
success: function (msg) {
$('#result').html(msg);
return false;
},
error: function () {
alert('error');
return false;
}
});
}
return false;
});
});
</script>
users_manage.php
<?php
//$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$db = new PDO('mysql:host=127.0.0.1;dbname=test', 'test_user', '1234');
$uid = $_POST['uid'];
//$user_type = $_POST['user_type'];
$user_type = 0;
$stmt = $db->prepare("SELECT id FROM users WHERE id=:uid");
$stmt->execute( [ ':uid' => $uid] );
$num1 = $stmt->rowCount(PDO::FETCH_NUM);
if($num1 == 1){
$stmt = $db->prepare("UPDATE users SET user_type=:user_type WHERE id=:uid");
$stmt->execute([ ':uid' => $uid, ':user_type' => $user_type]);
$result = "User successfully updated.";
} else {
$result = "User update fail.";
}
echo $result;