Post not sending action variable

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">&nbsp;
	<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>&nbsp;</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

Welcome Artemis to the site! ( I did not notice any spam… The code is posted well. )

Normally I check for posting with this code:
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
// Got something posted by the user
if(exists($_POST[“something”] ) {
do something…
}
if(exists($_POST[“something_else”]) {
do something else…
}
}
Quite normal. You never check for posts until something has shown up. But, often you need to check to see what is really being posted so that you can see if it all works normally. If you just dump the POST array it is easy to see what is happening. It also will show you the hidden fields that were posted. Here is a simple way to do that:
if($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
die("< pre>.print_r($_POST)."< /pre>");
This will print out the array and it is easy to read all the live data posted. This lets you test all your inputs and lets you see if the data is correct. I suggest attempting to debug it that way and let us know if you find the problem. If not make a smaller test page with just the form on it and test php code and show us that. Good luck!

One other debugging tool, you can get to the form that is failing and RIGHT-CLICK on the page and select VIEW-SOURCE-PAGE and it will show your code as it looks in the browser. Then, make sure that your form fields are all inside the < FORM > tag. If they are outside of it, they will never be posted to PHP. I thought of this after writing the previous note.

All,

Thank you all for your responses! I’ll look into those methods and procedures!

Thank you,

Sponsor our Newsletter | Privacy Policy | Terms of Service