In this post, l want to discuss how to simply create a form and process the form on the same page in Php.

For this example, we are going to create a form that will take information such as first name, last name and age.

Another requirement we want to implement is that, the age entered must not be less than 13, the first name and last name can not be left empty and they can’t be the same either. That means, we want the first name to be different from the first name. Well, this is just an example, there are some people who actually have the same name as first and last. yeah, they were born when all the names in the world were taken lol. ok, so we are going to create  our form first.

<form name = “information” action = “<? $_SERVER[‘PHP_SELF’]; ?>” Method= “POST”/>

<input name = “firstname” type  =”text” value =”Enter your first name here” onFocus=”this.value =’ ‘ “/>

<input name = “lastname”  type = “text” value =”Enter your last name here” onFocus = “this.value =’ ‘ “/>

<input name =”age” type =”text” value = “Enter your age here” onFocus =”this.value =  ‘ ‘ ” />

<input name =”submitInfo” type = “submit” value =”Submit”/>

That is the end of the form. Let me quickly go through the components of the form in case someone need that information to understand what is going on.

So l started the form with the html tag form, it can have a name, action, method and some other options. I gave it a name, it’s not necessary in this tutorial but it can help especially if you are using javascript to check user input and stuff rather than using php. That remind me of a question l was asked by a colleague of mine in class last time. What is the difference between Javascript and Php? well, I told him and l’m telling you now in case you don’t know it, that Javascript is for client side scripting and Php is server side scripting. This simply means, when you write a javascript code, it works directly on the browser, as far as it supports and enabled javascript. That means, javascript can take care of some minor stuff right on the user’s browsers without the user request being sent to the server to get a reply. Php on the other hand is for server side scripting. That means, it only works on the server and only works on request that comes to the server and called it. For example, when you are entering data on a site, javascript can be used to validate the texts you enter into those fields, so that when you enter for example, a wrong format of the date or entered less amount of characters for a password field, it will inform you right there. No request will be send to the server to check if you entered the right amount of characters in the password field. But when data is submitted, for security and because javascript can be disabled in a browser, a file Php check on the server is performed in most situations before finally performing the requested operation, such as creating a new user or reseting the password. Ok, enough of the blah, let’s go back to what we are doing.

So the form has a method option which tells it how to submit the form, there are two types, get and post. I used post in this form because l want it posted. The action option in the form has a php script which simply means this form will be processed right here. If there is another file on the server that will process this form, then you have to put that file name instead.

I hope the other parts of the form are clear, if not, please leave comment and l’ll explain it.

Let’s go to the php script that will handle this form on the same page. What l mean by on the same page is that, the form and the handling script are all in the same file. You know pages are files store on the server, which is served though an http request. So a page is a file but a file is not always a page lol.

Let’s assume l have a page called info.php on 2smart4school and l wrote that form creation script in html to display on the page, then what l mean on the same page is that, l’m going to write the php script to handle the form on the same page but that won’t be displayed to the user to see.

Ok, l don’t know how much more to explain this to make sure it’s clearer. l’m going to continue and assume it’s clear by now.

Here is the script now.

<?Php

if( $_POST[‘submitInfo’] == Submit)    //This means if the person clicked on the Submit button

{//Then we are going to do the following

$firstname = $_POST[‘firstname’]; // create a variable to take the submitted first name

$lastname = $_POST[‘lastname’]; //create a variable to take the submitted last name

$age = $_POST[‘ age’];// create a variable to take the age submitted

//Now let’s perform checks on the values submitted and make sure they meet our initial requirement

if($age == null || $firstname == null || $lastname == null)

{//One of the fields have been left blank, we don’t allow that.

echo “Please provide all information required”;

}else if($lastname==$firstname)

{// last name and first name are the same, we don’t allow that

echo “Please, we don’t allow last and first name to be the same”;

}else if($age < 13)

{// This person is too young, we don’t allow under 13 yrs

echo “Please, you are too young to see or register “;

}else

{// At this point, the information submitted is good for us. so we continue to use it

//assume we are creating an account for this user, then we have to connect to the database and create an account.

//lets assume you have a database and already connected to it in this file, and you want to put this new user into the members table.

// we also assume the members table has columns firstname, lastname and age.

mysql_query(“INSERT INTO members_table(firstname,lastname,age) VALUES(‘$firstname ‘,’$lastname ‘,’ $age’)  “);

// you have just inserted that user into the members database.

//do you want to make sure that user is actually created before you tell him or her, successfull?

// use this magic below

if( mysql_affected_rows() ==1)

{//if one row has been affected in the database, then we are successful inserting the user

echo ” Congratulation, you have been added to our system”;

}else   // this creation failed. blame it on the buzz lol

{

echo “Sorry, we couldn’t create an account for you no, please try again “;

}

}

}

//end of the php script

?>

So that is it, simple and cool to know. Any questions, corrections or feedbacks, please leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *