将按钮的名称和值存储在 mysql 表行中
Posted
技术标签:
【中文标题】将按钮的名称和值存储在 mysql 表行中【英文标题】:Store name and value of a Button in a mysql table row 【发布时间】:2021-02-06 05:38:17 【问题描述】:我已经在这个问题上挂了 3 天,但无法解决。看起来比较简单,可惜我做不到,因为我是菜鸟。
我想用按钮的onclick函数来获取它的2条信息
```html
<input id="'.$row['id'].'" type="button" value="Pause" name="'.$row['name'].'" onclick="pushDataToDB()">```
-
name = "'.$ row ['name'].'" [来自数据库的名称引用
表]
和 value="Pause"
这两个信息我想存储在其他数据库表中。 我试图用不同的方式解决这个问题。
我尝试以不同的方式将 event.target.name 和 event.target.event javascript 输出存储在 php 变量中,然后在 mysql 插入行中使用它,但我失败了。
我尝试将值发布到 ajax.php 然后将值存储在 PHP 变量中并将其用作值将其推送到数据库,但这也不起作用
索引.php <div class="button2">
<form action="ajax.php" method="POST">
<input id="'.$row['id'].'" type="button" value="Pause" name="'.$row['name'].'" onclick="pushDataToDB()">
</Form>
</div>'
脚本.js:
function pushDataToDB()
$.get("ajax.php");
return false;
ajax.php
<?php
include_once ('dbh.php');
if(isset($_POST['name']))
$sqlAufPause = "INSERT INTO aktuellaufpause (name, pausenart) VALUES ('$name', 'BP')";
if ($conn->query($sqlAufPause) === TRUE)
echo "New record created successfully";
else
echo "Error: " . $sql . "<br>" . $conn->error;
?>
我想说这就是为什么名称 Value 为空的原因, 但我不知道如何修复ist..
emptyNameValue
【问题讨论】:
好像你用'get'来处理javascript,处理'post'来处理php代码。 【参考方案1】:您可以使用JQuery + AJAX 执行异步POST
<?php
$con = mysqli_connect("localhost", "root", "", "testdb");
$query = mysqli_query($con, "SELECT * FROM mytable WHERE id = 1");
$row = mysqli_fetch_assoc($query);
print_r($row);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="button2">
<form id="frm">
<input id="<?php echo $row['id'] ?>" type="button" value="Pause" name="<?php echo $row['name'] ?>" onclick="pushDataToDB()">
</Form>
</div>
</body>
<script type="text/javascript">
function pushDataToDB()
var name = "<?php echo $row['name'] ?>";
var value = "Pause";
$.ajax(
url: "ajax.php",
type: "POST",
data:
"name": name,
"value": value
,
success: function(e)
if (e == "1")
alert("Success!");
else
alert("error!");
);
</script>
</html>
ajax.php
<?php
$con = mysqli_connect("localhost", "root", "", "testdb");
$name = $_POST['name'];
$value = $_POST['value'];
$query = "INSERT INTO mytable(name, value) values('$name','$value')";
$result = mysqli_query($con, $query);
if ($result)
echo "1";
else
echo "Error!";
EDITTTT****
我变了
var name = "<?php echo $row['id'] ?>";
到
var name = "<?php echo $row['name'] ?>";
因为它是您要存储在数据库中的“名称”
【讨论】:
它不起作用,我认为这是因为我通过数据库中的每个值回显按钮。我不知道如何更改此代码。 ` 0) while ($row = mysqli_fetch_assoc($result)) $_SESSION['klapptdasFrage'] = $row['name'];回声“我通过这种方式解决了这个问题。 非常感谢!!!
index.php
<?php
$sql = "SELECT * FROM `DB-TABLENAME` ORDER BY `VALUE1`, `VALUE2`";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
while ($row = mysqli_fetch_assoc($result))
echo "<tr>";
echo "<td>";
echo '<input style="margin: 0 auto 6px 17px;" type="checkbox" id="scales" name="scales">';
echo "</td>";
echo "<td>";
echo $row['VALUE1'];
echo "</td>";
echo "<td>";
echo $row['VALUE2'];
echo "</td>";
echo "<td>";
echo '<div class="button1">
<input id="'.$row['id'].'" type="button" value="BP" name="'.$row['VALUE2'].'" onclick="pushDataToDB()">
</div>';
echo "</td>";
echo "<td>";
echo '<div class="button2">
<form id="frm">
<input id="'.$row['id'].'" type="button" value="Mittagspause" name="'.$row['name'].'" onclick="pushDataToDB()">
</form>
</div>';
echo "</td>";
echo "</tr>";
else
echo "there are no comments";
?>
<script type="text/javascript">
function pushDataToDB()
var name = event.target.name;
var value = event.target.value;
$.ajax(
url: "ajax.php",
type: "POST",
data:
"name": name,
"value": value
,
success: function(e)
if (e == "1")
alert("Success!");
else
alert("error!");
);
</script>
ajax.php
<?php
include_once('dbconnectioncredentials.php');
$con = mysqli_connect($servername, $username, $password, $dbname);
$name = $_POST['VALUE1'];
$value = $_POST['VALUE2'];
$query = "INSERT INTO aktuellaufpause (VALUE1, VALUE2) VALUES ('$name','$value')";
$result = mysqli_query($con, $query);
if ($result)
echo "1";
else
echo "Error!";
?>
【讨论】:
以上是关于将按钮的名称和值存储在 mysql 表行中的主要内容,如果未能解决你的问题,请参考以下文章