jay_bo Posted April 5, 2010 Share Posted April 5, 2010 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 Quote Link to comment Share on other sites More sharing options...
the182guy Posted April 5, 2010 Share Posted April 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 Sorry here is the form1..... <form name="form1"> <input type="hidden" name="productid" /> <input type="hidden" name="command" /> </form> Quote Link to comment Share on other sites More sharing options...
leehanken Posted April 5, 2010 Share Posted April 5, 2010 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"]; Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 Option A didn't work i'm afraid, however that looks the postive way of doing it seems that is how i am getting the product id...I don't get where you mean to paste the code from option B Sorry to be a pain Quote Link to comment Share on other sites More sharing options...
leehanken Posted April 5, 2010 Share Posted April 5, 2010 Post the full code. I may have used '$select' mistakenly, because I thought it was price when in fact it seems to be size. Both options will work if understood and implemented correctly. Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 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 Quote Link to comment Share on other sites More sharing options...
leehanken Posted April 5, 2010 Share Posted April 5, 2010 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? Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 yes you are correct Quote Link to comment Share on other sites More sharing options...
leehanken Posted April 5, 2010 Share Posted April 5, 2010 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(); Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 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. Quote Link to comment Share on other sites More sharing options...
leehanken Posted April 5, 2010 Share Posted April 5, 2010 Sorry, this is too much hard guess work, without seeing the code for the whole page, it is difficult to figure out what the page does and what you are trying to achieve. Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 shall i post the whole page code on here? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted April 5, 2010 Share Posted April 5, 2010 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'> Quote Link to comment Share on other sites More sharing options...
jay_bo Posted April 5, 2010 Author Share Posted April 5, 2010 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 Quote Link to comment 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.