Archive for the ‘PHP’ Category

How to create a simple form in PHP

Posted: January 13, 2011 in PHP, Programming

Forms are the basic stuff in every website, either that is a part of a poll or of a sign up form.

So how easy it is to create one? It’s quite easy!

In the following example, i will create a form that includes input fields, text areas, check boxes, radio buttons, drop down lists, and a input button.

So firstly, i have to create my form. As I will send all the data of my fields into the same page, i will use the following block of code to create it.

 

<html>
<head>
<title> Form example </title>
</head>

</body>
<!– creating a form –>
<form method=”post” action=”<?php echo $PHP_SELF;?>”>

</form><br />

</body>
</html>

 

As a posting action I have used $PHP_SELF that will send all the form’s data to the same page the form is.

Then I will add 2 input areas by this

<!–input fields–>
First name: <input type=”text” size=”12″ maxlength=”12″ name=”Fname”><br>
Last name: <input type=”text” size=”12″ maxlength=”12″ name=”Lname”><br><Br>

 

Some radio buttons (they have the same name value so as to keep one of their values)

<!–radio buttons –>
Gender: <br>
Male <input type=”radio” value=”Male” name=”gender”><br>
Female <input type=”radio” value=”Female” name=”gender”><br><Br>

 

Then, some check boxes (in the name attribute the value is an array so as to keep, if necessary, more than one of their values)

<!– checkboxes –>
Favourite Food: <BR>
Pizza: <input type=”checkbox” value=”Pizza” name=”food[]”><br>
Chicken: <input type=”checkbox” value=”Chicken” name=”food[]”><Br>
Chinese Food: <input type=”checkbox” value=”Chinese food” name=”food[]”><Br><br>

 

Maybe,  a drop down list (use select to define the list, and option to define each of the list’s items)

<!– dropdown list –>
Select a Level of Education:<br />
<select name=”education”>
<option value=”Jr.High”>Jr.High</option>
<option value=”HighSchool”>HighSchool</option>
<option value=”College”>College</option>
</select><br />

 

Even, a text area

<!– textarea –>

<br>
<textarea name=”quote” rows=”4″ cols=”20″ wrap=”physical”>Insert your favorite quote! </textarea>
<br><Br>

 

And finally an input button

<!– inpute button –>
<input type=”submit” value=”submit” name=”submit”><br />

 

So… what’s after creating the form?? Well now we have to write the php code that will create the two views of this page (the form to be filled and the results page)

To get all the data that were posted in the form and were submited you can use this:

<?php
$Fname = $_POST[“Fname”];
$Lname = $_POST[“Lname”];
$gender = $_POST[“gender”];
$food = $_POST[“food”];
$quote = $_POST[“quote”];
$education = $_POST[“education”];

?>

$_POST[“Anything”] will get the value included in Anything object after the submit button is pressed.

To create the two views of this page, we use the isset() function. If not true then the page must show the first form. If it returns true it must show the results.

The final block of code that will get the job done is the following:

 

<?php
$Fname = $_POST[“Fname”];
$Lname = $_POST[“Lname”];
$gender = $_POST[“gender”];
$food = $_POST[“food”];
$quote = $_POST[“quote”];
$education = $_POST[“education”];

if(!isset ($_POST[“submit”]))
{
?>

<html>
<head>
<title> Form example </title>
</head>

</body>
<!– creating a form –>
<form method=”post” action=”<?php echo $PHP_SELF;?>”>
<!–input fields–>
First name: <input type=”text” size=”12″ maxlength=”12″ name=”Fname”><br>
Last name: <input type=”text” size=”12″ maxlength=”12″ name=”Lname”><br><Br>

<!–radio buttons –>
Gender: <br>
Male <input type=”radio” value=”Male” name=”gender”><br>
Female <input type=”radio” value=”Female” name=”gender”><br><Br>

<!– checkboxes –>
Favourite Food: <BR>
Pizza: <input type=”checkbox” value=”Pizza” name=”food[]”><br>
Chicken: <input type=”checkbox” value=”Chicken” name=”food[]”><Br>
Chinese Food: <input type=”checkbox” value=”Chinese food” name=”food[]”><Br><br>

<!– dropdown list –>
Select a Level of Education:<br />
<select name=”education”>
<option value=”Jr.High”>Jr.High</option>
<option value=”HighSchool”>HighSchool</option>
<option value=”College”>College</option>
</select><br />

<!– textarea –>

<br>
<textarea name=”quote” rows=”4″ cols=”20″ wrap=”physical”>Insert your favorite quote! </textarea>
<br><Br>

<!– inpute button –>
<input type=”submit” value=”submit” name=”submit”><br />
</form><br />

</body>
</html>

<?php
}
else {
echo “Thanks for adding your information <br>”;
echo “Your name is “.$Fname.”<br>” ;
echo “Your Lastname is “.$Lname.”<br>”;
echo “Your Gender is “.$gender.”<br>” ;
echo “Your favorite food is “;

foreach ($food as $f) {
echo “<br />”.$f;
}

echo “<br>”.”Your favorite quote is <i> “.$quote.”</i><br>” ;
echo “And your level of education is “.$education.”<br>”;
}
?>

 

Advertisements

All websites include  various meta tags, such as for description, keywords etc.

An easy way to values of this meta tags is by using the following command:

get_meta_tags (‘website-url’);

So a simple example of getting meta tags from a certain site is the following:

<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />

</head>

</body>

<?php
$tags = get_meta_tags(‘ website-url ‘);

echo $tags[‘description’].”<br><br>”;
echo $tags[‘keywords’].”<br><br>”;
echo $tags[‘generator’].”<br><br>”;

?>

</body>
</html>

The above block of code would retrieve the meta tags with names “description” , “keywords” and “generator” from the site given in website-url, and print their values on screen.

The meta tag given in the code for the content type is used for UTF-8 character set. If you want to use a different one or the results of your code are unreadable, then try some other from this list which includes most of them.

Some of the most common ones are the following 3:

<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />

<meta http-equiv=”Content-Type” content=”text/html; charset=Windows-1253″>

<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>?

Array invertion in PHP

Posted: January 12, 2011 in PHP, Programming

In the following example i will try to explain how to invert an array in php. Obviously there is an easier way using the array_reverse command

$result = array_reverse($array_name);

The above line will return the reversed array of $array_name into $result.

Another way would be to do this from scratch. The following block of code will first create an array and print it on screen, and then reverse the array and print it again.

<html>
<head>
</head>

<body >
<!– this is a simple example to show how to invert an array in php –>
<p>
<?php
//creating the array and filling it with strings
$array = array(“fox”, “dog”, “cat”, “mouse”);
print_r ($array);

//inverting the array and then printing again
//count command returns the size of the array
for($i=0 ; $i < count($array)/2; $i++)
{
$temp = $array[$i];
$array[$i] = $array[count($array) – $i – 1];
$array[count($array) – $i – 1] = $temp;

}
echo “<br><br>”;
print_r($array);
?>
</p>
</body>

After saving and running the above block of code, the result is the following:

Array ( [0] => fox [1] => dog [2] => cat [3] => mouse )

Array ( [0] => mouse [1] => cat [2] => dog [3] => fox )