简体   繁体   English

通过href链接进行POST

[英]Make POST by href link

I am trying to make a post through href link.I am using a dropdown menu which contains those links. 我正在尝试通过href链接发布帖子。我正在使用包含这些链接的下拉菜单。 The problem is that wherever click I get the same result as the first one. 问题在于,无论单击何处,我都将获得与第一个相同的结果。 I want each link to post to the result that get from query with that id not to the first one. 我希望每个链接都发布到从具有该ID的查询中获得的结果,而不是第一个。

<li>
  <?php  $rows=query("SELECT * FROM filmi") ?>
    <?php foreach($rows as $r):?>
      <form name="myform" method="post" action="description.php">
        <input type="hidden" name="id" value="
        <?=$r["id"]?>"/>
      </form>   
      <a href="description.php" onclick="document.forms['myform'].submit(); return false;">
        <?=$r["titulli"]?>
      </a>
  <?php endforeach?>    
</li>

You're opening a new form each time you run through a Foreach loop. 每次运行Foreach循环时,您都会打开一个新表单。 You're also using the same Input Field name for each interation. 您还为每个交互使用相同的输入字段名称。

Try this: 尝试这个:

<li>
    <?php  $rows=query("SELECT * FROM filmi") ?>
     // Your form is opened and closed outside of the Foreach() loop.
     // This means all of your hidden fields will print within the same form.
     <form name="myform" method="post" action="description.php">
     <?php foreach($rows as $r):?>
           //  If you're expecting more than one id, you'll need to 
           //  post your form using array syntax.  Each "id" will be posted
           //  to PHP in incremented fashion.
           <input type="hidden" name="id[]" value="<?=$r["id"]?>"/>                                           

      <a href="description.php" onclick="document.forms['myform'].submit(); return false;">   
    <?=$r["titulli"]?></a>                                          
    <?php endforeach?>  
        </form> 
    </li>

I get the same result as the first one 我得到与第一个相同的结果

That's because you have multiple forms with the same name. 那是因为您有多个具有相同名称的表格。 So your JavaScript reference to document.forms['myform'] always finds the first one by that name. 因此,您对document.forms['myform']的JavaScript引用始终会找到该名称的第一个。

Try making the name more dynamic. 尝试使名称更具动态性。 In your foreach loop, append some unique record value (the record's ID should do nicely) to the name="myform" and, subsequently, to the JavaScript reference to that element. foreach循环中,将一些唯一的记录值(记录的ID应该很好)附加到name="myform" ,然后附加到对该元素的JavaScript引用。 That should make the form names unique and allow the JavaScript to reference the specific form you want. 这应该使表单名称唯一,并允许JavaScript引用所需的特定表单。

That appen because ALL of your forms have the same name, for do what you want you need to make the form (tag name) dynamic. 出现这种情况是因为所有表单都具有相同的名称,因此您需要做的就是使表单(标记名)动态化。 like this for the name: 像这样的名字:

<?php $i = 0;?>
<?php foreach($rows as $r):?>
    <form name="myform<?php print $i;?>" method="post" action="description.php">
       <input type="hidden" name="id" value="<?=$r["id"]?>"/>                                           
    </form> 
    <a href="description.php" onclick="document.forms['myform<?php print $i;?>'].submit(); 
return false;">
<?php $i++;?>

document.forms['myform'].submit() will submit the form named "myform". document.forms['myform'].submit()将提交名为“ myform”的表单。 You're creating $rows forms in that name, so what happens is that only the first one gets submited. 您正在使用该名称创建$rows表单,所以发生的是只有第一个表单被提交。

I'd suggest naming each one differently: (look at the form name, and the onclick event) 我建议以不同的方式命名每个名称:(查看表单名称和onclick事件)

<li>
  <?php  $rows=query("SELECT * FROM filmi") ?>
    <?php foreach($rows as $r):?>
      <form name="myform<?=$r["id"]?>" method="post" action="description.php">
        <input type="hidden" name="id" value="<?=$r["id"]?>" />
      </form><a href="description.php" onclick="document.forms['myform<?=$r["id"]?>'].submit(); return false;">
        <?=$r["titulli"]?>
      </a>
  <?php endforeach?>    
</li>

Or, if they're always one after the other, simply posting the previousSibling. 或者,如果它们总是一个接一个,则只需发布previousSibling。

<li>
  <?php  $rows=query("SELECT * FROM filmi") ?>
    <?php foreach($rows as $r):?>
      <form method="post" action="description.php">
        <input type="hidden" name="id" value="<?=$r["id"]?>" />
      </form><a href="description.php" onclick="this.previousSibling.submit(); return false;">
        <?=$r["titulli"]?>
      </a>
  <?php endforeach?>    
</li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM