6 Replies - 11690 Views - Last Post: 11 February 2008 - 04:06 PM Rate Topic: -----

#1 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Trouble with $_POST['value']?

Posted 11 February 2008 - 02:03 PM

Okay, I'm really hoping someone can figure this out, it's been kicking my butt for several days now and I'm on a tight schedule. I have a page for entering a purchase order into a database. It populates lists of possible items from a pricebook table in the database and then inserts x instances of that item into a temp table of the database. The page looks up the count of each item in the temp table and allows the user to click a delete button next to each item to remove all instances of that item. The page posts to itself and determines if and which delete button was clicked and deletes the matching items from temp. I had this working perfectly several days ago, don't know what happened.

What I do know is that it appears the $_POST['name'] variable that is supposed to contain the item name to be deleted is null. I can retrieve the value from the field all the way up until posting. All other $_POST values are accurate. I've quadruple checked the names to make sure it wasn't a typo. I can't imagine why else this would be. I even tried commenting out blocks of code at a time to try to isolate the problem.

I will post the complete page code. I would greatly appreciate any help, this ones got me beat.

<!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>New Purchase Order</title>
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/cascadingDropdowns.js"></script>
<?php
		//Set session variable to enable savePO.php script (prevent direct access)
	session_start(); 
		$_SESSION['enabled'] = true;
	
	//Set vendor and PO Number for duration of session and disable vendor selection
	if(isset($_POST['vendor'])){
		$_SESSION['vendor'] = $_POST['vendor'];
		$_SESSION['disabled'] = " disabled=true ";
	}
	$disabled = $_SESSION['disabled'];
	
?>
</head>

