html - 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 :
<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
<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
if (isset($_GET['edit'])) {
# code...
$name= $_GET['category_name'];
echo $_POST['category-name']= $name;
}
Answer
Solution:
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.
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).
<input id="cc-pament" name="category-name" type="text" class="form-control" value="<?= htmlentities($_GET["edit"]??"") ?>">
Be sure to encode the value withhtmlentities
or similar to avoid XSS vulnerabilities.
Side note, what exactly is the intent of this line?
echo $_POST['category-name']= $name;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.