jroryl Posted February 27, 2011 Share Posted February 27, 2011 First time poster first php page .... I have a MySQL database which has products and I have populated a dropdown that displays the products and price (concatenate the 2 $item & $price), this (apparently is working). What I can’t figure out is how in the heck do I pass the chosen dropdown value to a form (PayPal), currently its always passing the last value in the dropdown. Help! Database is like this: Item price Product1 | 12.00 Product2 | 13.00 Product3 | 14.00 Product4 | 15.00 Product5 | 16.00 Etc … Dropdown has all the above values but when choose any value and click submit, Product5 is sent to the PayPal form, basically the last one every time. Here’s the code: <?php include('../../../config.php'); ?> <html> <body> Products : <select name"> <? $list=mysql_query("SELECT * FROM products"); // Show records by while loop. while($row_list=mysql_fetch_array($list)){ $title = $row_list[2]; $price = $row_list[4]; print "<option value=$title>$title $ $price</option>"; // End while loop. } ?> </select> <form method="get" action="blah blah" target="post"> <input type="hidden" name="cmd" value="_cart" /> <input type="hidden" name="business" value="blah blah" /> <input type="hidden" name="amount" value="<?php echo $price; ?>"> <input type="hidden" name="item_name" value="<?php echo $title; ?>"> <input type="hidden" name="item_number" value="<?php echo $title; ?>"> <input type="hidden" name="return" value="blah blah" /> <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="undefined_quantity" value="1" /> <input type="hidden" name="no_note" value="1" /> <input type="hidden" name="bn" value="PP-BuyNowBF" /> <input type="hidden" name="add" value="1" /> <input type="submit" value="Add To Cart" /> </select> </form> <? // End if statement. // Close database connection. mysql_close(); ?> </p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/ Share on other sites More sharing options...
jroryl Posted February 28, 2011 Author Share Posted February 28, 2011 anyone? is this even doable? Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1180623 Share on other sites More sharing options...
DavidAM Posted February 28, 2011 Share Posted February 28, 2011 [*]Your combobox is NOT inside the form. Only fields that are defined between the <FORM> and </FORM> tags are POSTed. [*]Your combobox does NOT have a valid name. Only fields with a name attribute are POSTed with the form. [*]You have an extra </SELECT> at the end of the form, that has NO associated openning tag, anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1180682 Share on other sites More sharing options...
jroryl Posted February 28, 2011 Author Share Posted February 28, 2011 First thank you for pointing out the fundamentals, I have changed the script to the following and it's still sending the last record to the form. <?php include('../../../config.php'); ?> <html> <body> Products : <form method="get" action="blah blah" target="post"> <select name"products"> <? $list=mysql_query("SELECT * FROM products"); // Show records by while loop. while($row_list=mysql_fetch_array($list)){ $title = $row_list[2]; $price = $row_list[4]; print "<option value=$title>$title $ $price</option>"; // End while loop. } ?> <input type="hidden" name="cmd" value="_cart" /> <input type="hidden" name="business" value="blah blah" /> <input type="hidden" name="amount" value="<?php echo $price; ?>"> <input type="hidden" name="item_name" value="<?php echo $title; ?>"> <input type="hidden" name="item_number" value="<?php echo $title; ?>"> <input type="hidden" name="return" value="blah blah" /> <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="undefined_quantity" value="1" /> <input type="hidden" name="no_note" value="1" /> <input type="hidden" name="bn" value="PP-BuyNowBF" /> <input type="hidden" name="add" value="1" /> <input type="submit" value="Add To Cart" /> </select> </form> <? // End if statement. // Close database connection. mysql_close(); ?> </p> </body> </html> Only fields with a name attribute are POSTed with the form. Does that mean that only 1 field can be sent to the form? How would you name a SELECT with 2 fields? ($title & $price)? Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1180899 Share on other sites More sharing options...
DavidAM Posted February 28, 2011 Share Posted February 28, 2011 it's still sending the last record to the form. You have all of the hidden fields INSIDE of the SELECT. This is not valid. Move the closing tag for the SELECT so it is output immediately after the last OPTION (before anything else on the form). Does that mean that only 1 field can be sent to the form? You can have multiple fields on a form. Each field has to have a name that is unique in the form. This name is the way you refer to the field's value in the $_POST array. How would you name a SELECT with 2 fields? ($title & $price)? A field on the form can return a single value or an array of values. The values you are assigning to your OPTIONS are composed of multiple database fields. You will have to break that value up in the script that receives the POST. The "array of values" does not seem to apply here. If you wanted to allow the user to select multiple OPTIONS, you could define the SELECT to allow multiples and to return an array. Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1180979 Share on other sites More sharing options...
jroryl Posted March 1, 2011 Author Share Posted March 1, 2011 The values you are assigning to your OPTIONS are composed of multiple database fields. You will have to break that value up in the script that receives the POST. Thank you for the reply, I guess that's where I'm a little hung up on. Could you supply an example on how to do this or a link? Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1181502 Share on other sites More sharing options...
redarrow Posted March 1, 2011 Share Posted March 1, 2011 Little example with a hidden field. It all static so you understand it. <?php $members_id="001"; $amount_money="£10.00"; if($_POST['submit']){ $ansaw=$_POST['ansaw']; if($members_id=="001" && $ansaw=="Yes" ){ echo "<center> Your id is: $members_id <br> You currently owe us $amount_money but your ansaw was $ansaw <br /> <br /> </center>"; } if($members_id=="001" && $ansaw=="No" ){ echo "<center> Your id is: $members_id <br> You no you owe us $amount_money please pay immediately! <br /> <br /> </center>"; } } ?> <?php echo"<center> Did you pay us, the full amount off money!. <br /> <br /></center>"; ?> <center> <form method="POST" action="form.php"> <select name="ansaw"> <option value="Yes">Yes</option> <option value="No">No</option> <input type="hidden" name="members_id"> </select> <br /> <br /> <input type="submit" name="submit" value="SEND"> </form> </center> Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1181540 Share on other sites More sharing options...
jroryl Posted March 1, 2011 Author Share Posted March 1, 2011 I'll try this, thank you for the example, I'm sure more questions will arise . Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1181609 Share on other sites More sharing options...
jroryl Posted March 2, 2011 Author Share Posted March 2, 2011 I tried to incorporate your example and I'm getting the same results as I was getting, basically I can see that it's still only POST(ing) one value (ansaw) in your example, at least when I do a print_r($_POST); only the [ansaw] in the array is populated. How do you get 2 values populated based on your example, either $members_id or $amount_money to show up in print_r($_POST);? I hope I explained that clearly enough. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1181718 Share on other sites More sharing options...
jroryl Posted March 3, 2011 Author Share Posted March 3, 2011 Getting closer ... So looks like I ended up with an array, here is the result from doing a print_r($_GET); Array ( [item_name] => Product1 1.49 [amount] => [business] => [email protected] [item_number] => [return] => http://www.DOMAIN.com/thankyou [currency_code] => USD [undefined_quantity] => 1 [no_note] => 1 [bn] => PP-BuyNowBF [add] => 1 [submit] => SEND ) The problem, for the item_name array it has "Product1 1.49" how to I get the "1.49" amount into the hidden input [amount]? Basically the above array should look like this: Array ( [item_name] => Product1 [amount] => 1.49 [business] => [email protected] [item_number] => [return] => http://www.DOMAIN.com/thankyou [currency_code] => USD [undefined_quantity] => 1 [no_note] => 1 [bn] => PP-BuyNowBF [add] => 1 [submit] => SEND ) Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1182167 Share on other sites More sharing options...
jroryl Posted March 4, 2011 Author Share Posted March 4, 2011 Could someone tell me why the below print_r($arr2[1]); as the correct value that I'm expecting but when I try to echo $arr2[1]; the input doesn't post the value to the form, it's empty? <?php include('../../../config.php'); $result=mysql_query("SELECT * FROM products"); ?> <form method="POST" action="product.php"> <select name="item_name"> <? while($row_list=mysql_fetch_array($result, MYSQL_ASSOC)){ <option value="<? echo $row_list['product']; ?>|<? echo $row_list['price']; ?>" { echo "selected"; } ?><? echo $row_list['title']; ?> $ <? echo $row_list['price']; ?></option> <? } $arr2 = explode("|",$_GET['item_name']); ?> </select> ?> <input type="hidden" name="amount" value="<? echo $arr2[1]; ?>"/> //THIS IS EMPTY? <input type="submit" name="submit" value="SEND"/> </form> <? print_r($arr2[1]); // Close database connection. mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183049 Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 Can you paste in the result of the print_r($arr2[1]); here, please? Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183050 Share on other sites More sharing options...
jroryl Posted March 4, 2011 Author Share Posted March 4, 2011 14.50 Thank you taking a look ... Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183052 Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 And the hidden field doesn't show any value if you do a view-->source either? Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183053 Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 While we're at it, you might as well change the print_r() to a var_dump() and post that output as well. Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183055 Share on other sites More sharing options...
jroryl Posted March 4, 2011 Author Share Posted March 4, 2011 interesting, if I do View-->Source I do see the value ... I guess I was expecting it to be passed with the form data, so it would show up in the URL (if that makes sense) Here's the dump: (learned something new, didn't know about this dump thing array(2) { [0]=> string( "Product1" [1]=> string(5) "14.50" } Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183057 Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 Well, it would show up in the URL if the form method was GET rather than POST . . . Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183061 Share on other sites More sharing options...
jroryl Posted March 4, 2011 Author Share Posted March 4, 2011 I changed my form back to this: (oops) <form method="GET" action="product.php"> and I'm getting this in the URL but source and dump have the 14.50 for the input name"amount" http://www.blahblah.com/sample2.php?item_name=Product1%7C14.50&amount=&submit=SEND Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183064 Share on other sites More sharing options...
jroryl Posted March 5, 2011 Author Share Posted March 5, 2011 Actually this .. http://www.blahblah.com/product.php?item_name=Product1%7C14.50&amount=&submit=SEND Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183077 Share on other sites More sharing options...
jroryl Posted March 5, 2011 Author Share Posted March 5, 2011 I know what the problem is but not sure how to fix it. If I click submit the first time and I get an empty input amount value but if I click on submit again I get the previous amount, it's always getting the previous amount. How do I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/229050-dynamic-dropdown-question-submit-2-values-from-dropdown-to-a-form/#findComment-1183093 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.