<body>
<?php 
echo $_POST['name2'];
echo "<br>";
echo $_POST['name3'];
  require_once('Connections/inventory.php'); 
	require_once('Connections/connect.php');

	  //Add Button Action
	  //Check if add button was clicked and add item to temp
	  if(isset($_POST['add'])){
			//Retrieve record for selected pricebook item
			$name = $_POST['item'];
			$sql = "SELECT * FROM pricebook WHERE name = '$name'";
			$result = mysql_query($sql) or die(mysql_error());
			$rowcount = mysql_num_rows($result);
			$row = mysql_fetch_array($result);
		
			//Retrieve values for selected pricebook item
			$name = $row['name'];
			$part_number = $row['part_number'];
			$description = $row['description'];
			$item_cost = $row['item_cost'];
			$item_retail = $row['item_retail'];
			$vendor = $_POST['vendor'];
			$location = "INVENTORY";
			$category = $row['category'];
			$purchase_order = $_POST['purchase_order'];
			$qty = $_POST['qty'];
		
			if($rowcount > 0){ //Prevent Null Entries
		  //Insert item into database
		  for($i = 0; $i < $qty; $i++){
			mysql_query("INSERT INTO temp (name, part_number, description, item_cost, item_retail, vendor, location, category, purchase_order)
							   	 VALUES ('$name', '$part_number', '$description', '$item_cost', \"$item_retail\", \"$vendor\", \"$location\",	\"$category\", \"$purchase_order\")")
					  or die(mysql_error());
					  }
				}
			}
			
//include('remove.php');

	//TEST: Remove Action
	//Get Row Count
	$sql = "SELECT COUNT(DISTINCT name) FROM temp";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$count = $row[0] + 1;
	echo $count."<br>";
	
	//Delete selected row from temp
	for($i = 2; $i <= $count; $i++){ //$i must be at least 2 two sync with item rows
		if(isset($_POST["remove$i"])){
			echo $i. "<br>";
			echo "POST: ".$_POST["name$i"]."<br>";
			$name = $_POST["name$i"];
			echo "NAME: ".$name;
			mysql_query("DELETE FROM inventory.temp WHERE name = '$name'") or die("Could Not Remove Items: " . mysql_error());
		}
	}
	
	
	/*
		//Remove Button Action
		//Check if and which remove button was clicked and remove the corresponding item from temp
	$sql = "SELECT COUNT(DISTINCT name) FROM temp";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$count = $row[0] + 1;
	  
	for($i = 2; $i <= $count; $i++){
		$name = $_POST["name$i"];
		if(isset($_POST["remove$i"])){ //Check which remove button was clicked
			$sql = "DELETE FROM temp WHERE name = '$name'";
			mysql_query($sql) or die(mysql_error());
		}
	} 
	*/   
	
	//Lookup user by IP address
	$ip = $_SERVER['REMOTE_ADDR'];
	$sql = "SELECT name FROM users WHERE ip = '$ip'";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$user = $row[0];
	
	//Set PO Number to next available. Display error if no records were found.
	$sql = "SELECT po_number FROM purchase_orders";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_assoc($result);
	$count = count($row);	
	if($count > 0){
		$po_number = max($row) + 1;	
		}
	else {
		$po_number = 0;
	echo "WARNING: No prior Purchase Orders found. If this is not correct please contact the administrator.";
		}
		
?>
<div align="center">
	<form action='new_po.php' method='POST' name='new_po'>
	  
	  <!--PO Fieldset-->
	  <fieldset><legend>Purchase Order</legend>
		  <table><tr>
			<?php echo "
			  <td><label for='purchase_order'>PO Number</label></td>
			<td><input type='text' name='purchase_order' id='purchase_order' value='$po_number' $disabled /></td>
			<td><label for='vendor'>Vendor</label></td>
			<td><select name='vendor' id='vendor'>
				<option value=''>--None Selected--</option>";
				$sql = "SELECT name FROM vendors";
				$result = mysql_query($sql);
				while($row = mysql_fetch_array($result)){
				  if(isset($_SESSION['vendor'])){ //Check if vendor is already set
			if($row[0] == $_SESSION['vendor']){ //Select vendor that was selected prior to form submission
				$selected = " SELECTED ";
			}
			else {
				$selected = '';
			}
		  } 
					echo "<option $selected value='$row[0]' $disabled >$row[0]</option>";
					} echo "
			</select></td>
			<td><label for='user'>Placed By</label></td>
			<td><input type='text' name='user' id='user' value='$user' $disabled /></td>";
			?>
		 </tr></table>
	</fieldset>
	
	<br>
	
	<!--Add Item Fieldset-->
	<fieldset><legend>Add Item</legend>
		<table><tr>
			<td><label for="category">Category</label></td>
			<td><select name="category" id="category">
			<option value="0">--None Selected--</option>
			<?php 
				//Populate Category Dropdown
				$sql = "SELECT category FROM categories";
				$result = mysql_query($sql) or die(mysql_error());
				$i = 1;
				while($row = mysql_fetch_array($result)){
					echo "<option value='$i'>$row[0]</option>";
					$i++;
					}
			?>
		 	</select></td>
			<td><label for="item">Item</label></td>
			<td><select name="item" id="item">
		 	<option class="0">--None Selected--</option>
			<?php 
				//Populate Items - Linked to category
				$sql = "SELECT category FROM categories";
				$result = mysql_query($sql) or die(mysql_error());
				$i = 1;
				while($row = mysql_fetch_array($result)){
					$sql2 = "SELECT name FROM pricebook WHERE category = '$row[0]'";
					$result2 = mysql_query($sql2) or die(mysql_error());
					$row_count = mysql_num_rows($result2);
					if($row_count > 0){ //Check for null rows to keep dropdown relationships in sync
						while($row2 = mysql_fetch_array($result2)){
							echo "<option class='$i' value='$row2[0]'>$row2[0]</option>";
							$i++;
							}
						}
					else {
						$i++; //Increment counter if null result set
						}
					}			
			?>
			</select></td>
			<td><label for="qty">Quantity</label></td>
			<td><input type="text" name="qty" id="qty" size=8 /></td>
			<td><input type="submit" name="add" id="add" value="Add" onclick="submit" /></td>
		</tr></table>
	 </fieldset>
   <!--/form-->
   
   <br>
   
   <!--Current Items Fieldset-->
   <fieldset><legend>Current Items</legend>
   		<!--form name="current" action="new_po.php" method="POST"-->
	   <table align="center">
			<tr class='header'><td><label>Item Name</label></td><td><label>Part Number</label></td><td><label>Cost</label></td>
				<td><label>Retail</label></td><td><label>Category</label></td><td><label>Qty</label></td></tr>
			<?php 
				//Select items from temp
				$sql = "SELECT * FROM temp GROUP BY name";
				$result = mysql_query($sql) or die(mysql_error());
				$i = 2; //Must be 2 or greater so $i%2 can be used to determine if row is odd or even 
				while($row = mysql_fetch_array($result)){
				  $name = $row['name'];
					$sql2 = "SELECT COUNT(name) 
				   FROM temp 
				   WHERE name = '$name'
				   GROUP BY name";
					$result2 = mysql_query($sql2) or die(mysql_error());
					$row2 = mysql_fetch_array($result2);

					//Determine whether row is even or odd for row coloring
					$odd = $i%2;
					if($odd == 0){
						$class = 'odd';
						}
					else {
						$class = 'even';
						}

		  //Set Item Count
					if($row2[0] == 0){
						$count = 0;
						}
					else {
						$count = $row2[0];
						}

					//Display Items					
					echo "<tr><td class='$class'><input type='text' name='name$i' disabled=true value='$name'></td>
							  <td class='$class'><input type='text' name='part_number$i' disabled=true value='".$row['part_number']."'></td>
							  <td class='$class'><input type='text' name='item_cost$i' disabled=true value='".$row['item_cost']."'></td>
							  <td class='$class'><input type='text' name='item_retail$i' disabled=true value='".$row['item_retail']."'></td>
							  <td class='$class'><input type='text' name='category$i' disabled=true value='".$row['category']."'></td>
							  <td class='$class'><input type='text' name='qty$i' disabled=true value='$count'></td>
							  <td><input type='submit' name='remove$i' id='remove$i' value='Remove' onclick='submit'></td></tr>";
					$i++; //Increment Row Number
					}
					//Terminate MySQL Connection
					mysql_close($link);
					
			?>
				 </form>
		</table>
	</fieldset>
	
	<!--Cancel and Purchase Order Submission Buttons-->
   <div style='width:80%; text-align:right; padding:10px;'>
		<form name='submit' action='savePO.php' method='POST'>
			<input type='button' name='cancel' id='cancel' value='Cancel' onclick='window.location="index.php"'>
			<input type='submit' name='submit' id='submit' value='Submit' onclick='submit'>			
		</form>
	</div>
	
</div>

<script language='javascript'>
//execute when the page is ready
window.onload=applyCascadingDropdowns;
</script>

</body>
</html>



Thanks In Advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with $_POST['value']?

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,069
  • Joined: 10-May 07

Re: Trouble with $_POST['value']?

Posted 11 February 2008 - 02:25 PM

2 things with your code.

1.) "name" is a keyword used in html, so I would avoid using it as the name of a variable.
2.) You are trying to reference $_POST["name$1"] within in a loop, therefor creating $_POST["name1"], $_POST["name2"] and so on. However, I don't see where you pass these values inside of the form. You only pass name. So when you receive name in your post it's $_POST['name'].

