dousuo2812 2014-01-08 23:02
浏览 33
已采纳

html表单按钮携带数组元素作为字符串

I want to be able to carry $_POST['v'] (string) to delete.php using an HTML form button.

$directory = "exports/";
$contents = scandir($directory,1);
foreach($contents as $k => $v) {
if ($v != '.' && $v != '..') { 
  echo "<a href=\"$directory" . $v . "\">".$v."</a>";
  echo "<form action=\"delete.php\" method=\"post\"><a class=\"buttonnohover\"><input type=\"submit\" class=\"button\" name=\"v\" value=\"Delete\" /></form>  <br>";
  }
}

At the moment the submit button simply brings back string(6) "Delete". I want $_POST['v'] to equal each individual $v (filename within the directory) carried on each button.

What am I not doing right?

  • 写回答

1条回答 默认 最新

  • doujiaozhao2489 2014-01-08 23:08
    关注

    When you want to get the value of $v to delete.php using the form, you can use a hidden input:

    echo '<a href="'.$directory.$v.'">'.$v.'</a>';
    echo '<form action="delete.php method="post">
        <input type="hidden" name="v" value="'.$v.'" />
        <a class="buttonnohover"><input type="submit" class="button" name="submit" value="Delete" />
        </form><br>';
    

    Currently you are getting "Delete" back, because it is the value of the button - just as you defined it in the HTML.

    Additionally I would suggest renaming $v to $filename - currently it is only a cryptic variable, because the context is small of course you know what it holds, but only by looking at other lines of code. When you name it $filename, you'll directly know what the content of that variable will be.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi