Simplest Question but I can’t seem to find out where I’m going wrong.
What I think my issue is: POST action variable not being set. Therefore my if/then action based PHP core cannot move forward.
FLOW
index.php (defaults to default action(list customers)) -->
(cutomer_search.php) Filled out Search field and submit (action SHOULD be search_customer) ->
index.php (with action = search_customer). ->
customer_search.php (with a shortened list)
What I’ve found. Based on my troubleshooting. I’m not getting an ACTION back (it’s null which hits my default loop at the top and defaults).
###index.php
//Customer Manager Index
require('../model/database.php');
require('../model/customer_db.php');
$action = filter_input(INPUT_POST, 'action');
echo "First Action :" . $action;
if ($action === NULL) {
$action = filter_input(INPUT_GET, 'action');
echo "Second : " . $action;
if ($action === NULL) {
$action = 'customer_search';
echo "Third Action : " . $action;
}
}
echo "Last Action: " . $action;
if ($action == 'customer_search') {
$customers = get_customers();
include('customer_search.php');
} else if ($action == 'search_customer') {
$lastName = filter_input(INPUT_POST, 'lastName');
if ($lastName == NULL) {
$error = "Missing Last Name in Search.";
include('../errors/error.php');
} else {
$customers = get_customers_by_lastName($lastName);
include('customer_search.php');
header("Location: .?category_id=$category_id");
}
###customer_search.php
<?php include '../view/header.php'; ?>
<main>
<h1>Customer Manager</h1>
<h2>Customer Search</h2>
<form action="." method="post">
<label>Last Name::</label>
<input type="text" name="lastName">
<input type="hidden" name="action" value="search_customer">
<input type="submit" value="Search"></br>
</form>
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Last Name</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach ($customers as $customer) : ?>
<tr>
<td><?php echo $customer['firstName'] . ' ' . $customer['lastName']; ?></td>
<td><?php echo $customer['email']; ?></td>
<td><?php echo $customer['city']; ?></td>
<td><form action="index.php" method="post">
<input type="hidden" name="action"
value="select_customer">
<input type="hidden" name="custid" value="<?php echo $customer['customerID']; ?>">
<input type="submit" value="Select">
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p class="last_paragraph">
<a href="?action=customer_search">Search customer</a>
</p>
<!-- </section> --!>
</main>
<?php include '../view/footer.php'; ?>
Sorry for the Spam, I don’t know the board to make them snippets.
Thank you,
Artemis