Removing array [0] in SESSION array error - php

As a novice in this and I'm struggling with a small project.
I’m putting together a page for INTERNAL orders in a small MTB club. My problem is if there are more than 1 order in the $_SESSION['cart'] and I try to remove the first one [0], then I get “Undefined offset: 0”.
I get that is to do with that I remove the first entry and therefor it can’t be found when it lists the array. I just can’t see how I solve it, I have googled and tried different solutions without result.
Please advice on how to get forward for me..
Below code is made for this purpose only
enter code here <?php
session_start();
if(isset($_POST['submit'])){
$total = $_POST['Amount'] * $_POST['Price'];
$cart=array(
'Amount'=>$_POST['Amount'], //Amoun ordered of clothes
'Size'=>$_POST['Size'], //Size of clothes
'Price'=>$_POST['Price'], //Price of clothes
'Product_id'=>$_POST['Product_id'],//Id of clothes for DB
'Product_Name'=>$_POST['Product_Name'], //Name of clothes
'Product_Total'=>$total //Total price
);
$_SESSION['cart'][]=$cart;
}
if(isset($_GET['Reset'])){
unset($_SESSION['cart']);
header('location:demo.php');
}
if(isset($_GET['remove'])){
$do = $_GET['do'];
unset($_SESSION['cart'][$do]);
//Redirecting After Unset SESSION
header('location:demo.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Session Demo/Test</title>
</head>
<body>
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center" >
<tbody>
<tr>
<td width="373">Name of clothes</td>
<td width="185">
Reset Session</td>
</tr>
</tbody>
</table>
<form method="POST">
<table width="590" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td colspan="4" rowspan="5" align="center"><img src="images/Bike_Jersy_short.jpg" width="200" height="200" alt=""/></td>
<td height="120" colspan="2">
Clothes description
</td>
</tr>
<tr>
<td width="112">Price:</td>
<td>300,00 Kr.</td>
</tr>
<tr>
<td width="112">Amount:</td>
<td width="116">
<input name="Amount" type="number" id="Amount" tabindex="1" value="" size="1" required >
</td>
</tr>
<tr>
<td>Size:</td>
<td>
<select name="Size" id="Size" tabindex="2" required>
<option value=""></option>
<option value="XS">XS</option>
<option value="S">S</option>
<option value="L">L</option>
<option value="XL">XL</option>
<option value="2XL">2XL</option>
<option value="3XL">3XL</option>
<option value="4XL">4XL</option>
<option value="5XL">5XL</option>
<option value="6XL">6XL</option>
</select>
</td>
</tr>
<tr>
<td>Add:</td>
<td>
<input type="submit" name="submit" id="submit" value="Add">
<input name="Price" type="hidden" id="Price" value="300">
<input name="Product_id" type="hidden" id="Product_id" value="1">
<input name="Product_Name" type="hidden" id="Product_Name" value="Name of clothes">
</td>
</tr>
</tbody>
</table>
</form>
<p></p>
<?php
if(empty($_SESSION['cart'])){
?>
<table width="898" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td>You have no items in yet </td>
</tr>
</tbody>
</table>
<?php
}
else{
?>
<table width="900" border="4" cellspacing="0" cellpadding="5" align="center">
<tbody>
<tr>
<td width="109">Name</td>
<td width="31">Amount</td>
<td width="33">Size.</td>
<td width="63">Price</td>
<td width="55">Total</td>
<td width="45">Remove</td>
</tr>
<?php for($i = 0 ; $i < count($_SESSION['cart']) ; $i++) {
?>
<tr>
<td><?php echo $_SESSION['cart'][$i]['Product_Name'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Amount'];?>
</td>
<td><?php echo $_SESSION['cart'][$i]['Size'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Price'];?></td>
<td><?php echo $_SESSION['cart'][$i]['Product_Total'];?></td>
<td>Remove
<?php
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>

Use the array_shift() function.
Like so:
$value = array_shift($_SESSION['cart']);
This will remove the first element in the array regardless of the current index. $value is the value of the element that was removed.

Related

Save multiple field in db from dropdown selection

this is what i successfully done.
User key in the data into the text field and preview back the data.
this is what i failed to do:
capture back the data from db because the data that i select from drop down didn't store in the db. thats why i cannot print back the page after clicked submit.
it works well for other field but not for my drop down selection. is there somethings wrong with my code for this part?
this is my index.html file
<tr>
<td colspan="4" align="left" class="form-group form-inline"><b>Section
B</b></td>
</tr>
<script type="text/javascript">
function show() {
var dropdown = document.getElementById("employment");
var current_value =
dropdown.options[dropdown.selectedIndex].value;
if(current_value =="--Choose one--"){
didfv3.style.display = 'none';
didfv2.style.display = 'none';
}
if(current_value == "Unemployed"){
didfv3.style.display = 'none';
didfv2.style.display = '';
}
if(current_value == "Working"){
didfv3.style.display = '';
didfv2.style.display = 'none';
}
}
</script>
<td>employment</td>
<td colspan="4">
<select name="employment" id="employment" onChange="show();">
<option value="--Choose one--">--Choose one--</option>
<option value="Unemployed">Unemployed</option>
<option value="Working">Working</option>
</select>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td width="50%">Balance</td>
<td width="1%" align="center">:</td>
<td width="73%" ><input type="text" name="balance" readonly maxlength="10"
value="<?php echo $balance?>" size="5" > months</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="left"
style="display:none">
<tr>
<td class="form-group form-inline">Basic Salary</td>
<td width="0.5%" align="knk" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="basic" type="text" id="basic" maxlength="20"/></td>
</tr>
<tr>
<td>Gross Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="gross" type="text" id="gross" maxlength="20"/></td>
</tr>
<tr>
<td class="form-group form-inline">Nett Salary</td>
<td width="0.5%" align="center" class="form-group form-inline">:</td>
<td width="73%" class="form-group form-inline">
<input name="nett" type="text" id="nett" maxlength="20"/></td>
</tr>
<tr>
</table>
here is my insert.php
<?
$employment = $_POST['xxx'];
if (isset($_POST['xxx'])) {
$employment =$_POST['xxx'];
}
if($employment =='Working'){
$basic = $_POST['basicsalary00'];
$gross = $_POST['grosssalary00'];
$nett = $_POST['nettsalary00'];
}
elseif($xxx =='Unemployed'){
$balance = $_POST['balance'];
}
$sqlinsert = "INSERT INTO application
(
xxx,basicsalary00,grosssalary00,nettsalary00)
VALUES(0,'$xxx','$basic','$gross','$nett')";
?>
// here is to print back the output
<form name="formName" action="form1" method="post" class="form-group form-
inline">
<tr>
<td rowspan="2">Employment</td>
<td colspan="3">
<option value="<?php echo $employment;?>"><?php echo $employment;?>
</option>
<table id="didfv2" width="90%" border="0" align="center"
style="display:none">
<tr>
<td colspan="3">
<input type="hidden" id="balance" value="<?php echo $balance;?>" />
</td>
</tr>
</table>
<table id="didfv3" width="90%" border="0" align="center"
style="display:none">
<tr></tr>
<tr>
<td>Basic Salary</td>
<td>
<input name="basic" type="hidden" id="basic" value="<?php echo $basic;?>" />RM <?php echo $basic;?></td>
</tr>
<tr>
<td>Gross Salary</td>
<td>
<input name="gross" id="gross" type="hidden" value="<?php echo $gross;?>"
/>RM <?php echo $gross;?></td>
</tr>
<tr>
<td>Nett Salary</td>
<td>
<input name="nett" type="hidden" id="nett" value="<?php echo $nett;?>"
/>RM <?php echo $nett;?></td>
</tr>
</table>
</td>
</tr>
</form>
So there will be 4 pages,
1) for user input (index.html)
2) Preview back the input (i didnt include the file in here - successful)
3) Insert the input to db (insert.php)
4) Print back the result (code at the bottom of the insert.php...fetch from db- which i failed. Working for other field but not for drop down section)
I really need your help.. i've been stuck here for a week.... thank you
A proper dropdown should have a <select> and the <option> elements should be inside it!
In your insert.php, I can see that you have only used this line:
<option value="<?php echo $employment;?>"><?php echo $employment;?></option>
How about your try it like this:
<select readonly>
<option value="--Choose one--">--Choose one--</option>
<option <?php echo ($employment == 'Unemployed') ? 'selected' : ''; ?> value="Unemployed">Unemployed</option>
<option <?php echo ($employment == 'Working') ? 'selected' : ''; ?> value="Working">Working</option>
</select>
Here, we are checking whether any of those <option> element is same as the one that the user has submitted. If so, make it as selected
Hope it helps.
EDIT
If you want to store the value of $employment to display it later, you definitely have to store it in your db.

MySql php data display in tables

I am doing a simple set of PHP scripts to edit and return MySQL records from a web site.
Everything works fine but there is a cosmetic that I just cannot seem to correct.
I presume being very rusty I am missing something obvious - I have tried everything I can think of though.
The content of field ADDTEXT can be fairly large and i would like to word wrap it all into the table cell. This script truncates it when a single line length is exceeded.
And yes I know I should be using mysqli_... but I am deaeling with that !
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>EDIT NEWS ITEM</td>
</tr>
<tr>
<td>
<table style="width:100%">
<tr>
<?
$id=$_GET['id'];
include "D***************.uk\public_html\html\ConnectDB.php";//database connection
$order = "SELECT * FROM st¬¬¬¬¬¬¬ where TYPE = '".$id."'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<? echo "$row[TITLE]"?>">
<tr>
<td>Item Title</td>
<td>
<input type="text" name="title"
value="<? echo "$row[TITLE]"?>">
</td>
</tr>
<tr>
<td>Item Text</td>
<td>
<input type="text" name="text"
value="<? echo "$row[ADDTEXT]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
Change it to textarea field.
<textarea name="text"><?php echo $row['ADDTEXT'];?></textarea>
Try it
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>EDIT NEWS ITEM</td>
</tr>
<tr>
<td>
<table style="width:100%">
<tr><td>
<?
$id=$_GET['id'];
include "D***************.uk\public_html\html\ConnectDB.php";//database connection
$order = "SELECT * FROM st¬¬¬¬¬¬¬ where TYPE = '".$id."'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row['TITLE']?>">
<tr>
<td>Item Title</td>
<td>
<input type="text" name="title"
value="<?php echo $row['TITLE']?>">
</td>
</tr>
<tr>
<td>Item Text</td>
<td>
<input type="text" name="text"
value="<?php echo $row['ADDTEXT']?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>

HTML Tables not visible, from PHP code

I am attempting to make part of a webpage, that basically shows what groups a user has joined. I want to put one table for the form 'makegroup' and another table for the 'groups owned', both these tables are part of the row of another bigger table. But however much I try the 'makegroup' is not getting displayed, and 'groupsowned' is taking its place completely. Any idea why this is happening? Should I post code?
<table width="90%" height="125" >
<tr bgcolor="#CCCCCC"><td height="20" colspan="2"><h4>Groups</h4>
</td></tr>
<tr bgcolor="#CCCCCC">
<?
if($accountid == $_SESSION['userid'])
{
?>
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="#CCCCCC"> <tr valign="top">
<td colspan="3"><h5>Create Groups</h5></td></tr>
<tr><td>
<input align="absmiddle" type="text" maxlength="25" name="gname" /><? if($nog==1) {echo "<br /><font color=red>Please enter a valid groupname</font>";}?></td>
<td><select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select></td><td><input type="submit" name="makegroup" value="create group"/></td></tr>
</table>
</form>
</td>
<?
}
$grp= "SELECT * from groups where accountid='". $accountid."'";
$rest = mysql_query($grp,$connection);
$cont=0;
?>
<td width="50%" >
<table width="100%" height="20%" bgcolor="#CCCCCC" >
<tr><td colspan="10"><h5>Groups Owned</h5>
</td></tr>
<?
while($grow=mysql_fetch_array($rest))
{
$cont=$cont+1;
$gid=$grow['groupid'];
$gnam=$grow['groupname'];
$gcreator=$grow['accountid'];
if($cont==0)
{
echo "<tr>";
}
echo "<td align='left' ><a href='groups_discussions.php?id=".$gid."'><font color="."#333333"." size='-1'>".$gnam."</font></a></td>";
if($cont==5)
{echo "</tr>";}
}
?>
</table>
</td>
</tr></table>
I want to know if everything looks all right to you guys. I can;t see anything wrong, but the fact is that the form is not getting displayed :(
You mean like this?
<table width="90%" height="125" bgcolor="orange">
<tr bgcolor="#CCCCCC"><td height="20" colspan="2"><h4>Groups</h4></td></tr>
<tr bgcolor="#CCCCCC">
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="green">
<tr valign="top">
<td colspan="3"><h5>Create Groups</h5></td>
</tr>
<tr bgcolor="blue">
<td bgcolor="orange">
<input align="absmiddle" type="text" maxlength="25" name="gname" />
<?php
$nog = 1;
if($nog == 1)
echo "<br /><font color=red>Please enter a valid groupname</font>";
?>
</td>
<td bgcolor="yellow">
<select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select>
</td>
<td bgcolor="red">
<input type="submit" name="makegroup" value="create group"/>
</td>
</tr>
</table>
</form>
</td>
<td width="50%">
<table width="100%" height="20%" bgcolor="pink" >
<tr><td colspan="10"><h5>Groups Owned</h5></td></tr>
<?php
//$grp= "SELECT * from groups where accountid='". $accountid."'";
//$rest = mysql_query($grp, $connection);
$rest = array(
array('groupid' => '123', 'groupname' => 'testname', 'accountid' => '456'),
array('groupid' => '678', 'groupname' => 'testname2', 'accountid' => '999')
);
$cont = 0;
foreach ($rest as $grow)
{
$gid = $grow['groupid'];
$gnam = $grow['groupname'];
$gcreator = $grow['accountid'];
if($cont == 0)
echo "<tr>";
echo "<td align='left' ><a href='groups_discussions.php?id=" . $gid . "'><font color="."#333333"." size='-1'>" . $gnam . "</font></a></td>";
if($cont == 1)
echo "</tr>";
$cont = $cont + 1;
}
?>
</table>
</td>
</tr>
</table>
You had a pile of problems in your code, such as only being able to handle a single set of 5 results in the second table, incrementing a counter (so it's minimum value would always be 1) and then testing to see if it was zero, etc.
<table width="90%" height="125" >
<tr bgcolor="#CCCCCC">
<td height="20" colspan="2">
<h4>Groups</h4>
</td>
</tr>
<tr bgcolor="#CCCCCC">
<?
if( isset( $_SESSION['userid'] ) && $accountid==$_SESSION['userid'] ){
?>
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="#CCCCCC">
<tr valign="top">
<td colspan="3">
<h5>Create Groups</h5>
</td>
</tr>
<tr>
<td>
<input align="absmiddle" type="text" maxlength="25" name="gname" />
<?
if( $nog==1 ){
?>
<br /><font color="red">Please enter a valid groupname</font>
<?php
}
?>
</td>
<td>
<select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select>
</td>
<td>
<input type="submit" name="makegroup" value="create group" />
</td>
</tr>
</table>
</form>
</td>
<?
}
$grp = "SELECT * from groups where accountid='{$accountid}'";
$rest = mysql_query( $grp , $connection );
$cont = 0;
?>
<td width="50%" >
<table width="100%" height="20%" bgcolor="#CCCCCC" >
<tr>
<td colspan="10">
<h5>Groups Owned</h5>
</td>
</tr>
<?
if( mysql_num_rows( $rest )>0 ){
while( $grow = mysql_fetch_array( $rest ) ){
$gid = $grow['groupid'];
$gnam = $grow['groupname'];
$gcreator = $grow['accountid'];
if( ( $cont%5 )==0 )
echo '<tr>';
echo "<td align='left' ><a href='groups_discussions.php?id={$gid}'><font color='#333333' size='-1'>{$gnam}</font></a></td>\n";
if( ( $cont%5 )==4 )
echo '</tr>';
$cont++;
}
}else{
echo '<tr><td colspan="10">No Records</td></tr>';
}
?>
</table>
</td>
</tr>
</table>
Might help.
Check whether this condition is matching or not: if($accountid == $_SESSION['userid'])
If it doesn't matches, then the form for makegroup won't be visible.
I have tidied up your code a bit for better readability:
<table width="90%" height="125" >
<tr bgcolor="#CCCCCC">
<td height="20" colspan="2"><h4>Groups</h4></td>
</tr>
<tr bgcolor="#CCCCCC">
<?
if($accountid == $_SESSION['userid'])
{
?>
<td width="50%">
<form name="groupscreate" method="post" onsubmit="return Confirm()">
<table width="99%" height="6%" bgcolor="#CCCCCC">
<tr valign="top">
<td colspan="3"><h5>Create Groups</h5></td>
</tr>
<tr>
<td>
<input align="absmiddle" type="text" maxlength="25" name="gname" />
<? if($nog==1) {echo "<br /><font color=red>Please enter a valid groupname</font>";}?>
</td>
<td>
<select name="gcategory">
<option value="General" selected>General</option>
<option value="Comedy">Comedy</option>
<option value="Education">Education</option>
<option value="Entertainment">Entertainment</option>
<option value="Gaming">Gaming</option>
<option value="Music">Music</option>
<option value="Science & Technology">Science & Technology</option>
<option value="Sports">Sports</option>
</select>
</td>
<td>
<input type="submit" name="makegroup" value="create group"/>
</td>
</tr>
</table>
</form>
</td>
<?
}
$grp= "SELECT * from groups where accountid='". $accountid."'";
$rest = mysql_query($grp,$connection);
$cont=0;
?>
<td width="50%" >
<table width="100%" height="20%" bgcolor="#CCCCCC" >
<tr>
<td colspan="10"><h5>Groups Owned</h5></td>
</tr>
<?
while($grow=mysql_fetch_array($rest))
{
$cont=$cont+1;
$gid=$grow['groupid'];
$gnam=$grow['groupname'];
$gcreator=$grow['accountid'];
if($cont==0)
echo "<tr>";
echo "<td align='left' ><a href='groups_discussions.php?id=".$gid."'><font color="."#333333"." size='-1'>".$gnam."</font></a></td>";
if($cont==5)
echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>
Few things:
1) This part...
$cont=$cont+1;
$gid=$grow['groupid'];
$gnam=$grow['groupname'];
$gcreator=$grow['accountid'];
if($cont==0)
{
echo "<tr>";
}
...will never fire the if statement since on the first iteration $cont=1. ($cont=0+1=1)
2) You need to reset $cont after you close your if($cont==5){echo "</tr>";}} so the top section of the while statement creates a new <tr> tag.

php - drop down from database

Im a newbie ih php.Im trying to show drop down list values from database using this but the data not shown up.There is no error as well. Need some experts advice.
Code
<?php
include('connectdb.php');
$sql="SELECT id,name FROM companydetailstbl";
$result=mysql_query($sql);
$options="";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
//echo $id;
$thing=$row["name"];
$options.="<OPTION VALUE=\"$id\">".$thing;
}
?>
<table width="900" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">Company Name </td>
<td width="3%">:</td>
<td width="75%"><SELECT NAME="thing">
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT></td>
</tr>
<tr>
<td>Installation Date </td>
<td>:</td>
<td><input type="text" name="ins_dt" />
(ddmmyyyy)</td>
</tr>
<tr>
<td>Expiry Date </td>
<td>:</td>
<td><input type="text" name="ex_dt" />
(ddmmyyyy)</td>
</tr>
<tr>
<td>Status</td>
<td>:</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Update" />
<input type="submit" name="Submit2" value="Reset" /></td>
</tr>
</table>
Please help. Thanks
I don't know what DTD you are currently using, but according to the HTML 4.0 Specification, you need to close your option tag.
$options .= '<option value="' . $id . '">' . $thing . '</option>';
Also there,
<select name="thing">
<option value="0">Choose</option>
<?php echo $options; ?>
</select>
please try to close the tag
$options.="<OPTION VALUE=\"$id\">".$thing."</OPTION>";
also edit
<SELECT NAME="thing">
<OPTION VALUE=0>Choose</OPTION>
<?=$options?>
</SELECT>
<?php
include('connectdb.php');
$sql="SELECT id,name FROM companydetailstbl";
$result=mysql_query($sql);
?>
<table width="900" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">Company Name </td>
<td width="3%">:</td>
<td width="75%">
<select name="thing">
<?php
while($row = mysql_fetch_object($result)){
?>
<option value="<?=$row["id"]; ?>"><?=$row["name"]; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Installation Date </td>
<td>:</td>
<td><input type="text" name="ins_dt" />(ddmmyyyy)</td>
</tr>
<tr>
<td>Expiry Date </td>
<td>:</td>
<td><input type="text" name="ex_dt" />(ddmmyyyy)</td>
</tr>
<tr>
<td>Status</td>
<td>:</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="submit" name="Submit" value="Update" />
<input type="submit" name="Submit2" value="Reset" />
</td>
</tr>
</table>
Hope it helps

Check drop down and create if statement based on that selection

I want to create a php variable depending on what was selected in a drop down.
This is the drop down:
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
Depending if "per hour" or "per annum" was selected, I want to do the following (I'm not quite sure syntax wise if this is correct and this part is on another page):
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
How can this be done?
Here's my full form:
<form action="<?php the_permalink(); ?>calculations" method="post">
<h2>Savings calculator</h2>
<div class="calculator-divide"></div>
<table border="0">
<tr>
<td colspan="3"><h3>Current service costs</h3></td>
</tr>
<tr>
<td width="440"><p>Pharmacist</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="22.00" type="text" name="pharmacist" />
</p></td>
<td width="5" rowspan="2"><select id="ddlTime">
<option selected="selected" value="perhour" name="perhour">per hour</option>
<option value="perannum" name="perannum">per annum</option>
</select></td>
</tr>
<tr>
<td><p>Dispenser / Technician</p></td>
<td><p style="padding-left:5px!IMPORTANT;">£
<input style="width:145px!IMPORTANT;" value="8.00" type="text" name="dispenser" />
</p></td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"><h3>Time taken to carry out manual dispensing tasks</h3></td>
</tr>
<tr>
<td><p>Measure 50mls dose by hand including Pharmacist check</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="1" type="text" name="measure-check" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Preparing labels from dispensary system</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="0.5" type="text" name="labels" />
Minute(s)</p></td>
</tr>
<tr>
<td><p>Write up CD register</p></td>
<td colspan="2"><p style="padding-left:5px!IMPORTANT;">
<input value="2" type="text" name="cd-register" />
Minute(s)</p></td>
</tr>
<tr>
<td></td>
<td colspan="3"><div class="estimate">
<input style="margin-bottom:20px;" type="submit" value="Estimate my savings" />
</div></td>
</tr>
</table>
</form>
<select id="ddlTime" name="ddlTime">
and
if( $_POST['ddlTime']=='perhour' ){
// if per hour is selected:
$result_pharmacist = $_POST["pharmacist"];
$result_dispenser = $_POST["dispenser"];
}elseif( $_POST['ddlTime']=='perannum' ){
// if per annum is selected:
$user_pharmacist = $_POST["pharmacist"];
$result_pharmacist = $user_pharmacist/37.5/52;
$user_dispenser = $_POST["dispenser"];
$result_dispenser = $user_dispenser/37.5/52;
}
I hope this help you :
<?php
if(isset ($_POST['save']))
{
$temp = $_POST['opsi'];
echo $temp;
}
?>
<html>
<body>
<form action="newEmptyPHP.php" method="POST">
<h3> Choose Your Option</h3>
<select name="opsi">
<option value=0 selected>- Customer -</option>
<option value="perannum" name="perannum">perannum</option>
<option value="perhour" name="perhour">perhour</option>
</select> <br> <br>
<input type="submit" name="save" value="Save and Commit">
</form>
</body>
</html>

Categories