两个<a> and form <name=""> can&#39;t work together ? In html PHP

时间:2018-12-18 10:43:46

标签: php html href

If I use this code only <a href works > but I Want name="send" also work at a time(in isset($_POST['send']). but it's not working. so can I do for both cases?

I need to download and also send data to the database at the same time.

<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>

1 个答案:

答案 0 :(得分:0)

在提交表单之前,您需要更正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(如您在注释中提到的)将表单提交到同一url。参见下面的示例,

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

要使url中的ajax保持整洁,您可以将$result['product_file'];保留为HTML中的隐藏字段。因此,当我们按下免费下载按钮时,$(this).serialize()也将对该值进行序列化。因此,URL看起来与此类似,

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

在PHP后端中,product_file的检索需要稍加修改以适应此新更改。

希望这会有所帮助

谢谢