2 Replies - 366 Views - Last Post: 12 October 2017 - 10:59 AM Rate Topic: -----

#1 Kendra Davis 2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-October 17

What am I missing my form won't post after pressing submit button?

Posted 11 October 2017 - 08:26 PM

Hi everyone,
I am new to PHP, MVC. I am trying to Update my form, but when I press the submit button nothing will show up on the index form. Can anyone help me figure out what I am doing wrong? Thanks in advance.

Index:

<?php 

//Edit Products:

$action = filter_input(INPUT_POST, 'action');
if($action == NULL) {
	$action = filter_input(INPUT_POST, 'action');
    if($action == NULL) {
		$action = 'list_product';
	}	
}

if ($action == 'list_product') {
    $category_id = filter_input(INPUT_POST, 'category_id', 
            FILTER_VALIDATE_INT);
    if ($category_id == NULL || $category_id == FALSE) {
        $category_id = 1;
    }
    $category_name = get_category_name($category_id);
    $categories = get_categories();
    $products = get_products_by_category($category_id);
} else if ($action == 'view_product'){
	$categories = get_categories();
	$categories = view_category($category_id);
	$product_id = filter_input(INPUT_POST, 'product_id',
	        FILTER_VALIDATE_INT);

	    
} else if ($action == 'show_edit_form') {
	$product_id = filter_input(INPUT_POST, 'product_id',
	       FILTER_VALIDATE_INT);
     if($product_id == NULL) {
		 $product_id = filter_input(INPUT_POST, 'product_id',
		     FILTER_VALIDATE_INT);
	 }
     $product = get_product($product_id);
     $categories = get_categories();
     include('edit_product.php');
	 
} else if ($action == 'update_product') {
	 $product_id = filter_input(INPUT_POST, 'product_id',
	         FILTER_VALIDATE_INT);
	 $category_id = filter_input(INPUT_POST, 'category_id',
	         FILTER_VALIDATE_INT);
     $code = filter_input(INPUT_POST, 'code');
     $name = filter_input(INPUT_POST, 'name');
     $price = filter_input(INPUT_POST, 'price');

      if ($product_id == FALSE || $category_id == FALSE || $code == NULL || $name === NULL || $price == NULL || $price == FALSE) {
		  $error = 'Invalid product data.
		            Check all fields and try again.';
	  }	 else {
		  $categories = get_categories();
		  update_product($product_id, $category_id, $code, $name, $price);
		  $product = get_product($product_id);
		  header('Location: .?action=list_products'); 
	  }
}
?>




// Product Db:


//Edit Products

function view_product() {
	global $db;
    $query = "SELECT * FROM categories
              WHERE categoryID = '$categoryID'";
    $category = $db->query($query);
    $category = $category->fetch();
    return $product;
}

function show_edit_form($product_id, $category_id, $code, $name, $price) {
	global $db;
	$query = 'UPDATE products
	          SET categoryID = :category_id,
                  productCode = :code,
                  productName = :name,
                  listPrice = :price
               WHERE productID = :product_id)';
	$statement = $db->prepare($query);
    $statement->bindValue(':category_id', $category_id);
    $statement->bindValue(':code', $code);
    $statement->bindValue(':name', $name);
    $statement->bindValue(':price', $price);
	$statement->bindValue(':product_id', $product_id);
    $statement->execute();
    $statement->closeCursor();
}

function update_product($product_id) {
    global $db;
    $query = 'UPDATE FROM products
              WHERE productID = :product_id';
    $statement = $db->prepare($query);
    $statement->bindValue(':product_id', $product_id);
    $statement->execute();
    $statement->closeCursor();
	include ('edit_product.php');
	
}

?>




// Edit Product:

<?php include '../view/header.php'; ?>
<?php $product_id = filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT); ?>

<!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>My Guitar Shop</title>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>

<!-- the body section -->
<body>
    <header><h1>Product Manager</h1></header>

    <main>
        <h1>Edit Product</h1>
        <form action="index.php" method="POST"
              id="edit_product_form">
            <input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
			<input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>" />
			
            <label>Category ID:</label>
		   <select name="category_id">
        <?php foreach ( $categories as $category ) : ?>
            <option value="<?php echo $category['categoryID']; ?>">
                <?php echo $category['categoryName']; ?>
            </option>
        <?php endforeach; ?>
        </select>
        <br>

		   
            <label>Code:</label>
            <input type="input" name="code" value="<?php echo $product['productCode']; ?>"><br>
          

            <label>Name:</label>
            <input type="input" name="name" value="<?php echo $product['productName']; ?>"><br>

            <label>List Price:</label>
            <input type="input" name="price" value="<?php echo $product['listPrice']; ?>"><br>

            <label>&nbsp;</label>
            <input type="submit" value="Save Changes">
            <br>
        </form>
       <p class="last_paragraph">
        <a href="index.php?action=list_products">View Product List</a>
    </p>
		
    </main>
	
	<?php include '../view/footer.php'; ?>
</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: What am I missing my form won't post after pressing submit button?

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: What am I missing my form won't post after pressing submit button?

Posted 11 October 2017 - 11:09 PM

What have you done to debug your code?

Progressive Testing

You need to provide a more helpful description than "nothing will show up" and a wall of code.
Was This Post Helpful? 0
  • +
  • -

#3 Kendra Davis 2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-October 17

Re: What am I missing my form won't post after pressing submit button?

Posted 12 October 2017 - 10:59 AM

Update: I am trying to update my PHP MVC form and When I put in the textbox: Category ID: Guitars, Code: les Paul, Name: Gibson Les Paul_2, List Price: 119.00. But when I press the submit button in the index it won't update Gibson Les Paul to
"Gibson Les Paul_2". It shows the form but it doesn't show my update. I try debug it by writing an if and else statement to see if it works and it says nothing works. I am not sure else how to debug it. I also notice it is undefined $product and how do you define it.
 <label>Code:</label> <input type="input" name="code" value="<?php echo $product['productCode']; ?>"><br> 
. Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1