leighhobson89 Posted February 18, 2007 Share Posted February 18, 2007 This is a university assignment, and, being quite a novice at PhP, I might seem silly. The situation is that I am creating a shopping cart for a sports shop, and one of the screens, shopping.php, collects orders from users by way of a 4-way drop down menu. (namely sporttype, product, colour, and size) There is an SQL database that the page accesses for the menu items, (dynamic) and, when an item is selected, the onchange() command runs a javascript function to reload the page with a new SQL query, thus refining the list down. When each item is selected, the script adds the primary keys ID to the URL. Eventually there will be a box at the bottom of the page to "catch" all this information, before the user selects "checkout" to submit the order. Problem: Right, here we go - the problem with the page is that it defaults the sport type to one particular selection, and, although it allows you to select a different one, and correctly writes this in the URL, when the next field is selected, the first one changes back again, and this seems to be the case with all of the fields. Can anyone help?! Script: <!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=iso-8859-1" /> <title>Lets Go Shopping!!</title> <SCRIPT language=JavaScript> function reload(form) { var val=form.sport.options[form.sport.options.selectedIndex].value ; self.location='shopping.php?sport=' + val ; } function reload2(form) { var val=form.sport.options[form.sport.options.selectedIndex].value ; var val2=form.product.options[form.product.options.selectedIndex].value ; self.location='shopping.php?sport=' + val +'&product=' + val2 ; } function reload3(form) { var val=form.sport.options[form.sport.options.selectedIndex].value ; var val2=form.product.options[form.product.options.selectedIndex].value ; var val3=form.colour.options[form.colour.options.selectedIndex].value ; self.location='shopping.php?sport=' + val +'&product=' + val2 + '&colour=' + val3 ; } function reload4(form) { var val=form.sport.options[form.sport.options.selectedIndex].value ; var val2=form.product.options[form.product.options.selectedIndex].value ; var val3=form.colour.options[form.colour.options.selectedIndex].value ; var val4=form.size.options[form.size.options.selectedIndex].value ; self.location='shopping.php?sport=' + val +'&product=' + val2 + '&colour=' + val3 + '&size=' + val4 ; } </script> </head> <body> <p>Lets Go Sports Shopping!</p> <?php $connection = mysql_pconnect("213.171.218.248", "lpt", "lpt"); mysql_select_db("TE3888",$connection); $sql = 'SELECT links FROM lptLptLinks'; if(!$result = mysql_query($sql,$connection)) echo "ERROR. SQL is '".$sql."'\n"; else { echo "<table border='1'>\n<tr>"; $numberfields = mysql_num_fields($result); echo "</tr>\n"; $Row = mysql_fetch_row($result); while($Row) { echo "<tr>"; for ($i=0; $i<$numberfields ; $i++ ) echo "<td><a href='$Row[$i].php'>$Row[$i]</a></td>"; echo "</tr>\n"; $Row = mysql_fetch_row($result); } echo "</table>\n"; } mysql_close($connection); ?> <p>Please Browse around at your free will, by selecting a department from the following links, and by choosing a product from the menus that follow:</p> <?php $connection = mysql_pconnect("213.171.218.248", "lpt", "lpt"); mysql_select_db("TE3888",$connection); @$sport=$HTTP_GET_VARS['sport'] ; $quer2=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`SportId` FROM `lptSpShStockPrice` ORDER BY `lptSpShStockPrice`.`SportId` ASC"); if(isset($sport) and strlen($sport) > 0) { $quer=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`CatalogNumber` FROM `lptSpShStockPrice` WHERE SportID=$sport ORDER BY `lptSpShStockPrice`.`CatalogNumber` ASC"); } else { $quer=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`CatalogNumber` FROM `lptSpShStockPrice` ORDER BY `lptSpShStockPrice`.`CatalogNumber` ASC"); } @$product=$HTTP_GET_VARS['product']; if(isset($product) and strlen($product) > 0){ $quer3=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`ColourId` FROM `lptSpShStockPrice` where CatalogNumber=$product ORDER BY `lptSpShStockPrice`.`ColourId` ASC"); } else { $quer3=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`ColourId` FROM `lptSpShStockPrice` ORDER BY `lptSpShStockPrice`.`ColourId` ASC"); } @$colour=$HTTP_GET_VARS['colour']; if(isset($colour) and strlen($colour) > 0){ $quer4=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`SizeId` FROM `lptSpShStockPrice` where ColourId=$colour ORDER BY `lptSpShStockPrice`.`ColourId` ASC"); } else { $quer4=mysql_query("SELECT DISTINCT `lptSpShStockPrice`.`SizeId` FROM `lptSpShStockPrice` ORDER BY `lptSpShStockPrice`.`SizeId` ASC"); } echo "<form method='post' name='shopform' action='cart.php'>"; echo "Sport: <select name='sport' onchange='reload(this.form)'> <option selected value=''>Select one</option>"; while($noticia2 = mysql_fetch_array($quer2)) { $sport6 = mysql_query("SELECT sportType FROM lptSpShCatalogSport WHERE SportId = $noticia2[sportId]"); $sport2 = mysql_fetch_array($sport6); if($noticia2['SportId']==$sport) { echo "<option selected value='$noticia2[sportId]'>$sport2[sportType]</option>"."<BR>"; } else { echo "<option selected value='$noticia2[sportId]'>$sport2[sportType]</option>"; } } echo "</select><BR /><BR />"; echo "Product: <select name='product' onchange='reload2(this.form)'> <option value=''>Select one</option>"; while($noticia = mysql_fetch_array($quer)) { $product6 = mysql_query("SELECT productName FROM lptSpshcatalogproduct WHERE CatalogNumber = $noticia[CatalogNumber]"); $product2 = mysql_fetch_array($product6); echo "<option selected value='$noticia[CatalogNumber]'>$product2[productName]</option>"; } echo "</select><BR /><BR />"; echo "Colour: <select name='colour' onchange='reload3(this.form)'> <option value=''>Select one</option>"; while($noticia3 = mysql_fetch_array($quer3)) { $colour6= mysql_query("SELECT productColour FROM lptSpShCatalogColour WHERE ColourId = $noticia3[ColourId]"); $colour2 = mysql_fetch_array($colour6); echo "<option value='$noticia3[ColourId]'>$colour2[productColour]</option>"; } echo "</select><BR /><BR />"; echo "Size: <select name='size' onchange='reload4(this.form)'> <option value=''>Select one</option>"; while($noticia4 = mysql_fetch_array($quer4)) { $size6= mysql_query("SELECT size FROM lptSpShCatalogSize WHERE SizeId = $noticia4[sizeId]"); $size2 = mysql_fetch_array($size6); echo "<option value='$noticia4[sizeId]'>$size2</option>"; } echo "</select><BR /><BR />"; echo "<input type=submit value=Submit>"; echo "</form>"; ?> <p>Created by Leigh Hobson 2006/2007</p> </body> </html> Thanks in advance if anyone can help!! Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/ Share on other sites More sharing options...
nloding Posted February 19, 2007 Share Posted February 19, 2007 Wow, why doesn't anyone used the code tags anymore? Or am I too new to complain? Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/#findComment-188323 Share on other sites More sharing options...
roopurt18 Posted February 19, 2007 Share Posted February 19, 2007 I didn't bother reading that huge mess of code you posted since it's not in [ code ] tags, but my guess would be you need to read the URL using $_SERVER['REQUEST_URI'] and set the selected attribute of the appropriate < option > items when building your select control. Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/#findComment-188333 Share on other sites More sharing options...
leighhobson89 Posted February 20, 2007 Author Share Posted February 20, 2007 sorry about the tag issue. I am a forum noob and had no idea. could you explain what you meant by the last post, setting the $_SERVER['REQUEST_URI'] as i am quite a novice at php too!! Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/#findComment-189350 Share on other sites More sharing options...
leighhobson89 Posted February 20, 2007 Author Share Posted February 20, 2007 obviously a noob Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/#findComment-189351 Share on other sites More sharing options...
nloding Posted February 20, 2007 Share Posted February 20, 2007 When using drop-down lists, there is a "selected='selected'" choice you put in the <option> tag. So basically you've got <option name="name" value="value" selected="selected" /> And whatever is "selected" will show as the first in the list, even if it's really in the middle of the drop-down. Google it. Right now, every option you output has "selected" in it, which does nothing for you. To get that set up, you need to run an if statement against your $_GET['sport'] or whatever variable to check the value. if($noticia2['SportId']==$sport) { echo "<option value='$noticia2[sportId]'"; if($noticia2[sportId] == $_GET['sport']) echo "selected='selected'"; echo ">".$sport2[sportType]."</option>"."<BR>"; } else { echo "<option value='$noticia2[sportId]'"; if($noticia2[sportId] == $_GET['sport']) echo "selected='selected'"; echo ">".$sport2[sportType]."</option>"; } That is just part of your code, but I hope you get it. If the value of the option equals the value javascript just passed to the URL, then that option is set as "selected='selected'". Otherwise, the "selected=" is left out entirely. You'd also have to put the if statement against your initial value ("Select One"), which would just be this: if(!$_GET['sport']) echo "selected='selected'"; BTW, it's proper to use "selected='selected'" if you're using the XHTML standard. Otherwise, just "selected" is fine if you're using HTML 4.01. Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/#findComment-189397 Share on other sites More sharing options...
leighhobson89 Posted March 16, 2007 Author Share Posted March 16, 2007 Thanks, sorted now! Link to comment https://forums.phpfreaks.com/topic/39091-php-meltdown/#findComment-208768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.