Jump to content

Retrieving a value from a drop down box


jay_bo

Recommended Posts

When i press the button in the code below

 

		$result=mysql_query("SELECT * FROM products WHERE cat_id=$cat");
		while($row=mysql_fetch_array($result)){

echo '<br />';
echo '<div id="item_container">';
echo '<div id="item_picture"><a href="'.$row["picture_large"].'" class="thickbox" rel="album" ><img src="'.$row["picture"].'" border="0" alt="'.$row["image_alt"].'"></a></div>';
echo '<div id="item_title"><h2>'.$row["name"].'</h2></div>';
echo '<div id="item_text"><p>'.substr($row["description"], 0, 100).'</p><br /><br />';
echo '<select name="dropdown">';
$type=$row["type"];
$sql="SELECT * FROM products WHERE type=$type";
						$result2 =mysql_query($sql);
						while ($row=mysql_fetch_assoc($result2)){
echo'<option value =<"'.$row["serial"].'" >'.$row["price"].'</option>';
}
echo '</select>';
echo '<br /><br/>';
echo '<input type="button" value="Add to Cart" onclick="addtocart('.$row["serial"].')"/></span>';
		echo '</div>';
		echo '<br />';
		echo '<br />';
echo '</div>';
}

 

goes to this javascript code...

 

	<script language="javascript">
function addtocart(pid){
	document.form1.productid.value=pid;
	document.form1.command.value='add';
	document.form1.submit();

}
</script>	

 

Which then goes to this code...

 

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
	$pid=$_REQUEST['productid'];
	$select = $_POST['dropdown'];
	addtocart($pid,1);
	header("location:shoppingcart.php?size=$select");
	exit();
}

 

how would i get the drop down value from the first php code to the last php code, just above ^^

 

****Please note all this code is on the same page.

 

Thanks for any help

 

Link to comment
Share on other sites

Do you have a <form> tag? I see in the javascript you target 'form1' but I don't see the form tag.

 

In your last PHP snippet, you already access the value of the dropdown here

$select = $_POST['dropdown'];

 

However, this will only work if the form has a method of POST. If the method is GET then you'll need to use $_GET['dropdown'] instead, or $_REQUEST will also find it. From looking at the code, you use $_REQUEST to access the other form data, so this suggests to me that you're form is setup with the GET method.

Link to comment
Share on other sites

You could pass the drop down value as an extra parameter (option A) or you could look it up from the database in the final piece of code (option B)

 


 

option A

 

- create another hidden field in form1

 

	<input type="hidden" name="dropdown" />

 

- modify the first code

 

	echo '<input type="button" value="Add to Cart" onclick="addtocart('.$row["serial"].','.$row["price"].')"/></span>';

 

- modify the javascript

 

	function addtocart(pid,price){
..
	document.form1.dropdown.value=price;

 


 

option B

 

- add this to the final code

 

	$sql="SELECT * FROM products WHERE serial=$pid";
$result2 =mysql_query($sql);
$row=mysql_fetch_assoc($result2);
$select = $row["price"];

 

Link to comment
Share on other sites

Thanks for getting back to me....Right i changed and messed about with the coding my self and here it is

 

$result=mysql_query("SELECT * FROM products WHERE cat_id=$cat");
		while($row=mysql_fetch_array($result)){

echo'<form name="form1">';
		  	
				    echo'<input type="hidden" name="command" />';
echo '<br />';
echo '<div id="item_container">';
echo '<div id="item_picture"><a href="'.$row["picture_large"].'" class="thickbox" rel="album" ><img src="'.$row["picture"].'" border="0" alt="'.$row["image_alt"].'"></a></div>';
echo '<div id="item_title"><h2>'.$row["name"].'</h2></div>';
echo '<div id="item_text"><p>'.substr($row["description"], 0, 100).'</p><br /><br />';
echo '<select name="productid">';
$type=$row["type"];
$sql="SELECT * FROM products WHERE type=$type";
						$result2 =mysql_query($sql);
						while ($row=mysql_fetch_assoc($result2)){
echo'<option value ='.$row["serial"].'>'.$row["price"].'</option>';
}
echo '</select>';
echo '<input type="button" value="Add to Cart" onclick="addtocart('.$row["serial"].')"/></span>';
echo '<br /><br/>';

		echo '</div>';
		echo '<br />';
		echo '<br />';
echo '</div>';
}
}
echo '</form>';

 

Which once the button is pressed goes to javascript....

<script language="javascript">
function addtocart(pid){
	document.form1.productid.value=pid;
	document.form1.command.value='add';
	document.form1.submit();

}
</script>

 

Which looks at form1 tag which i have added to the php code above and renamed the dropdown list as productid ^^ then the javascript code should go to....

 

if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
	$pid=$_GET['productid'];
	addtocart($pid,1);
	header("location:shoppingcart.php");
	exit();
}

 

As i mentioned before that i had added the form1 tag to the php code and changed the dropdown list name, actually adds products to the checkout but however because it is looping it also adds other products... this is the url i see.....

 

http://localhost%20Site/orderonline.php?command=&productid=98&command=&productid=99

 

If i changed it and got rid of product 2 so it looked like this

 

http://localhost%20Site/orderonline.php?command=&productid=98

 

then it would add to the basket fine.........I just need it to stop adding the other products to the url and i know this is because i have added it to the loop so that the form1 tag is around every product that gets displayed.

 

 

Thanks

Link to comment
Share on other sites

Okay, so just to clarify, when you say you want the 'value' of the drop down list it is not the price which is displayed in the drop-down that you want in the final section, it is the product_id / serial number that you want to use. Is this correct?

Link to comment
Share on other sites

Okay, one evident problem is that the code for the form (posted above) does not have an associated 'action', or a 'method'

 

It should say

 

<form name='form1' action='myscript.php' method='post'>

 

or something like that. Otherwise the javascript does nothing:

 

document.form1.submit();

 

 

Link to comment
Share on other sites

Okay, one evident problem is that the code for the form (posted above) does not have an associated 'action', or a 'method'

 

It should say

 

<form name='form1' action='myscript.php' method='post'>

 

or something like that. Otherwise the javascript does nothing:

 

document.form1.submit();

 

That is because all that code is on one php page.

Link to comment
Share on other sites

Okay, one evident problem is that the code for the form (posted above) does not have an associated 'action', or a 'method'

 

It should say

 

<form name='form1' action='myscript.php' method='post'>

 

or something like that. Otherwise the javascript does nothing:

 

document.form1.submit();

 

That is because all that code is on one php page.

 

It doesn't matter if the code is all on one page, once it goes to the browser, the browser doesn't know what to do when the form submits if there's no form action or form method. You still need to do what leehanken suggests:

 

<form name='form1' action='myscript.php' method='post'>

Link to comment
Share on other sites

Okay, one evident problem is that the code for the form (posted above) does not have an associated 'action', or a 'method'

 

It should say

 

<form name='form1' action='myscript.php' method='post'>

 

or something like that. Otherwise the javascript does nothing:

 

document.form1.submit();

 

That is because all that code is on one php page.

 

It doesn't matter if the code is all on one page, once it goes to the browser, the browser doesn't know what to do when the form submits if there's no form action or form method. You still need to do what leehanken suggests:

 

<form name='form1' action='myscript.php' method='post'>

 

Yes i see what you mean, tomorrow i will make the form post and copy the code onto a new page.

 

Thanks for your help guys

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.