Reputation: 781
I am practicing with PHP and trying to build Validation for my HTML register form, My idea is to create PHP function for each input field within my HTML form and call it if the field has been left out empty.
Am at a stage where everything is ready but I dont know how to call my functions within HTML...
Could some one sugest a way of calling a functions withing HTML form.
session_start();
if(isset($_POST['reg'])){
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$tel = mysql_real_escape_string($_POST['telephone']);
$firstname = mysql_real_escape_string($_POST['firstname']);
}
class Validation{
public function FirstName(){
if($firstname == NULL){
echo '<p>Enter Name</p>';
}
}
public function LastName(){
if($lastname == NULL){
echo '<p>Enter \surname</p>';
}
}
public function EmailAdress(){
if ($email == NULL){
echo '<p>Enter Email</p>';
}
}
public function TelephoneNumber(){
if($tel == NULL){
echo '<p>Enter Name</p>';
}
}
}
?>
<!-- banner -->
<div class="hero-unit banner-back">
<div class="container">
<div class="row-fluid">
<div class="span8">
</div>
<div class="span4 form-back text-center">
<form name="reg" action="" method="post">
<fieldset>
<legend>Free Workshop</legend>
<div class="input-wrapper">
<input type="text" placeholder="First Name" id="firstname" name="firstname <? FirstName(); ?>"/>
</div>
<div class="input-wrapper">
<input type="text" placeholder="Last Name" id="lastname" name="lastname"/>
</div>
<div class="input-wrapper">
<input type="email" placeholder="Email Address" id="email" name="email"/>
</div>
<div class="input-wrapper telephone">
<input type="text" placeholder="Telephone number" id="telephone" name="telephone"/>
</div>
<div class="input-wrapper">
Upvotes: 0
Views: 67
Reputation: 2145
You cannot call PHP from HTML after you delivered the Website to the Browser. (well you can with AJAX, but that would be some overkill). When the user does some input to the fields the page is not sent to your server (where PHP is interpreted).
So I suggest you do the validation client side in JavaScript.
To output PHP Variables or the return of a Function call simply use the <?= myFunction() ?>
syntax.
Upvotes: 1