简体   繁体   中英

submit form with checkbox 2 values

I am a newbie and I have tried multiple ways with no luck.

I am retrieving productName, productID, and productLink from my database.

I have a form based on checkbox. I want to be able to pass only the data of the checked box. I want to POST the productId and the productLink of the checked box.

Right now when I submit, I get the productId of the checked product but I get all the product Links. How do I make it so I only get the product link of the checked box.

    <form action="process.php" method="post">
   <table>
   <tr> 
   <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" /> Product Name Goes Here</td>
   </tr>
   <tr>
    <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" />  Product Name Goes Here</td>
    </tr>
    <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" /> Product Name Goes Here</td>
    <tr>
    <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" /> Product Name Goes Here</td>
    </tr>
    <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" /> Product Name Goes Here</td>
    <tr>
    <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" /> Product Name Goes Here</td>
    <td> <input type="checkbox" name="productId[]" value="<?php echo $products->id; ?>" /> <input type="hidden" name="productLink[]" value="<?php echo $products->link; ?>" /> Product Name Goes Here</td>
    </tr>

    </table>
    <input type="submit" name="formSubmit" value="Submit" />

    </form>

Here is the array I am now getting.

    Array
    (

    [productId] => Array
    (
    [400] => link.html
    [399] => link.html
    )

    }

Since you are using an array as the variable you're passing, make the array do the work for you:

<label>
    <input type="checkbox" 
           name="productId[<?= $products->id ?>]" 
           value="<?= $products->link ?>" /> Product Name Goes Here 
</label>

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