Hi All,

I am using the following code for form validation but i am getting an error saying that unexpected T_variable in line

<?php include("menu.inc");
      include("tiki-setup.php");
?>

<div id="centerContent">

<p class="first-letter">Please fill the following form in for any enquiries that you may have:</p>
<p>

<?php

// if the form has been posted, analyse it: 
if ($_POST) {
foreach ($_POST as $field => $value) {
$value = trim($value);
}

// creating variables
$inquiry=$_POST['inquiry'];
$title=$_POST['title'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$reply=$_POST['reply'];
$contact=$_POST['contact'];

// create empty error variable
$error = ""; 

// check for data in required fields
if (($inquiry == "") || ($title == "") || ($first_name == "") || ($last_name == "") || ($email == "") || ($phone == "") || ($message == "") || ($reply == "") || ($contact == "")) {
$error = "Please fill in all the required fields!";
}

// validate first_name
if ((ctype_alpha($first_name) == FALSE 
$error = "Please enter a valid name (Alphabets only)<br/>";
}

// validate surname
if ((ctype_alpha($last_name) == FALSE {
	$error = "Please enter a valid last name (Alphabets only)<br/>";
}

// validate e-mail
if ((strpos($email, "@") === FALSE || 
	(strpos($email, ".") === FALSE ||
	(strpos($email, " ") != FALSE || // DON'T KNOW WHY USING "!=" SIGN
	(strpos($email, "@") === FALSE || // DON'T KNOW WHY USING 3 "=" SIGN
	(strpos($email, "@") > strrpos($email, "."))) {
	$error = "Please enter a valid e-mail address<br/>";
}

// validate phone
if (is_numeric($phone) == FALSE {
	$error = "Please enter a valid contact number (must contain numbers only)<br/>";
}

//If everything is OK then Confirmation of each field's data as filled by the user
else {
echo "<p><b>Nature of Inquiry:</b> $inquiry<br/>";
echo "<p><b>Title:</b> $title<br/>";
echo "<p><b>First Name:</b> $first_name<br/>";
echo "<p><b>Last Name:</b> $last_name<br/>";
echo "<p><b>E-mail:</b> $email<br/>";
echo "<p><b>Contact No.:</b> $phone<br/>";
echo "<p><b>Message:</b> $message<br/>";
echo "<p><b>Reply:</b> $reply<br/>";
echo "<p><b>Contact Method:</b> $contact<br/>";

echo "Thanks for contacting us. We will get back to you shortly!";
}
}
$smarty->assign('mid', 'tiki-validate.tpl');
$smarty->display("tiki.tpl");
?>
</p>

<table class="table">

<tr class="tr">
<td class="td"><form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Nature of Enquiry:</td>
<td class="td"><select name="inquiry" value="<?php echo $inquiry ?>">
<option></option>
<option>General Inquiry</option>
<option>Price Quotation</option>
<option>Comments</option>
<option>Other</option>
</select></td>
</tr>

<tr class="tr">
<td class="td">Title:</td>
<td class="td"><select name="title" value="<?php echo $title ?>">
<option></option>
<option>Mr</option>
<option>Miss</option>
<option>Mrs</option>
<option>Other</option>
</select></td>
</tr>

<tr class="tr">
<td class="td">First Name:</td>
<td class="td"><input type="text" size="30" maxlength="30" name="first_name" value="<?php echo $first_name ?>"/></td>
</tr>

<tr class="tr">
<td class="td">Last Name:</td>
<td class="td"><input type="text" size="30" maxlength="30" name="last_name" value="<?php echo $last_name ?>"/></td>
</tr>

<tr class="tr">
<td class="td">E-mail:</td>
<td class="td"><input type="text" size="30" maxlength="30" name="email" value="<?php echo $email ?>"/></td>
</tr>

<tr class="tr">
<td class="td">Contact No.:</td>
<td class="td"><input type="text" size="30" maxlength="20" name="phone" value="<?php echo $phone ?>"/></td>
</tr>

<tr class="tr">
<td class="td">Message:</td>
<td class="td"><textarea rows="10" cols="50" wrap="physical" name="message" value="<?php echo $message ?>">
</textarea></td>
</tr>

<tr class="tr">
<td class="td">Reply Required:</td>
<td class="td"><input type="radio" name="reply" value="<?php echo $reply ?>"/>Yes
<input name="reply" type="radio"/>No
<input name="reply" type="radio"/>Maybe</td>
</tr>

<tr class="tr">
<td class="td">How would you like to be contacted (if required)?<br/><br/></td>
<td class="td"><input type="radio" name="contact" value="<?php echo $contact ?>"/>E-mail
<input type="radio" name="contact"/>Telephone</td>
</tr>

<tr class="tr">
<td class="td"></td>
<td class="td"><input type="reset" name="reset" value="Reset"/>
<input type="preview" name="preview" value="Preview"/></td>
<input type="submit" name="submit" value="Submit"/></td>
</form>
</tr>
</table>
</div>

Please help me on this......

Waiting for the reply...

Recommended Answers

All 11 Replies

Hi..
In validating phone you missed out one bracket { in the if condition

// validate phone
if (is_numeric($phone) == FALSE ) {
$error = "Please enter a valid contact number (must contain numbers only)<br/>";
}

Oops i was trying to highlight the ) bracket (not curly one { )
I'll paste code here again..

// validate phone
if (is_numeric($phone) == FALSE ) {
$error = "Please enter a valid contact number (must contain numbers only)<br/>";
}

Thanx a lot for ur reply i have tried with that and the error is solved but i am getting one more error

syntax error, unexpected '{' on line

i,e in this code

// validate e-mail
if ((strpos($email, "@") === FALSE || 
	(strpos($email, ".") === FALSE ||
	(strpos($email, " ") != FALSE || // DON'T KNOW WHY USING "!=" SIGN
	(strpos($email, "@") === FALSE || // DON'T KNOW WHY USING 3 "=" SIGN
	(strpos($email, "@") > strrpos($email, "."))) 
{
	$error = "Please enter a valid e-mail address<br/>";
}

Please help me on this......

Hi..

Always be very careful when you use so many conditions in if..

Count the no of opening brackets and closing brackets.. opening brackets are greater then closing brackets in if conditions...

Thats why getting { as unexpected error..
Here is the correct code..

<?php
// validate e-mail
if (
	(strpos($email, "@") === FALSE) || 
	(strpos($email, ".") === FALSE) ||
	(strpos($email, " ") != FALSE) || // DON'T KNOW WHY USING "!=" SIGN
	(strpos($email, "@") === FALSE) || // DON'T KNOW WHY USING 3 "=" SIGN
	(strpos($email, "@") > strrpos($email, "."))
   ) 
{
$error = "Please enter a valid e-mail address<br/>";
}
?>

Hi

Thank you very much for ur support.
Everything is fine the code is working but the errors are not able to display for a particular field.

I want to validate every field and if it is not valid i need to display the error message for that particular filed this was not happening.

Kindly request to guide me with code to display error..

Waiting for the reply.....

heres the programming part of the code. i have fixed it for u and have also commented in areas where i thought that ought to be explained.

however, i haven't checked if the code is working on the system, but i have done what i could do.

<?php include("menu.inc");

      include("tiki-setup.php");
   
      ?>
      <div id="centerContent">
       
      <p class="first-letter">Please fill the following form in for any enquiries that you may have:</p>
      <p>
   
      <?php
      
      // if the form has been posted, analyse it:
  
	  if ($_POST) {

	  foreach ($_POST as $field => $value) {

	  $value = trim($value);
	      }

       

      // creating variables

	  //if the user has just entered space in the field and has not enetered any text then trim will remove that space. basically trim removes leading spaces.
	  
      $inquiry=trim($_POST['inquiry']);

      $title=trim($_POST['title']);

      $first_name=trim($_POST['first_name']);

      $last_name=trim($_POST['last_name']);

      $email=trim($_POST['email']);

      $phone=trim($_POST['phone']);

      $message=trim($_POST['message']);

      $reply=trim($_POST['reply']);

      $contact=trim($_POST['contact']);

       

      // create empty error variable

//    $error = " "; //write space for empty values.... but in php you do not have to declare a variable.

       

      // check for data in required fields


//i am not exactly sure if you can use FALSE in the conditions because i use the NOT operator (!) to check if any field is invalid. though i have left that unchanged. i have fixed the brakets problem as well.

//i am using empty() function to check if the variables are empty or not. the way you were checking might not have worked for empty values, because usually we write space  for empty values but you used something else. so i thing that it was here why your individual fields were not being checked properly.

      if (empty($inquiry ) || empty($title ) || empty($first_name ) || empty($last_name) || empty($email) || empty($phone ) || empty($message) || empty($reply ) || empty($contact )) 
	  {

      $error = "Please fill in all the required fields!";

      } //if any field is empty, that is nothing has been typed in the field then display an error.

       

      // validate first_name

      if (ctype_alpha($first_name) == FALSE))
	  {
 
      $error = "Please enter a valid name (Alphabets only)<br/>";

      }

       

      // validate surname

      if (ctype_alpha($last_name) == FALSE) 
	  {

      $error = "Please enter a valid last name (Alphabets only)<br/>";

      }

       

      // validate e-mail

      if ( 
	  
	  (strpos($email, "@") == FALSE) ||

      (strpos($email, ".") == FALSE) ||

      (strpos($email, " ") != FALSE) || // !=  means not equals to.
 
      (strpos($email, "@") == FALSE) || // you will use == only. using === is syntax error as far as i know.

      (strpos($email, "@") > strrpos($email, "."))) 
	  
	  {
 
      $error = "Please enter a valid e-mail address<br/>";

      }
 
       
 
      // validate phone
 
      if (is_numeric($phone) == FALSE) 
	  {
  
      $error = "Please enter a valid contact number (must contain numbers only)<br/>";
  
      }

       

      //If everything is OK then Confirmation of each field's data as filled by the user

	  //do not use else for the moment. you have not included an if statement to go with the else statement. right now  the following lines will execute when all your checks have been made.
    //  else {

      echo "<p><b>Nature of Inquiry:</b> $inquiry<br/>";

      echo "<p><b>Title:</b> $title<br/>";

      echo "<p><b>First Name:</b> $first_name<br/>";

      echo "<p><b>Last Name:</b> $last_name<br/>";
 
      echo "<p><b>E-mail:</b> $email<br/>";

      echo "<p><b>Contact No.:</b> $phone<br/>";

      echo "<p><b>Message:</b> $message<br/>";

      echo "<p><b>Reply:</b> $reply<br/>";

      echo "<p><b>Contact Method:</b> $contact<br/>";

       

      echo "Thanks for contacting us. We will get back to you shortly!";

      //}

      }

      $smarty->assign('mid', 'tiki-validate.tpl');

      $smarty->display("tiki.tpl");

      ?>

      </p>

=== is not a syntax error. It checks to make sure they are the same value and the same type.

$number = 1;
$string = "1";
if($number === $string) {
  echo "They match!"; // This won't happen
} else {
  echo "They don't match!"; // This will happen
}

Hi,

The code for form validation is working and i need to save the data into database once the data is validated.

can anyone please help me with the code to add in form validation code..

Advance thanx....

Hi..
Here's the code to add to the database

<?php
// if the form has been posted, analyse it: 
if ($_POST) {
foreach ($_POST as $field => $value) {
$value = trim($value);
}

// creating variables
$name=$_POST['name'];
$offadd=$_POST['offadd'];
$boardtelno=$_POST['boardtelno'];
$admmail=$_POST['admmail'];
$contacts1=$_POST['contacts1'];
$contacts2=$_POST['contacts2'];
$billingadd=$_POST['billingadd'];

// create empty error variable
$error = "";

// check for data in required fields
if (($name == "") || ($offadd == "") || ($boardtelno == "") || ($admmail == "") || ($contacts1 == "") || ($contacts2 == "") || ($billingadd == "")) {
//echo $error = "Please fill in all the required fields!";//
$smarty->assign('msg', tra("Please fill in all the required fields!"));
$smarty->display("error.tpl");
die;
display("form");
}

else
if ((ctype_alpha($name) == FALSE)) 
echo $error = "Please enter a valid name (Alphabets only)<br/>";
if ((ctype_alpha($offadd) == FALSE)) {
echo $error = "Please enter a valid off address (Alphabets only)<br/>";
}
if (is_numeric($boardtelno) == FALSE ) {
echo $error = "Please enter a valid boardtelno (must contain numbers only)<br/>";
}
if (
(strpos($admmail, "@") === FALSE) ||
(strpos($admmail, ".") === FALSE) ||
(strpos($admmail, " ") != FALSE) || // DON'T KNOW WHY USING "!=" SIGN
(strpos($admmail, "@") === FALSE) || // DON'T KNOW WHY USING 3 "=" SIGN
(strpos($admmail, "@") > strrpos($admmail, "."))
)
{
echo $error = "Please enter a valid e-mail address<br/>";
}
if (is_numeric($contacts1) == FALSE ) {
echo $error = "Please enter a valid contacts1 number (must contain numbers only)<br/>";

}
if (is_numeric($contacts2) == FALSE ) {
echo $error = "Please enter a valid contacts2 number (must contain numbers only)<br/>";

}
if ((ctype_alpha($billingadd) == FALSE)) {
echo $error = "Please enter a valid billing address (Alphabets only)<br/>";

}

/////////////////////////////
//Adding the data to database
/////////////////////////////

//Enter your mysql server details
$hostname="localhost";
$username="root";
$password="";
$database_name="forms";

//Step1 : Connect to the database
mysql_connect($hostname,$username,$password) or die("Error in connection".mysql_error());
//Step2: Select the database
mysql_select_db($database_name) or die("Error in database selection".mysql_error());

//Step3: Write the sql query to insert into the database
$sql = "insert into form_table(name,offadd,boardtelno,admmail,contacts1,contacts2,billingadd) 
                        values('$name','$offadd','$boardtelno','$admmail','$contacts1','$contacts2','$billingadd')";
						
//Step4: Finally execute this query
mysql_query($sql);

echo "Successfully written the following data to database<br />";
echo "<p><b>Name:</b> $name<br/>";
echo "<p><b>Offadd:</b> $offadd<br/>";
echo "<p><b>boardtelno:</b> $boardtelno<br/>";
echo "<p><b>admmail:</b> $admmail<br/>";
echo "<p><b>contacts1:</b> $contacts1<br/>";
echo "<p><b>Contacts2.:</b> $contacts2<br/>";
echo "<p><b>Billingadd:</b> $billingadd<br/>";
display("form");
}
?>

Hi,

In the below code i am saving the data into database once the data is validated.

But even the data is invalid also it is saving to database.

Can anyone help me....

if (!isset($_REQUEST["clientid"]))
	$_REQUEST["clientid"] = 0;

if (isset($_REQUEST["delete"]) && isset($_REQUEST["clients"])) {
	check_ticket('clients');
	foreach (array_keys($_REQUEST["clients"])as $clients) {
		$clientslib->remove_clients($user, $clients);
	}

	if (isset($_SESSION['clients']))
		unset ($_SESSION['clients']);
}


if ($_REQUEST["clientid"]) {
	$info = $clientslib->get_clients($user, $_REQUEST["clientid"]);
} 




// if the form has been posted, analyse it: 
if ($_POST) {
foreach ($_POST as $field => $value) {
$value = trim($value);
}

// creating variables
$name=$_POST['name'];
$offadd=$_POST['offadd'];
$boardtelno=$_POST['boardtelno'];
$admmail=$_POST['admmail'];
$contacts1=$_POST['contacts1'];
$contacts2=$_POST['contacts2'];
$billingadd=$_POST['billingadd'];

// create empty error variable
$error = "";

// check for data in required fields
if (($name == "") || ($offadd == "") || ($boardtelno == "") || ($admmail == "") || ($contacts1 == "") || ($contacts2 == "") || ($billingadd == "")) {
	//echo $error = "Please fill in all the required fields!";//
	$smarty->assign('msg', tra("Please fill in all the required fields!"));
	$smarty->display("error.tpl");
	die;
	display("form");
}

else
if ((ctype_alpha($name) == FALSE)) 
echo $error = "Please enter a valid name (Alphabets only)<br/>";
if ((ctype_alpha($offadd) == FALSE)) {
	 echo $error = "Please enter a valid off address (Alphabets only)<br/>";
}
if (is_numeric($boardtelno) == FALSE ) {
	echo $error = "Please enter a valid boardtelno (must contain numbers only)<br/>";
}
if (
(strpos($admmail, "@") === FALSE) ||
(strpos($admmail, ".") === FALSE) ||
(strpos($admmail, " ") != FALSE) || // DON'T KNOW WHY USING "!=" SIGN
(strpos($admmail, "@") === FALSE) || // DON'T KNOW WHY USING 3 "=" SIGN
(strpos($admmail, "@") > strrpos($admmail, "."))
)
{
echo $error = "Please enter a valid e-mail address<br/>";
}
if (is_numeric($contacts1) == FALSE ) {
	 echo $error = "Please enter a valid contacts1 number (must contain numbers only)<br/>";
	
}
if (is_numeric($contacts2) == FALSE ) {
	echo $error = "Please enter a valid contacts2 number (must contain numbers only)<br/>";
	
}
if ((ctype_alpha($billingadd) == FALSE)) {
	  echo $error = "Please enter a valid billing address (Alphabets only)<br/>";
	
}

else
/////////////////////////////
//Adding the data to database
/////////////////////////////
//Enter your mysql server details
$hostname="localhost";
$username="root";
$password=" ";
$database_name="tiki";

//Step1 : Connect to the database
mysql_connect($hostname,$username,$password) or die("Error in connection".mysql_error());

//Step2: Select the database
mysql_select_db($database_name) or die("Error in database selection".mysql_error());

//Step3: Write the sql query to insert into the database
$sql = "insert into tiki_emclients(user,name,offadd,boardtelno,admmail,contacts1,contacts2,billingadd)
values('$user','$name','$offadd','$boardtelno','$admmail','$contacts1','$contacts2','$billingadd')";

//Step4: Finally execute this query
mysql_query($sql);
echo "Successfully written the following data to database<br />";
echo "<p><b>Name:</b> $name<br/>";
echo "<p><b>Offadd:</b> $offadd<br/>";
echo "<p><b>boardtelno:</b> $boardtelno<br/>";
echo "<p><b>admmail:</b> $admmail<br/>";
echo "<p><b>contacts1:</b> $contacts1<br/>";
echo "<p><b>Contacts2.:</b> $contacts2<br/>";
echo "<p><b>Billingadd:</b> $billingadd<br/>";
display("form");
}

Hi,

The above code for form validation is working and i want to validate the data at the field itself how to validate that data.

while entering the data if it is not valid it should not allow to enter the data into that field.Please can anyone help me on this.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.