hi im having problems with a script and im not sure whats going on being a relative newcomer to php
i created the script and managed to get it working how i want which can be found here
http://steven-green.co.uk/sqlstuff/productspost7.php
the only problem is that when the form in this script is submitted it is also reset.
After speaking to some people i was told i would have to use sessions and the fruits of todays labour can be seen in the code below and at
http://steven-green.co.uk/sqlstuff/productspost10.php
it is behanving really weird and im not sure why 🙁
anyway if someone has any tips that would get this working like the script from the first address then ill post them a mars bar.
<?
# start the session
session_start();
#connection
$dbc = mysql_connect('************' , 'mediacentre' , 'mediacentre') or die (mysql_error());
mysql_select_db ('mediacentredb', $dbc) or die (mysql_error());
# if the form is submitted run this part
if(isset($_POST['submit'])) {
$soundcards_value = explode("|", $_POST['soundCards']); # from the select menu called soundCards
# register the session variable for soundcard set to the product desc
$_SESSION['soundCards'] = $soundcards_value[1];
$_SESSION['soundCardsPrice'] = $soundcards_value[0];
# register the session variable for motherboards
$motherboards_value = explode("|", $_POST['motherboards']);
$_SESSION['motherboards'] = $motherboards_value[1];
$_SESSION['motherboardsPrice'] = $motherboards_value[0];
# register the session variable for cpu
$cpu_value = explode("|", $_POST['cpu']);
$_SESSION['cpu'] = $cpu_value[1];
$_SESSION['cpuPrice'] = $cpu_value[0];
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="33%" bgcolor="#F0F0F0"><form name="selectors" action="<? $_SERVER['PHP_SELF']; ?>" method="POST">
<font size="2" face="Verdana, Arial, Helvetica, sans-serif"> SoundCards
<select name="soundCards">
<!-- set the first option on the select menu to blank or SESSION variable if it has been registered -->
<? if(isset($_SESSION['soundCards'])) { ?>
<option selected="<? echo $_SESSION['soundCardsPrice']; ?>|<? echo $_SESSION['soundCards']; ?>"><? echo $_SESSION['soundCards']; ?> - £<? echo $_SESSION['soundCardsPrice']; ?></option>
<? } else { ?>
<option selected=""></option>
<? } ?>
<? $soundCards = mysql_query("select * from products where product_type='soundCards' order by product_id") or
die(mysql_error());
$rows_sc = mysql_fetch_assoc($soundCards);
do { ?>
<option value="<? echo $rows_sc['price']; ?>|<? echo $rows_sc['product_name']; ?>"><? echo $rows_sc['product_name']; ?>
- £<? echo $rows_sc['price']; ?></option>
<? } while($rows_sc= mysql_fetch_assoc($soundCards)); ?>
</select>
<br>
<br>
<br>
Motherboards
<select name="motherboards">
<!-- set the first option on the select menu to blank or SESSION variable if it has been registered -->
<? if(isset($_SESSION['motherboards'])) { ?>
<option selected="<? echo $_SESSION['motherboardsPrice']; ?>|<? echo $_SESSION['motherboards']; ?>"><? echo $_SESSION['motherboards']; ?></option>
<? } else { ?>
<option selected=""></option>
<? } ?>
<? $motherboards = mysql_query("select * from products where product_type='motherboards' order by product_id") or
die(mysql_error());
$rows_m = mysql_fetch_assoc($motherboards);
do { ?>
<option value="<? echo $rows_m['price']; ?>|<? echo $rows_m['product_name']; ?>"><? echo $rows_m['product_name']; ?>
- £<? echo $rows_m['price']; ?></option>
<? } while($rows_m= mysql_fetch_assoc($motherboards)); ?>
</select>
<br>
<br>
<br>
CPU's
<select name="cpu">
<? if(isset($_SESSION['cpu'])) { ?>
<option selected="<? echo $_SESSION['cpuPrice']; ?>|<? echo $_SESSION['cpu']; ?>"><? echo $_SESSION['cpu']; ?></option>
<? } else { ?>
<option selected=""></option>
<? } ?>
<? $cpu = mysql_query("select * from products where product_type='cpu' order by product_id") or
die(mysql_error());
$rows_cpu = mysql_fetch_assoc($cpu);
do { ?>
<option value="<? echo $rows_cpu['price']; ?>|<? echo $rows_cpu['product_name']; ?>"><? echo $rows_cpu['product_name']; ?>
- £<? echo $rows_cpu['price']; ?></option>
<? } while($rows_cpu= mysql_fetch_assoc($cpu)); ?>
</select>
<br>
<input type="submit" name="submit" value="submit">
</font>
</form></td>
<td width="67%" align="center" valign="top">
<? if(isset($_POST['submit'])) { ?>
<table width="500" border="0" cellspacing="2" cellpadding="0">
<tr bgcolor="#F0F0F0">
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Product</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Price</font></strong></td>
</tr>
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? echo $soundcards_value[1]; ?></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? echo $soundcards_value[0]; ?></font></td>
</tr>
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? echo $motherboards_value[1]; ?></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? echo $motherboards_value[0]; ?></font></td>
</tr>
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? echo $cpu_value[1]; ?></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? echo $cpu_value[0];?></font></td>
</tr>
<tr>
<td align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Total</strong></font></td>
<td bgcolor="#F0F0F0"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><? $total = $_SESSION['cpuPrice'] + $_SESSION['motherboardsPrice'] + $_SESSION['soundCardsPrice']; echo $total; ?></font></td>
</tr>
</table>
<? } ?>
<pre><? print_r($_SESSION); ?></pre>
</td>
</tr>
</table>
</body>
</html>
:p :p