【问题标题】:Both <a> and form <name=""> can't work together ? In html PHP<a> 和表单 <name=""> 不能一起工作?在 html PHP
【发布时间】:2019-05-18 18:25:31
【问题描述】:

如果我只使用此代码 &lt;a href works &gt; 但我希望 name="send" 一次也可以工作(在 isset($_POST['send']) 中)。但它不起作用。所以我可以为这两种情况做?

我需要同时下载数据并将数据发送到数据库。

<a href="./users/assets/upload/file/<?php echo $result['product_file']; ?>">
  <form method="POST" action=" ">
    <div class="row" style="background-color: #8CC63F;">
      <div class="col-lg-12 dw_text text-center">
        <button type="submit" name="send" style="font-size: 30px;">FREE DOWNLOAD</button>
      </div>
  </form>
</a>

【问题讨论】:

  • 您的 HTML 无效。并且您没有任何操作 - 您可以使用 AJAX 来执行这两项操作或在服务器上重定向
  • 使用 AJAX 可以向数据库提交一个值,并且一次可以使用 ?????
  • 您可以将href的url发送到服务器并在返回时提交表单,或者您可以提交表单并在返回时单击链接,或者您可以提交表单并且服务器重定向到新网址 - 给猫剥皮的多种方法
  • 你能给我看一个演示代码吗,因为我对此感到困惑。
  • 好的,我明白了,谢谢

标签: php html href


【解决方案1】:

在提交表单之前,您需要更正您的 HTML。看起来您在按下“免费下载”按钮时正在拨打url

要更正 HTML,您可以直接使用链接的 url 作为表单的 action(像这样)

  <form method="POST" action="./users/assets/upload/file/<?php echo $result['product_file']; ?>">
    <div class="row" style="background-color: #8CC63F;">
      <div class="col-lg-12 dw_text text-center">
        <button type="submit" name="send" style="font-size: 30px;">FREE DOWNLOAD</button>
      </div>
  </form>

或使用 jQuery(正如您在 cmets 中提到的)将表单提交到相同的 url。请参阅下面的示例,

$.ajax({
    type: 'POST', 
    url: "./users/assets/upload/file/<?php echo $result['product_file']; ?>",
    data: $(this).serialize(),
    success: function(data) { } 
});

为了在上述ajax 提交中保持url 整洁,您可以将$result['product_file']; 保留为HTML 中的隐藏字段。因此,当我们按下免费下载按钮时,$(this).serialize() 也会序列化该值。因此,网址看起来与此类似,

url: "./users/assets/upload/file",

在 PHP 后端,product_file 的检索需要稍微修改以适应这一新变化。

希望对你有帮助

谢谢

【讨论】:

  • 但对于 ,“发送”名称在此过程中不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
相关资源
最近更新 更多