Reposting again due to coding problem, I have attached the files
I am having trouble getting my button to submit the words in the text box over to the table on another page. What can I do to fix this?
Thanks

Recommended Answers

All 26 Replies

Thank you for resubmitting your code. It's much appreciated, to help us to help you :)

That being said, when you have:

<form action="product_list.php" method="post">
    <input type="text" name="...">
    <input type="submit">
</form>

Then, unless you have Javascript that is preventing the default event action, the values of the text input fields will be sent over in the POST request when you submit the form.

That being said, can you please explain in a little more detail exactly what you mean as far as submitting the words in the text box? Which form? Which text box?

In your file product_list.php, I see a form that posts to index.php to delete products in the table. Does that one work as intended?

In your file product_add.php, I see a form that posts to product_list.php. Is that the one you're having difficulty with?

Basically that form on the product_add.php page will submit a POST request to product_list.php with text input fields code, name, version, and release_date. The values will be user entered. The value of the submit button, itself, will be "Add Product" and there will be a hidden field with name action and value "add_product".

So now let's go to product_list.php, which is where we are POSTing to. I'm not seeing anything in the product_list.php file that inserts new records into the database.

In fact, I see that you have some code in index.php that says:

} else if ($action == 'add_product') { ... }

But I'm not seeing where that code block is ever being hit, because your form submits its data to product_list.php and not to index.php.

I do, see, however, that product_list.php loops through $products ... where is it getting this list of products? What is happening in header.php?? Is that where it's connecting to the database and pulling products from the database? Does index.php get called from within header.php? Can you upload header.php so we can see the full picture?

I'm not sure if the delete button works as of yet. I am having trouble with the other button. It is suppose to take the text that is imputed into the text boxes and put that into the table on the product_list.php page. I'm having trouble with getting that data to go across. I'm only suppose to be using php and HTML for this

I'm only suppose to be using php and HTML for this

No worries. My comment about Javascript was that you can, theoretically, create Javascript that blocks normal HTML behavior. Provided you don't have Javascript, it's just normal HTML behavior.

Let's see how you structured your app:

index.php => This page seems to serve as the entry point into your app. It loads the database.php file (which you haven't included, but I assume it has the functions needed to connect to the database, as well as defining the $db variable), and it loads product_db.php, which is basically just a bunch of functions that do different things interacting with the database (and are either returning result sets, or adding a row to the products table).

I would clean up index.php and make it look like this instead:

<?php
require('../model/database.php');
require('../model/product_db.php');

// Try to retrieve $_POST['action']
$action = filter_input(INPUT_POST, 'action');

// If that didn't work ...
if ($action === NULL) {

    // Try to retrieve $_GET['action']
    $action = filter_input(INPUT_GET, 'action');

    // If that didn't work
    if ($action === NULL) {
        $action = 'list_products';
    }
}

if ($action == 'list_products') {

    // Call the function defined in product_db.php to retrieve the list of products from the database
    $products = get_products();

    // Display the product list
    include('product_list.php');

} else if ($action == 'delete_product') {

    // Retrieve the product code we want to delete
    $product_code = filter_input(INPUT_POST, 'product_code');

    // If a product code was passed in via a form
    if ($product_code != false) {
        // Call the function defined in product_db.php to delete the product
        delete_product($product_code);
    }

    // Retrieve the updated list of products, now that one has been deleted
    $products = get_products();

    // Show the updated product list
    include('product_list.php');

} else if ($action == 'show_add_form') {

    //Display the form to add a new product
    include('product_add.php');

} else if ($action == 'add_product') {

    $code = filter_input(INPUT_POST, 'code');
    $name = filter_input(INPUT_POST, 'name');
    $version = filter_input(INPUT_POST, 'version', FILTER_VALIDATE_FLOAT);
    $release_date = filter_input(INPUT_POST, 'release_date');

    // Validate the inputs
    if ( $code === NULL || $name === FALSE || 
            $version === NULL || $version === FALSE || 
            $release_date === NULL) {
        $error = "Invalid product data. Check all fields and try again.";
        include('../errors/error.php');
    } else {

        // Call the function defined in product_db.php to add a new product record
        add_product($code, $name, $version, $release_date);

        // Retrieve the updated list of products, now that one has been added
        $products = get_products();

        // Show the updated product list
        include('product_list.php');
    }
}
?>

In product_add.php, try changing:

<form action="product_list.php" method="post" id="aligned">

into

<form action="index.php" method="post" id="aligned">

You see, the way you have it set up, index.php in your code is serving as the main page that the end-user interacts with, and everything branches off from there. Therefore, we want to submit the form to index.php.

Let me know how that works for you!

This is working better than the code that I was using, but when I go to press the button I'm getting a blank page of the index.php instead of the product_list.php page

Sometimes you get a blank page when there's a fatal PHP error (syntax error, parse error, etc.

Add this to the very top of index.php and it should spit out any errors:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

Also, keep in mind, the code I typed out the other day was just typed here on DaniWeb in the reply textbox. I never checked it for errors.

I have added in the code and done error checking and I'm still having the same result

So let me get this straight ... when you hit the button for the add_product form, you would expect it to redirect to index.php and then, on line 6, retrieve the action 'add_product'. Then, you would expect it to trigger line 50 of index.php where it retrieves the code, name, version, and release date from the form. Then, you would expect it to call add_product() as on line 66 of index.php, then retrieve the list of products as on line 69, and then show the updated product list as on line 72. Is that all correct?

What I would do is set up little debugging echo statements along the way to see where the breakdown is.

For example, on line 4 of index.php I would add:

echo "Loaded index.php successfully<br>";

Then on line 19 of index.php I would add:

echo "Determined the action is $action<br>";

Then on line 64 of index.php I would add:

echo "Ready to add the product to the database<br>";

The expectation would be for all 3 of those statements to print out. If none print out, and you can confirm by your web browser that you are correctly at index.php, then I would say you have a PHP fatal error occurring in database.php

I'm needing the button to take the information that is put into the text boxes and go through the index.php file and output into the product_list.php file in the table that is supplied. From there, I should be able to delete the information that has been submitted onto that page

Oh the button you're referring to is for when the action is delete_product?

This should be correct (the way you have it already):

            <td><form action="index.php" method="post">
                <input type="hidden" name="action"
                       value="delete_product">
                <input type="hidden" name="product_code"
                       value="<?php echo htmlspecialchars($product['productCode']); ?>">
                <input type="submit" value="Delete">
            </form></td>

What happens when you test out adding different echo statements to debug your code as in my above post?

There's two buttons to this. The delete one and the add product button. I'm needing to get the add product button to take the information that is put into the text boxes and transfer it to the table on the product_list.php page, but it needs to go through the index.php page

I get that, and the changes I recommended for you should be doing that. If it's not working, I recommend adding debug echo statements.

When I added in the echo statements, it shows all of the other coding underneath in the index.php. It doesn't redirect to the desired page

You don't have any redirects in your code? Do you mean the include('product_list.php') file is never opened? Add an echo statement right before and right after the include, and see if they both run.

Yes I mean the include('product_list.php'). What should I be including in these echo statements?

The 3 echo statements I mentioned in my previous post, on the specific lines I mentioned.

When I do that I get this showing up when I press any button.

"; ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); $action = filter_input(INPUT_POST, 'action'); if ($action === NULL) { $action = filter_input(INPUT_GET, 'action'); if ($action === NULL) { $action = 'list_products'; } } echo "Determined the action is $action
"; if ($action == 'list_products') { $products = get_products(); echo include('product_list.php'); echo } else if ($action == 'delete_product') { $product_code = filter_input(INPUT_POST, 'product_code'); if ($product_code != false) { delete_product($product_code); } $products = get_products(); include('product_list.php'); } else if ($action == 'show_add_form') { include('product_add.php'); } else if ($action == 'add_product') { $code = filter_input(INPUT_POST, 'code'); $name = filter_input(INPUT_POST, 'name'); $version = filter_input(INPUT_POST, 'version', FILTER_VALIDATE_FLOAT); $release_date = filter_input(INPUT_POST, 'release_date'); // Validate the inputs if ( $code === NULL || $name === FALSE || $version === NULL || $version === FALSE || $release_date === NULL) { $error = "Invalid product data. Check all fields and try again."; include('../errors/error.php'); } else { add_product($code, $name, $version, $release_date); $products = get_products(); include("product_list.php"); } } ?> echo "Ready to add the product to the database
";

You have a syntax error. It looks like you didn't close a quote (") somewhere.

I've attached the code cause I cannot seem to find the syntax error. I have all of my quotes closed.

Move the ini_set() and error_reporting() function calls to right beneath the opening <?php and let me know what happens.

Here is your syntax error right here:

echo
    include('product_list.php');
echo

See, I told you there was definitely a syntax error :)

I'm still getting the same thing coming up when I press a button

Here is the updated coding

You didn’t change either of the things I said to change. I’m in the car now nowhere near a computer. When I get home in a few hours, I can try to make the corrections for you and show you updated code. In the meantime, try applying the two code changes I said to make in my last few posts.

Sorry, I forgot about you :)

You added all these statements that just say 'echo' but you need to complete the statement.

e.g.:

echo "String Here";

You can't just have

echo

on a line of its own, as you have multiple times in your code above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.