简体   繁体   中英

two inputs in same form are conflicting

I have a form that contains a table. Within each row of the table are two inputs. One of them is a text input to update the quantity of items in a cart. The other input is an image. When clicked the item should be deleted. If you update the amount in the text cell and hit return, it attempts to update it AND delete it. I've dumped the variables, and the $_POST array contains both the following entries:

KEY: summary

VAL: array(3) { ["ccbna007"]=> string(1) "5" ["ccbna001"]=> string(2) "10" ["ccbna002"]=> string(2) "10" }

AND

KEY: delete

VAL: array(1) { ["ccbna007"]=> string(4) "-302" }

Can someone help me figure out how my markup is affecting my submitted variables, and in turn, how to fix the error? See the markup below. Please let me know if more information is needed.

...inside a table within the form

<form action="distro.php" method="post">

    <tr>
    <td>S/T LP</td>
    <td><input type="text" value="10" name="summary[ccbna001]" size="2"></td>
    <td>10.00</td>
    <td>
      <a>
          <input alt="Remove item from your cart" type="image" title="Remove Item" src="img/delete.gif" name="delete[ccbna001]" height="10px" width="10px">

      </a>
    </td>
  </tr>

...More rows like this one...

</form>

If I were you I would do it like this:

<form action="distro.php" method="post">
<table>
    <tr>
    <td>S/T LP</td>
    <td>
       <input type="text" value="10" name="summary[ccbna001]" size="2">
    </td>
    <td>10.00</td>
    <td>
      <a href="delete.php?id=123" id="delete[ccbna001]" >
          <img src="img/delete.gif" alt="Remove item from your cart" 
                height="10px" width="10px"/>
      </a>
    </td>
  </tr>
  <tr><td><input type="sumit" value="submit"></td></tr>
</table>
</form>

Because an image isnt an input, I would call a deleting script through the anchor tag.

When you press enter , you are submitting all inputs as they fall under the same form. Make two forms and keep both inputs separate if you want them to be treated separately.

Change the image input to simple image tag, then enclose it with link. The link should guide to the script which will delete a row from your database or session.

You should have form for every row like this:

<tr>
    <form action="distro.php" method="post">
        <td>S/T LP</td>
        <td><input type="text" value="10" name="summary[ccbna001]" size="2"></td>
        <td>10.00</td>
        <td>
            <a>
                <input alt="Remove item from your cart" type="image" title="Remove Item" src="img/delete.gif"
                       name="delete[ccbna001]" height="10px" width="10px">

            </a>
        </td>

        <input type="sumit" value="submit">
    </form>
</tr>

and so on for each row ...

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