简体   繁体   中英

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

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

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. That should make the form names unique and allow the JavaScript to reference the specific form you want.

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". You're creating $rows forms in that name, so what happens is that only the first one gets submited.

I'd suggest naming each one differently: (look at the form name, and the onclick event)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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