Jump to content

perl to PHP


bal bal

Recommended Posts

Hi I am new in PHP, I did this using perl but don't know how to do the form validation using PHP. here is the code for perl. can anyone please help me how can I do the form validation using PHP. I have got "text box" and "select" from the list in the PHP form.

Thanks in advance.

 

#!/usr/bin/perl - w

use CGI ':standard';
use CGI::Carp "fatalsToBrowser";
use DBI;

print header;

$forename=param('txtfname');
$surname=param('txtsurname');
$day=param('lstday');
$month=param('lstmonth');
$year=param('lstyear');
$passport=param('txtpassport');
$gender=param('lstgender');
$email=param('txtemailadd');
$phone=param('txtphone');
$galaxy=param('lstgalaxy');
$flightdate=param('dptdate');
$flightmonth=param('dptmonth');
$flightyear=param('dptyear');

print "<html> <body bgcolor=\"burlyWood\">";

if ($forename !~/\A[A-Z][a-z]+\Z/i)     
{
 print "Please enter a valid forename<br>";
 print "</body></html>";
}
else	{
print "Forename: $forename <br>";
} 

if ($surname !~/\A[A-Z][a-z]+\Z/i)     
{
 print "\n Please enter a valid Surname <br>";
 print "</body></html>";
}
else	{
print "\n\nSurname: $surname <br>";

} 

if ($day !~m/\d/)
{
 print "Please choose the valid date<br>";
 print "</body></html>";
}
else 	{
print "Date of Birth "; 
print "$day/"; 
}

if ($month !~m/\d/)
{
 print "Please choose the valid month<br>";
 print "</body></html>";
}
else 	{
print "$month/"; 
}

if ($year !~m/\d/)
{
 print "Please choose the valid year<br>";
 print "</body></html>";
}
else 	{
print "$year<br>"; 
}

if ($passport !~m/\d/)
{
 print "Please enter 4 digits(0 to 9)";
 print "</body></html>";
}
else 	{
print "Passport Number: $passport <br>"; 
} 

if ($gender !~/\A[A-Z][a-z]+\Z/i)
{
 print "Please choose a gender<br>"
}
else
{
print"Gender: $gender<br>";
}
if ($email !~m/\A[A-Z][a-z]|d+\@\[A-Z][a-z]|d+\.\[A-Z][a-z]|d+\z/i)
{
 print "Please enter a valid email address<br>";
}
else
{
 print "Email: $email<br>";
}
if ($phone !~m/\A\d+\Z/)
{
 print "Please enter a valid phone number<br>";
 print "</body></html>";
}
else 	{
print "Phone Number: $phone <br>"; 
} 

if ($galaxy !~/\A[A-Z][a-z]+\Z/i)
{
 print "Please choose one from the list<br>"
}
else
{
print"Galaxy: $galaxy<br>";
}

if ($flightdate !~m/\d/)
{
 print "Please choose the valid date<br>";
 print "</body></html>";
}
else 	{
print "Departure Date ";
print "$flightdate/"; 
}

if ($flightmonth !~m/\d/)
{

 print "Please choose the valid month<br>";
 print "</body></html>";
}
else 	{

 print "$flightmonth/"; 
}

if ($flightyear !~m/\d/)
{
 print "Please choose the valid year<br>";
 print "</body></html>";
}
else 	{
print "$flightyear<br>"; 
}

Link to comment
Share on other sites

The HTML side will be the same. As you're probably aware the only thing that will be different is the scripting behind it.

 

<?php
//get form variables
$name=$_POST['name'];
//validate
if ($_POST['subsend']) {
  if ($name!='') {
    //name passed verification
  } else {
    echo 'Name must be specified';
  }
}
<html>
<head>
  <title>Test</title>
</head>
<body
  <form action="" method="post">
    Name: <input type="text" name="name" /><br />
    <input type="submit" value="Send Details" name="subsend" />
  </form>
</body>
</html>

 

That's the very basics.

Link to comment
Share on other sites

Depending on what type of inbox box you're using on your form (and what data it contains) depends really on how you validate the data - and how strict you want to be with it.

 

For example, validating something like a name is easy - either it has content or it doesn't. This can be checked using empty()

if (empty($name)) {echo 'We are empty!';}

 

For dates you can very specific or relaxed. Does it matter what order the d/m/y are in or can they use whatever format they want?

Link to comment
Share on other sites

Depending on what type of inbox box you're using on your form (and what data it contains) depends really on how you validate the data - and how strict you want to be with it.

 

For example, validating something like a name is easy - either it has content or it doesn't. This can be checked using empty()

if (empty($name)) {echo 'We are empty!';}

 

For dates you can very specific or relaxed. Does it matter what order the d/m/y are in or can they use whatever format they want?

 

thanks for the reply.

Just to make it a bit clear.

I have got a textbox for name and another one for email address and a select button (value is cat, cow, dog) from where I will select one and validate the user name and email address.

so the name field should not contain any digit and the email address should be a valid email address...

thanks once again

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.