I'm an experienced programmer, and I've done this a hundred times, but for some reason it's not working, and it's driving me insane.
Here's the short version first:
Here's where they sit inside my form:
Here is what populates the div. The below line is placed in a properly working while loop (mysql_fetch_array):HTML Code:<div class="scroll_checkboxes"> <?php echo $options; ?> </div>
And here is where I'm trying to grab it after a submit:PHP Code:$options .= "<input type=\"checkbox\" name=\"mayNeed[]\" value=\"$id\" /> $id - $product_name - $modelNumber<br />";
It's not working. I get the results of my else statement instead. For some reason it is not setting the POST.PHP Code:$tester = "";
if(isset($_POST['mayNeed']))
{
$likeArray = $_POST['mayNeed'];
foreach($likeArray as $item)
{
$tester .= $item;
}
} else {
echo "<h1>IT'S NOT WORKING!</h1>";
}
Here's the long version in case you want to or need to see all of my code. Most of it is superfluous to this problem, but here you go:
PHP Code:<?php include('credentials.php');?>
<?php
// Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Delete items
if(isset($_GET['deleteid'])) {
echo "Are you sure you want to delete product with the ID " .$_GET['deleteid'].'? <a href="inventory_list.php?yesdelete='.$_GET['deleteid'].'">Yes</a> | <a href="inventory_list.php">No</a>';
exit();
}
if(isset($_GET['yesdelete'])) {
// remove item from system and delete its picture
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("delete from products where id='$id_to_delete' limit 1") or die(mysql_error());
$pic_to_delete = ("../inventoryImages/$id_to_delete.jpg");
if(file_exists($pic_to_delete)) {
unlink($pic_to_delete);
}
header("location: inventory_list.php");
exit();
}
?>
<?php
// Parse form data and add inventory item
$tester = "";
if(isset($_POST['mayNeed']))
{
$likeArray = $_POST['mayNeed'];
foreach($likeArray as $item)
{
$tester .= $item;
}
} else {
echo "<h1>IT'S NOT WORKING!</h1>";
}
if(isset($_POST['product_name'])) {
$product_name = mysql_real_escape_string($_POST['product_name']);
$model_number = mysql_real_escape_string($_POST['model_number']);
$price = mysql_real_escape_string($_POST['price']);
$quantity = mysql_real_escape_string($_POST['quantity']);
$category = mysql_real_escape_string($_POST['category']);
$subcategory = mysql_real_escape_string($_POST['subcategory']);
$details = mysql_real_escape_string($_POST['details']);
if($category == "loungeSeats"){
$category = 6;
} else if($category == "jumpSeats") {
$category = 7;
} else if($category == "bolsterSeats") {
$category = 1;
} else if($category == "bucketSeats") {
$category = 4;
}
$sql = mysql_query("select id from products where product_name = '$product_name' limit 1");
$productMatch = mysql_num_rows($sql);
if($productMatch > 0) {
echo "Sorry, you tried to place a duplicate product name into the system. <a href='inventory_list.php'>Click Here</a>";
exit();
}
$sql = mysql_query("insert into products (product_name, model_number, price, quantity, details, category, subcategory, date_added) values ('$product_name', '$model_number', '$price', '$quantity', '$details', '$category', '$subcategory', now())") or die(mysql_error());
$pid = mysql_insert_id();
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'],"../inventoryImages/$newname");
header("location: inventory_list.php");
exit();
}
?>
<?php
// Grabs list for viewing
$product_list = "";
$options = false;
$sql = mysql_query("select * from products");
$product_count = mysql_num_rows($sql);
if($product_count > 0){
while($row = mysql_fetch_array($sql)) {
$id = $row['id'];
$product_name = $row['product_name'];
$modelNumber = $row['model_number'];
$price = $row['price'];
$quantity = $row['quantity'];
$date_added = strftime("%b %d, %Y", strtotime($row['date_added']));
$product_list .= "
<tr>
<td>Product ID: $id</td>
<td><strong>$product_name</strong></td>
<td>$modelNumber</td>
<td>$$price</td>
<td>$quantity</td>
<td>$date_added</td>
<td><a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a></td>
</tr>";
$options .= "<input type=\"checkbox\" name=\"mayNeed[]\" value=\"$id\" /> $id - $product_name - $modelNumber<br />";
}
} else {
$product_list = "You have not products listed in your store yet.";
}
?>
<html>
<head>
<title>Inventory List</title>
<link rel="stylesheet" type="text/css" href="../style/styles.css" />
<script type="text/javascript" src="../jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
$('.list tr:even').css('background-color', '#999');
});
</script>
<script type="text/javascript" language="javascript">
function validateMyForm(){
var isValid = true;
if(document.myForm.product_name.value==""){
alert("Please enter a product name");
isValid = false;
} else if (document.myForm.price.value=="") {
alert("Please enter a price");
isValid = false;
} else if (document.myForm.category.value=="") {
alert("Please enter a category");
isValid = false;
} else if (document.myForm.details.value=="") {
alert("Please enter a description");
isValid = false;
} else if (document.myForm.fileField.value=="") {
alert("Please upload a file");
isValid = false;
}
return isValid;
}
</script>
</head>
<body>
<div id="mainWrapper">
<div id="pageHeader">
<br />
</div>
<div id="pageContent">
<div id="navcontainer">
<?php include("../inlineNav.php"); ?>
</div>
<div style="text-align: right; padding-right: 20px">
<a href="inventory_list.php#inventoryForm">+Add New Item</a>
</div>
<div style="margin-left: 24px">
<h2>Inventory List</h2>
<table width=90% border=1 class="list">
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Model Number</th>
<th>Price</th>
<th>Quantity</th>
<th>Date</th>
<th>Admin</th>
</tr>
<?php echo $product_list; ?>
</table>
</div>
<div style="margin-left: 24px">
<a name="inventoryForm" id="inventoryForm"></a>
<h2>New Item Form</h2>
<form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">
<table width=90%>
<tr>
<td width=20%>Product Name</td>
<td width=80%>
<label>
<input type="text" name="product_name" id="product_name" size="64" />
</label>
</td>
</tr>
<tr>
<td width=20%>Model Number</td>
<td width=80%>
<label>
<input type="text" name="model_number" id="model_number" size="12" />
</label>
</td>
</tr>
<tr>
<td width=20%>Product Price</td>
<td width=80%>
<label>
$<input type="text" name="price" id="price" size="12" />
</label>
</td>
</tr>
<tr>
<td width=20%>Quantity</td>
<td width=80%>
<label>
<input type="text" name="quantity" id="quantity" size="5" />
</label>
</td>
</tr>
<tr>
<td width=20%>Category</td>
<td width=80%>
<label>
<select name="category" id="category">
<option value=""></option>
<option value="bolsterSeats">Bolster Seats</option>
<option value="bucketSeats">Bucket Seats</option>
<option value="camo">Camo</option>
<option value="fishingSeats">Fishing Seats</option>
<option value="jumpSeats">Jump Seats</option>
<option value="loungeSeats">Lounge Seats</option>
</select>
</label>
</td>
</tr>
<tr>
<td width=20%>Subcategory</td>
<td width=80%>
<label>
<select name="subcategory" id="subcategory">
<option value=""></option>
<option value="none">None</option>
</select>
</label>
</td>
</tr>
<tr>
<td width=20%>Like Items</td>
<td width=80%>
<div class="scroll_checkboxes">
<?php echo $options; ?>
</div>
</td>
</tr>
<tr>
<td width=20%>Product Details</td>
<td width=80%>
<label>
<textarea name="details" id="details" cols="64" rows="5"></textarea>
</label>
</td>
</tr>
<tr>
<td width=20%>Product Image</td>
<td width=80%>
<label>
<input type="file" name="fileField" id="fileField" />
</label>
</td>
</tr>
<tr>
<td></td>
<td>
<label>
<input type="submit" name="button" id="button" value="Add this item now" onClick="javascript:return validateMyForm();" />
</label>
</td>
</tr>
</table>
</form>
</div>
<div id="pageFooter">
</div>
</div>


Reply With Quote


Bookmarks