简体   繁体   中英

How do I pass arguments from one php file to another?

I have to make an online shop. I generate my products from a database like this:

 $sql = "SELECT * FROM wblouses";
                    $result = mysql_query($sql);
                    if (! $result)
                    {

                        echo "eroare db  database-item";
                    }
                    else
                        while($db_field = mysql_fetch_assoc($result)) {
                            echo "<div class=\"col-md-4\"><div class=\"thumbnail\"><form  method=\"POST\" action=\"addToChart.php\" ><img style=\"display: block;\" src=";
                            echo $db_field["poza"]." height=\"250\" width=\" 400 \"> <p class=\"prices\">
                                        <span class=\"price\" data-color=\"401\" >
                                                <span class=\"currentPrice\"> ".$db_field["pret"]."&nbsp;LEI&nbsp;&nbsp;</span>
                                        </span>
                                        <input type=\"username\" name=\"username\">
                                        <select id=\"myselect\" name=\"myselect\">
                                          <option value=".$db_field["ID"].">SIZE</option>
                                          <option value=\"XS\">XS</option>
                                          <option value=\"S\">S</option>
                                          <option value=\"M\">M</option>
                                          <option value=\"L\">L</option>
                                        </select>
                                        <input type=\"submit\" value=\"add to chart\">

                                </p></form><button id=\"ilas\" onClick=\"fct(this.id)\">B3</button></div></div>  ";

                        }

In another .php file I manage a database which represents a list of items selected from the one posted. How do I pass the id of a particular item to another file, I only seem to get the last generated id.

How can I pass some arguments to another php file? The problem is that every product has it`s own generated div,dropdown and button with the same name and ID , how I find out in .php which product I refer to(it always refer to the last added product)? @Skrrp @TEster

Simple answer, you can't pass both bits of data from the same select.

You could do some funky concatenation, such as;

<option value=\"" . $db_field["ID"] . ";XS\">XS</option>

and then do a string spilt on the other side. This is silly. Don't do this.

If you have multiple products that you want the user to select from, what you want is 2 select drop-downs, one for product and one for size. If certain products are available only in certain sizes you will need some JavaScript to sort out the second.

What you are probably looking for is a hidden data field. It looks like you have already selected the product (and its ID) by the stage you generate this form. Rather than putting the $db_field["ID"] in the select, put it in its own control.

<input type=\"hidden\" name=\"id\" value=\"" . $db_field["ID"] . "\">

Then when you get to your next page, $_POST["id"] will contain the product ID you need.

As the above guys said,

I recommend using a hidden field to store the value.

$id = $db_field["ID"];
<input type='hidden' name='id' value='$id'>

Then just collect it using $_POST['id'] on the other php script.

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