Jump to content

Dynamic Dropdown question, submit 2 values from dropdown to a form??


jroryl

Recommended Posts

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>

Link to comment
Share on other sites

[*]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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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] => address@email.com [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] => address@email.com [item_number] => [return] => http://www.DOMAIN.com/thankyou [currency_code] => USD [undefined_quantity] => 1 [no_note] => 1 [bn] => PP-BuyNowBF [add] => 1 [submit] => SEND )

 

 

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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

 

array(2) { [0]=> string(8) "Product1" [1]=> string(5) "14.50" }

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.