I hope that this will help shed some light on your code. I'll look into it further when I have more time to dedicate to it.
Was This Post Helpful? 0
  • +
  • -

#3 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Trouble with $_POST['value']?

Posted 11 February 2008 - 02:53 PM

been reading the code, some sample debug data would be usefull.
Was This Post Helpful? 0
  • +
  • -

#4 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Trouble with $_POST['value']?

Posted 11 February 2008 - 03:09 PM

just sujestion, but i would rewrite it to return id's.
strings can cause issues..

the select by group name an assoc group id?

then couple changes to change the field names and post results.

like.

<input type='text' name='name[$db_id]' disabled=true value='$name'>
//then foreach the post name and you have the db id.




then you know for shure what record.

int array's in general are easier to work with then strings.

another thing may help, print_r($_POST);

This post has been edited by SpaceMan: 11 February 2008 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 Mike007  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 332
  • Joined: 30-August 07

Re: Trouble with $_POST['value']?

Posted 11 February 2008 - 03:32 PM

Just a little suggestion, in addition to what those guys have said, I would like to direct your attention to this bit of code:
	//Set PO Number to next available. Display error if no records were found.
	$sql = "SELECT po_number FROM purchase_orders";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_assoc($result);
	$count = count($row); 



Why are you selecting the entire row and fetching it from the database if all you need is the number of records? It would be alot faster to do it with SQL like this:
SELECT COUNT(po_number) AS num_records FROM purchase_orders


Then you would just get the results with mysql_result, it should be alot faster and alot nicer :).
Was This Post Helpful? 0
  • +
  • -

#6 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Trouble with $_POST['value']?

Posted 11 February 2008 - 03:36 PM

found the problem..

inputs are not posting, disabled.
use hidden and display with div tag.

works.
<?

if($_POST['submit']){
  print_r($_POST);
}

echo 'Sample form<form method="post">';

//sample loop of DB records
$rand_values = array('these','that','stuff','cart item','items');
for ($i = 0; $i <= 4; $i++) {
   $name = $rand_values[$i];
   echo $i . ' <input type="text" name="name'.$i.'" value="'.$name.'"><br>';
}
echo '<input type="submit" name="submit" value="Post"></form>';

?>



does not work.
<?

if($_POST['submit']){
  print_r($_POST);
}

echo 'Sample form<form method="post">';

//sample loop of DB records
$rand_values = array('these','that','stuff','cart item','items');
for ($i = 0; $i <= 4; $i++) {
   $name = $rand_values[$i];
   echo $i . ' <input type="text" name="name'.$i.'" disabled=true value="'.$name.'"><br>';
}
echo '<input type="submit" name="submit" value="Post"></form>';

?>




line 246, can change. then should work and display like you had it.
<input type='text' name='temp$i' disabled=true value='$name'><input type='hidden' name='name$i' value='$name'>




future codeing, recomend. int array's.
<?

if($_POST['name']){
  foreach($_POST['name'] as $var => $val){
	if(!is_numeric($var)) continue;
	echo "$var => $val<br>";
  }
}

echo 'Sample form<form method="post">';

//sample loop of DB records, id's and names.
$rand_values = array('these','that','stuff','cart item','items');
for ($i = 0; $i <= 4; $i++) {
   $name = $rand_values[$i];
   echo '<input type="text" name="temp['.$i.']" value="'.$name.'"><input type="hidden" name="name['.$i.']" value="'.$name.'"><br>';
}
echo '<input type="submit" name="submit" value="Post"></form>';

?>


This post has been edited by SpaceMan: 11 February 2008 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 bstonehill  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 113
  • Joined: 21-January 08

Re: Trouble with $_POST['value']?

Posted 11 February 2008 - 04:06 PM

Wow, thanks everyone for the help. Lots of good info, I really appreciate it.

This post has been edited by bstonehill: 11 February 2008 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1