gpt4 book ai didi

php - 如何在单击单选按钮时插入数据 - jquery/php/mysql?

转载 作者:行者123 更新时间:2023-11-29 03:18:41 25 4
gpt4 key购买 nike

在 php 和 mysql 输出表中的数据列表,其中包含数据库中每一行的单选按钮、下拉框和文本框,我希望能够通过 AJAX 更新数据库 onclick of radio button, dropdown boxes或输入文字。这就是我的...

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<?php
require_once 'config.php';

echo '<table style="margin:0 auto;">
<tr>

</tr>';

$sql = "SELECT id, address, suburb, lat, lng, day, date, time FROM addresses";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {


echo '<tr>

<td><span style="font-weight:bold;">'. $row["address"].' '. $row["suburb"].'</span> <button style="float:right;" type="button">Go</button><br><br>
H<input type="radio" name="home['. $row["id"].']" value="1"/>
NH<input type="radio" name="home['. $row["id"].']" value="2"/>
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time1" size="2">
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time2" size="2"><br>
<input style="width:100%; margin-top:5px;" type="text" name="notes" placeholder="Add note">
<br><br>
</td>
<td>

</td>


</tr>';

}

} else {
echo "0 results";
}
echo '</table>';

$conn->close();

?>

<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var home = $(this).val();
$.ajax({
url:"updateaddress.php",
method:"POST",
data:{home:home},
});
});
});
</script>

<?php
// Include config file
require_once 'config.php';

$id = mysqli_real_escape_string($conn, $_POST['id']);
$home = mysqli_real_escape_string($conn, $_POST['home']);



if(isset($_POST["home"])) {
$sql = "UPDATE addresses SET home='$home' WHERE id=$id";
if($conn->query($sql) === TRUE){

} else {
echo "error" . $sql . "<br>".$conn->error;
}
}



mysqli_close($conn);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
?>

我不知道我需要做什么才能让它工作。请帮忙。

最佳答案

您需要将行的 ID 传递给该 ajax 页面...

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<?php
require_once 'config.php';

echo '<table style="margin:0 auto;">
<tr>

</tr>';

$sql = "SELECT id, address, suburb, lat, lng, day, date, time FROM addresses";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {


echo '<tr>

<td><span style="font-weight:bold;">'. $row["address"].' '. $row["suburb"].'</span> <button style="float:right;" type="button">Go</button><br><br>
H<input type="radio" name="home['. $row["id"].']" id="'. $row["id"].'" value="1"/>
NH<input type="radio" name="home['. $row["id"].']" id="'. $row["id"].'" value="2"/>
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time1" size="2">
<select style="padding:1.4px;">
<option></option>
<option>Mo</option>
<option>Tu</option>
<option>We</option>
<option>Th</option>
<option>Fr</option>
<option>Sa</option>
<option>Su</option>
</select>
<input type="text" name="time2" size="2"><br>
<input style="width:100%; margin-top:5px;" type="text" name="notes" placeholder="Add note">
<br><br>
</td>
<td>

</td>


</tr>';

}

} else {
echo "0 results";
}
echo '</table>';

$conn->close();

?>

<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var home = $(this).val();
var id = $(this).attr('id');
$.ajax({
url:"updateaddress.php",
method:"POST",
data:{home:home,id:id},
});
});
});
</script>

<?php
// Include config file
require_once 'config.php';

$id = mysqli_real_escape_string($conn, $_POST['id']);
$home = mysqli_real_escape_string($conn, $_POST['home']);



if(isset($_POST["home"])) {
$sql = "UPDATE addresses SET home='$home' WHERE id=$id";
if($conn->query($sql) === TRUE){

} else {
echo "error" . $sql . "<br>".$conn->error;
}
}



mysqli_close($conn);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
?>

关于php - 如何在单击单选按钮时插入数据 - jquery/php/mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49357705/

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