[英]How to pass text/variable from php script to input value?
I need to get id and name from URL when I click edit anchor and I get it actually but I can't post data to input value to make an update by the text input... here is my HTML form:当我单击编辑锚点时,我需要从 URL 获取 ID 和名称,但我无法将数据发布到输入值以通过文本输入进行更新......这是我的 HTML 表单:
<form action="" method="post">
<div class="form-group">
<label for="cc-payment" class="control-label mb-1">Category Name</label>
<input id="cc-pament" name="category-name" type="text" class="form-control" value="">
</div>
<div>
<button id="payment-button" type="submit" name="submit" class="btn btn-lg btn-info">Save
</button>
</div>
</form>
here is HTML TABLE And have the Edit anchor to go if isset这里是 HTML TABLE 如果 isset 有编辑锚点到 go
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody action="" method="get">
<?php
$query = " select * from category ";
$result = mysqli_query($conn,$query);
while($cat = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>{$cat['cat_id']}</td>";
echo "<td>{$cat['cat_name']}</td>";
echo "<td><a href='?edit={$cat['cat_id']}&category_name={$cat['cat_name']}' name='edit'
class='btn
btn-info'> Edit</a></td>";
echo "<td><a href='delete_category.php?id={$cat['cat_id']}' class='btn btn-danger'>Delete</a>
</td>";
echo "</tr>";
}
?>
</tbody>
here is the PHP code这是 PHP 代码
if (isset($_GET['edit'])) {
# code...
$name= $_GET['category_name'];
echo $_POST['category-name']= $name;
}
If I am understanding correctly, I believe you want to populate the category-name field on your form based the edit value in the URL.如果我理解正确,我相信您想根据 URL 中的编辑值填充表单上的类别名称字段。
If so, you have most of what you need, you simply need to insert it into the HTML(I am assuming the form is part of a PHP script as well).如果是这样,您拥有大部分所需内容,只需将其插入 HTML(我假设该表单也是 PHP 脚本的一部分)。
<input id="cc-pament" name="category-name" type="text" class="form-control" value="<?= htmlentities($_GET["edit"]??"") ?>">
Be sure to encode the value with htmlentities
or similar to avoid XSS vulnerabilities.请务必使用htmlentities
或类似方法对值进行编码,以避免 XSS 漏洞。
Side note, what exactly is the intent of this line?旁注,这条线的意图到底是什么?
echo $_POST['category-name']= $name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.