简体   繁体   English

如何将 php 脚本中的文本/变量传递给输入值?

[英]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.

相关问题 将“值”变量从“输入按钮”传递给php - Pass “value” variable from “input button” to php 如何将 PHP 变量值从外部 PHP 文件传递​​到 html 输入框? - How to pass PHP variable value to html input box from external PHP file? 如何将输入从PHP脚本传递到另一个脚本? - How can I pass the input from a PHP script to another script? 将变量从URL传递到PHP并传递给Javascript,并在输入字段中显示值 - Pass variable from URL to PHP to Javascript and Display Value in Input Field 如何从输入文本中获取变量的值? - How to take value in a variable from an input text? 如何在PHP命令(CLI)中传递bash脚本变量值 - How to pass bash script variable value in PHP command (CLI) 如何将java脚本提示msg变量值传递给php? - how to pass java script prompt msg variable value in to php? 如何将html输入值传递给php? - 变量不起作用 - How to pass html input value to php? - variable not working 如果文本输入字段是可选的,我应该如何将其值从javascript传递到php - if the text input field is optional, how should i pass the value of it from javascript to php 我键入时如何通过Jquery Ajax将输入文本中的值传递给php函数? WordPress的 - How to pass a value from input-text through Jquery Ajax to php function while I type? Wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM