...

View Full Version : Using PHP generated form data to choose other data



Mtruffa
08-18-2012, 03:51 AM
I have PHP page in which it inputs delivery info based on customer. The form goes to the mysql database to get the userid and company name for the form and the user inputs the rest. Each customer has 3 pricing levels based on grade in their user table. What I need to do is assign the price for the pallet based on the userid and the grade chosen in the form. I know I can get the data when I goto the db in the first form line but i can not figure out how to pass the info down after the company (id) is selected. the pallet price is the price I am looking for based on the grade selected in the input above that.

Here is the code for the page




<?php
include 'dbc.php';
page_protect();

if(!checkAdmin()) {
header("Location: login.php");
exit();
}

$page_limit = 10;




// filter GET values
foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
$post[$key] = filter($value);
}


$rs_all = mysql_query("select count(*) as total_all from users") or die(mysql_error());


list($all) = mysql_fetch_row($rs_all);
?>
<?php
$sql="SELECT id, user_name, company FROM users WHERE user_name <> 'admin'";
$result=mysql_query($sql) or die(mysql_error());

$options="";

while ($row=mysql_fetch_array($result)) {

$id=$row["id"];
$thing=$row["company"];
$options.="<OPTION VALUE=\"$id\">".$thing.'</option>';
}
?>



<?php
$err = array();

if($_POST['doDeliver'] == 'Enter Del')
{
if(empty($err)) {
$pallets = $_POST['pal_del'];
$price = $_POST['pal_price'];
$total_cost = $pallets*$price;

$sql_insert = "INSERT into `deliveries`
(`id`,`del_date`,`trail_num`,`pal_del`,`pal_grd`,`pal_price`,`debit`)
VALUES
('$_POST[id]','$_POST[del_date]','$_POST[trail_num]','$_POST[pal_del]','$_POST[pal_grd]','$pal_price','$total_cost') ";

mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
}

}


?>
<html>
<head>
<title>USMI Pallets, Inc. :: Delivery Entry Main Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery-ui-1.8.22.custom.min.js">
</script>
<link href="jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css">
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: "yy-dd-mm" });
});
</script>
</head>

<body>


<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%" valign="top"><?php
if (isset($_SESSION['user_id'])) {?>
<div class="myaccount">
<p><strong>Admin Center</strong></p>
<a href="admin.php">Admin Home</a><br>
<a href="pickup_ent.php">Pickups Entry</a><br>
<a href="delivery_ent.php">Delivery Entry</a><br>
<a href="payment_ent.php">Payment Entry</a><br>

<a href="logout.php">Logout </a>


<?php } ?>
</td>
<td width="74%" valign="top" style="padding: 10px;"><h2><font color="#FF0000">Delivery Entry
Page</font></h2>

<p>&nbsp;</p>
<form name "deliveryform" action="delivery_ent.php" method="post">
<table width="80%" border="0" align="center" cellpadding="10" cellspacing="0" style="background-color: #E4F8FA;padding: 2px 5px;border: 1px solid #CAE4FF;" >
<tr>
<td>
Account:
<SELECT NAME=id>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT> </td>
</tr>
<tr>
<td>Date: <input name="del_date" type="text" id="datepicker" /></td>
</tr>
<tr>
<td>Trailer #:<select name="trail_num" id="trail_num">
<option selected value=""></option>
<option value="1986-1">1986-1</option>
<option value="1986-2">1986-2</option>
<option value="1986-3">1986-3</option>
<option value="1986-4">1986-4</option>
<option value="1986-5">1986-5</option>
<option value="1986-6">1986-6</option>
<option value="1986-7">1986-7</option>
<option value="1986-8">1986-8</option>
<option value="1986-9">1986-9</option>
<option value="1986-10">1986-10</option>
<option value="1986-11">1986-10</option>
<option value="1986-12">1986-12</option>
</select></td>
</tr>
<tr>
<td>Pallets Delivered: <input id="pal_del" name="pal_del" size="8"></td>
</tr>
<tr>
<td>Grade:<select name="pal_grd" id="pal_grd">
<option selected value=""></option>
<option value="A">Garde A</option>
<option value="B">Grade B</option>
<option value="C">Grade C</option>
</select></td>
</tr>
<tr>
<td>Pallet Price: <input id="pal_price" name="pal_price" size="8" ></td>
</tr>
<tr>
<td><input name="doDeliver" type="submit" id="doDeliver" value="Enter Del"></p>
</td>
</tr>

</table>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p></td>
<td width="12%">&nbsp;</td>
</tr>
</table>

</body>
</html>

sunfighter
08-18-2012, 03:51 PM
When you extract your client information you assign php variables to the three pallet prices. I used $pick_1 etc. Then when user chooses the Grade you use javascript to fill in the Pallet Price. Things look like this in php with js:


<?php
$pick_1 = 'One Dollar';
$pick_2 = 'Two Dollars';
$pick_3 = 'Five Dollars';
?>


<td>Grade:
<select name="pal_grd" id="pal_grd" onchange="math(value);">
<option selected="selected"></option>
<option value="A">Grade A</option>
<option value="B">Grade B</option>
<option value="C">Grade C</option>
</select>
</td>
<br /><br /><br />
<td>Pallet Price: <input id="pal_price" name="pal_price" size="8" ></td>

<script type="text/javascript">
function math(value){
if(value == 'A') document.getElementById("pal_price").value = "<?php echo $pick_1; ?>";
if(value == 'B') document.getElementById("pal_price").value = "<?php echo $pick_2; ?>";
if(value == 'C') document.getElementById("pal_price").value = "<?php echo $pick_3; ?>";
}

</script>

Mtruffa
08-18-2012, 07:20 PM
i am getting the info in the box but it is giving me grade_a, grade_b, grade_c which is what I set for One Dollar, Two dollar... What I need it to give me is the value of grade_a from the user table based on the id chosen in the account input. I changed the select statement to include the grades $sql="SELECT id, user_name, company, grade_a, grade_b, grade_c FROM users WHERE user_name <> 'admin'"; but it is not pulling the data. I am missing a line of code somewhere that after the account is picked associate the grade price to that account.

UPDATE: I have changed the code you gave me to pull the data grade_a, b & c after the id is selected. the only problem is pull the last user in the db no the user from the id chosen under account. here is the code I added.



<?php
$userid = 'id';
$sql_grd = "SELECT id, grade_a, grade_b, grade_c FROM users WHERE id = $userid";
$result_grd = mysql_query($sql_grd) or die(mysql_error());
while ($row_grd = mysql_fetch_array($result_grd)) {
$grdA = $row_grd["grade_a"];
$grdB = $row_grd["grade_b"];
$grdC = $row_grd["grade_c"];
}

?>
<td>Grade:
<select name="pal_grd" id="pal_grd" onchange="math(value);">
<option selected="selected"></option>
<option value="A">Grade A</option>
<option value="B">Grade B</option>
<option value="C">Grade C</option>
</select>
</td>
</tr><tr>
<td>Pallet Price: <input id="pal_price" name="pal_price" size="8" ></td>

<script type="text/javascript">
function math(value){
if(value == 'A') document.getElementById("pal_price").value = "<?php echo $grdA; ?>";
if(value == 'B') document.getElementById("pal_price").value = "<?php echo $grdB; ?>";
if(value == 'C') document.getElementById("pal_price").value = "<?php echo $grdC; ?>";
}

</script>

sunfighter
08-19-2012, 04:55 PM
Are you running my code as a .PHP file? Are you running the code I gave ONLY, nothing added? There is no grade_a, grade_b, grade_c in my code so it could not give you that.

Get my code working first then check yours. Run the top php section by it's self and echo out the $grdA, $grdB, $grdC.



<?php
$userid = 'id';
$sql_grd = "SELECT id, grade_a, grade_b, grade_c FROM users WHERE id = $userid";
$result_grd = mysql_query($sql_grd) or die(mysql_error());
while ($row_grd = mysql_fetch_array($result_grd)) {
$grdA = $row_grd["grade_a"];
$grdB = $row_grd["grade_b"];
$grdC = $row_grd["grade_c"];
}
echo $grdA;
echo $grdB;
echo $grdC;
?>


Once that's working you can substitute it into my code.

Mtruffa
08-19-2012, 06:19 PM
Your code does work sort of. here is what i have. I went back to just your code.



<?php

$grdA = 'grade_a';
$grdB = 'grade_b';
$grdC = 'grade_c';

echo $grdA;
echo $grdB;
echo $grdC;
?>
<td>Grade:
<select name="pal_grd" id="pal_grd" onchange="math(value);">
<option selected="selected"></option>
<option value="A">Grade A</option>
<option value="B">Grade B</option>
<option value="C">Grade C</option>
</select>
</td>
</tr><tr>
<td>Pallet Price: <input id="pal_price" name="pal_price" size="8" ></td>

<script type="text/javascript">
function math(value){
if(value == 'A') document.getElementById("pal_price").value = "<?php echo $grdA; ?>";
if(value == 'B') document.getElementById("pal_price").value = "<?php echo $grdB; ?>";
if(value == 'C') document.getElementById("pal_price").value = "<?php echo $grdC; ?>";
}

</script></tr>


it echo's grade_agrade_bgrade_c and gives me in pal_price the corresponding to what i selected in pal_grd. The only problem is it gives me "grade_a" not the value from the user table for grade_a for that user selected in Account. That is why I added the other code, which did not work.

Can I add something to this code which is before the form.



?php
$sql="SELECT id, user_name, company, grade_a, grade_b, grade_c FROM users WHERE user_name <> 'admin'";
$result=mysql_query($sql) or die(mysql_error());

$options="";

while ($row=mysql_fetch_array($result)) {

$id=$row["id"];
$thing=$row["company"];
$options.="<OPTION VALUE=\"$id\">".$thing.'</option>';
}
?>


this is what selects the Account



<tr>
<td>
Account:
<SELECT NAME=id>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT> </td>
</tr>


somehow I need to associate the grades that are in the user table to the user selected in the Account portion of the form. it would seem that it would need to be some extra js that is missing.

Thanks

sunfighter
08-19-2012, 06:29 PM
That is not my code. My code is in the second post of this thread, go look at it.
Here it is again:


<?php
$pick_1 = 'One Dollar';
$pick_2 = 'Two Dollars';
$pick_3 = 'Five Dollars';
?>


<td>Grade:
<select name="pal_grd" id="pal_grd" onchange="math(value);">
<option selected="selected"></option>
<option value="A">Grade A</option>
<option value="B">Grade B</option>
<option value="C">Grade C</option>
</select>
</td>
<br /><br /><br />
<td>Pallet Price: <input id="pal_price" name="pal_price" size="8" ></td>

<script type="text/javascript">
function math(value){
if(value == 'A') document.getElementById("pal_price").value = "<?php echo $pick_1; ?>";
if(value == 'B') document.getElementById("pal_price").value = "<?php echo $pick_2; ?>";
if(value == 'C') document.getElementById("pal_price").value = "<?php echo $pick_3; ?>";
}

</script>

Mtruffa
08-19-2012, 06:49 PM
Your code does work but the value for pick_1, pick_2, pick_3 is different for every user. I would be great if it was a set price but there are variables that determine the price. When the admin enters the user he sets the price for the customer as grade_a, grade_b and grade_c. So based on what user he is adding pallet delivery info for is what the price set should be. That is why I need somehow a way to look up those values based on what is chosen in account and then associated it with what is chosen for pallet grade.

Mtruffa
08-20-2012, 02:20 AM
I think I got it. I did everything after submit. I did it via php instead of js. here is the code see under if DoSubmit. that is where I selected the data by id. If there is a way to do this on the fly with js please let me know.




<?php
include 'dbc.php';
page_protect();

if(!checkAdmin()) {
header("Location: login.php");
exit();
}

$page_limit = 10;




// filter GET values
foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
$post[$key] = filter($value);
}


$rs_all = mysql_query("select count(*) as total_all from users") or die(mysql_error());


list($all) = mysql_fetch_row($rs_all);
?>
<?php
$sql="SELECT id, user_name, company FROM users WHERE user_name <> 'admin'";
$result=mysql_query($sql) or die(mysql_error());

$options="";

while ($row=mysql_fetch_array($result)) {

$id=$row["id"];
$thing=$row["company"];
$options.="<OPTION VALUE=\"$id\">".$thing.'</option>';
}
?>



<?php
$err = array();

if($_POST['doDeliver'] == 'Enter Del')
{
if(empty($err)) {
$pallets = $_POST['pal_del'];


$PGrade = $_POST['pal_grd'];
$userid = $_POST['id'];

$sql_grd = "SELECT id, grade_a, grade_b,grade_c FROM users WHERE id = $userid";
$result_grd=mysql_query($sql_grd) or die(mysql_error());
while ($row_grd=mysql_fetch_array($result_grd))
{
if ($PGrade = "A")
{$price = $row_grd["grade_a"];}
elseif ($PGrade = "B")
{$price = $row_grd["grade_B"];}
else {$price = $row_grd["grade_c"];}

}
$total_cost=$pallets*$price;

$sql_insert = "INSERT into `deliveries`
(`id`,`del_date`,`trail_num`,`pal_del`,`pal_grd`,`pal_price`,`debit`)
VALUES
('$_POST[id]','$_POST[del_date]','$_POST[trail_num]','$_POST[pal_del]','$_POST[pal_grd]','$price','$total_cost') ";

mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
}

}


?>
<html>
<head>
<title>USMI Pallets, Inc. :: Delivery Entry Main Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script language="JavaScript" type="text/javascript" src="js/jquery-ui-1.8.22.custom.min.js">
</script>
<link href="jquery-ui-1.8.22.custom.css" rel="stylesheet" type="text/css">
<script>
$(function() {
$( "#datepicker" ).datepicker({ dateFormat: "yy-dd-mm" });
});
</script>
</head>

<body>


<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%" valign="top"><?php
if (isset($_SESSION['user_id'])) {?>
<div class="myaccount">
<p><strong>Admin Center</strong></p>
<a href="admin.php">Admin Home</a><br>
<a href="pickup_ent.php">Pickups Entry</a><br>
<a href="delivery_ent.php">Delivery Entry</a><br>
<a href="payment_ent.php">Payment Entry</a><br>

<a href="logout.php">Logout </a>


<?php } ?>
</td>
<td width="74%" valign="top" style="padding: 10px;"><h2><font color="#FF0000">Delivery Entry
Page</font></h2>

<p>&nbsp;</p>
<form name "deliveryform" action="delivery_ent.php" method="post">
<table width="80%" border="0" align="center" cellpadding="10" cellspacing="0" style="background-color: #E4F8FA;padding: 2px 5px;border: 1px solid #CAE4FF;" >
<tr>
<td>
Account:
<SELECT NAME=id>
<OPTION VALUE=0>Choose
<?=$options?>
</SELECT> </td>
</tr>
<tr>
<td>Date: <input name="del_date" type="text" id="datepicker" /></td>
</tr>
<tr>
<td>Trailer #:<select name="trail_num" id="trail_num">
<option selected value=""></option>
<option value="1986-1">1986-1</option>
<option value="1986-2">1986-2</option>
<option value="1986-3">1986-3</option>
<option value="1986-4">1986-4</option>
<option value="1986-5">1986-5</option>
<option value="1986-6">1986-6</option>
<option value="1986-7">1986-7</option>
<option value="1986-8">1986-8</option>
<option value="1986-9">1986-9</option>
<option value="1986-10">1986-10</option>
<option value="1986-11">1986-10</option>
<option value="1986-12">1986-12</option>
</select></td>
</tr>
<tr>
<td>Pallets Delivered: <input id="pal_del" name="pal_del" size="8"></td>
</tr>
<tr>

<td>Grade:
<select name="pal_grd" id="pal_grd" >
<option selected="selected"></option>
<option value="A">Grade A</option>
<option value="B">Grade B</option>
<option value="C">Grade C</option>
</select>
</td>
</tr>
<tr>
<td><input name="doDeliver" type="submit" id="doDeliver" value="Enter Del"></p>
</td>
</tr>

</table>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p></td>
<td width="12%">&nbsp;</td>
</tr>
</table>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum