How would I implement this?

Hey All,

I am in the process of learning PHP so I am not an expert by any means. I am looking for some advice on how I would go about implementing this idea.

  1. I have a page called addproduct.php with a form and which also includes the header.php file
  2. when the form is submitted on the addproduct.php it goes to the database processing script called addproduct-func.php,
  3. the addproduct-func.php page has a conditional that checks to see if the database query succeeded if so it redirects using the header function back to addproduct.php.

however what I would like to implement is some form validation and checking before it gets submitted to the addproduct-func.php and only if validation and form checking passes then goto addproduct-func.php.

I also would like to output a little success msg informing the user that the record had been added to the database just above the addproduct.php form.

I am using bootstrap as my css framework.

Many Thanks for your time and help


Hi J,

What I normally do is handle the validation and form processing in the same action that I use to display the form. The flow works something like this:


If request is a POST request:
    do validation
    If validation passes:
        save data
        redirect to product list (for example)
        get errors to pass to view
    display form view (passing in errors/form data if necessary)

If validations fails, it’s simple to redisplay the form pre-filled with the submitted data and the error messages.

Thanks fretburner,

I getting that little be closer but I have a strange issue that I cant seem to solve

I am having some issues my data gets added to the database ok but since I am using a header redirection (to clear the $_post variable) my success message is not displayed or my error if there is an error.

Basically I would like my message to be displayed just above the form to say success it worked or error if it did not.
header(“Location: addproduct.php”);
echo ’ <div class=“span9” id=“content”>
<div class=“alert alert-success”>
<div id=“success”><p>Product has been added to the database</p></div>
the above code is a snippet of my main code I am also using ajax to help with this.

my simple ajax code
$(‘#btnSave’).click(function() {
$.post($(“#addproduct”).attr(“action”), data, function(info){ $(“#success”).html(info); });

$(“#addproduct”).submit( function() {
return false;

function clearInput() {
$(“#addproduct :input”).each( function() {

Many Thanks


Hi J,

If you’re submitting the form by ajax, then you don’t need to be doing a redirect in your code… that’s going to mess things up. I’d remove that and just return your success/error message.

Hi Fretburner,

sorry I am bit of a noob at this do you mean remove the ajax or the header redirection as the reasson why i added the redirect was to stop the page be refreshed and the data being resubmitted into the database,

do you know of any tutorials that mite be able to help with this so I am able to read and learn.

and thanks


Yeah doing redirect after post avoids users hitting the reload button in their browser and resubmitting the data but that’s only for normal (non-ajax) requests, but if you’re submitting via ajax then there’s no way for the user to refresh the page… JS sends the request to the page with the data and the page sends a response, the orginal request is discarded along with the post variables.

There are non I can think of off the top of my head - you can find many just by searching google but you have to be careful as there are many, many tutorials out there which promote out-of-date (or just plain bad) practices. What is it specifically that you want to learn more about, ajax? or building CRUD (create/read/update/delete) apps with PHP?

The other alternative of course is to stick around the forums here and we’ll do our best to give you a hand whenever you get stuck :slight_smile: