Hi,

Fogive me if my subject line doesnt really explain what I am doing well enough...but here is the problem. I am trying to build a very simple shopping cart. I want to select products by using a pull down and hitting submit. I am populating the pulldown using values from a MySQL query. Here is the code:


<body>
<form name="form1">
	<input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>
<div align="center">
	<h1 align="center">Products</h1><br />

<table border="0" cellpadding="2px" width="600px">
	 <select name="gfd" id="gfd">
	 <?
		$result=mysql_query("select * from products");
		while($row=mysql_fetch_array($result)){
	?>
 <option value=<?=$row['serial']?>><?=$row['name']?></option>
<?
}
?>
</select>

<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
			</td>
		</tr>
        <tr><td colspan="2"><hr size="1" /></td>

</table>
</div>
</body>


My issue is each product in the database as a name, price, and serial number and I need to get the value of the selected items SERIAL outside of the loop and into the onclick line.

<input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />

What is the best way to have the user select a product from the pulldown and have the serial value of that product in the database to be reported outside of the loop so that I have that value be passed in onclick?

Hope I explained well. Thank you in advance for any advice.....

    Well, the first step is obviously to send whatever SERIAL the user select to the server.

    What does your [font=monospace]addtocart()[/font] method do? Are you using AJAX?

      addtocart is a function:

      function addtocart($pid,$q){
      		if($pid<1 or $q<1) return;
      
      	if(is_array($_SESSION['cart'])){
      		if(product_exists($pid)) return;
      		$max=count($_SESSION['cart']);
      		$_SESSION['cart'][$max]['productid']=$pid;
      		$_SESSION['cart'][$max]['qty']=$q;
      	}
      	else{
      		$_SESSION['cart']=array();
      		$_SESSION['cart'][0]['productid']=$pid;
      		$_SESSION['cart'][0]['qty']=$q;
      	}
      }
      function product_exists($pid){
      	$pid=intval($pid);
      	$max=count($_SESSION['cart']);
      	$flag=0;
      	for($i=0;$i<$max;$i++){
      		if($pid==$_SESSION['cart'][$i]['productid']){
      			$flag=1;
      			break;
      		}
      	}
      	return $flag;
      }
      

        That looks like a PHP function; the addtocart() function in the HTML code you posted must be a Javascript function. PHP is a server-side language.

        As traq mentioned, you'll have to send the value to the server. One way to do this is to send an AJAX request (using Javascript) to a PHP script on your server.

          nope; [font=monospace]addtocart()[/font] (as you are calling it) is a javascript method - your PHP function will never be called that way.

          PHP works first, on your server. When it is done, it sends its output to the browser.

          HTML, JavaScript, etc., work later, on the user's computer. PHP is long gone by this point. Javascript has no idea it ever existed.

          If you want the user to select something and have PHP use it, you need to send the user's choice back to the server first. That's what forms are for. An good alternative in this case is to use AJAX to send the selection to the server, let PHP process it, and receive the result.

            OK. I think I understand a little better. I've never used AJAX so I decided that I could have the form post to itself and call the variable from the form into the function like this:

            <?
            	include("includes/db.php");
            	include("includes/functions.php");
            
            if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
            	$pid=$_REQUEST['productid'];
            	addtocart($pid,1);
            	header("location:shoppingcart.php");
            	exit();
            }
            ?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Products</title>
            <script language="javascript">
            	function addtocart(pid){
            		document.form1.productid.value=pid;
            		document.form1.command.value='add';
            		document.form1.submit();
            	}
            </script>
            </head>
            
            
            <body>
            <form name="form1" action="products.php" method="post">
            	<input type="hidden" name="productid" />
                <input type="hidden" name="command" />
            
            <div align="center">
            	<h1 align="center">Products</h1><br />
            
            <table border="0" cellpadding="2px" width="600px">
            	 <select name="gfd" id="gfd">
            	 <?
            		$result=mysql_query("select * from products");
            		while($row=mysql_fetch_array($result)){
            
            	?>
             <option value="<?=$row['serial']?>"><?=$row['name']?></option>
            <?
            }
            ?>
            </select>
            
            <INPUT type="submit" value="Send">
            </form>
            
            <?
            
            echo $_POST['gfd'];
            
               if($_SERVER['REQUEST_METHOD']=='POST')
               {
                  addtocart($_POST['gfd']);
               } 
            ?>
            
            		</td>
            	</tr>
                <tr><td colspan="2"><hr size="1" /></td>
            
            </table>
            </div>
            </body>
            </html>

            I get the error:

            Warning: Missing argument 2 for addtocart(), called in products.php on line 57 and defined in functions.php on line 34

            I'm not sure what other argument it is looking for....any idea's on improving my method? I think I'm fairly close.

            Thanks

              When posting PHP code, please use the board's [noparse]

              ..

              [/noparse] bbcode tags as they make your code much easier to read and analyze.

              sgtpepper;11027341 wrote:

              I'm not sure what other argument it is looking for

              Going back to the code snippet you posted previously:

              sgtpepper;11027323 wrote:

              addtocart is a function:

              function addtocart($pid,$q){
              

              we see that the PHP function addtocart() requires two arguments when called; $pid and $q.

                PERFECT!!! SOLVED!! Thank you.

                $q is looking for a quantity value...I added that...then changed the HEADER INFO to

                
                
                   if($_SERVER['REQUEST_METHOD']=='POST')
                   {
                      addtocart($_POST['gfd'],1);
                	  header("location:shoppingcart.php");
                	  exit();
                   } 
                
                

                  Looks to me like all you have done is recreate what a form does - might be simpler to use standard form submit and dump the javascript.

                    23 days later

                    I tried sgtpepper's code, but I am facing one error. Is there any other solution available?

                      annaharris;11028525 wrote:

                      I tried sgtpepper's code, but I am facing one error. Is there any other solution available?

                      Rather than hijack someone else's thread, you should start your own. In it, show us the code you're using and describe the problem you're having as well as what you've tried so far to resolve it.

                        Write a Reply...