PHP FORM | w/immediate field validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
passam
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 6:55 pm

PHP FORM | w/immediate field validation

Post by passam »

PHP FORM | w/immediate field validation


Hi

I am trying to complete a form with an error report in case a sender forgot to complete a name field or any other fields. I was hoping to have an error

post immediately after the submit button is pushed. But I do not want to redirect to an error page. Just to have a red colored error description appear

above the field. What happens? After the submit button is pushed, page changes and gives out an error on line 42. Which is where the line

if{$error=="") starts. Please help! After four days of trying I give up. I need help. I cant take this anymore! I striped all the styling for quick

appearance here.


Code: Select all

 
 
 
 
<form action="feedback.php" method="post">
    
 
    <label>Contact`s Name:</label><input name="name" type="text" value="" style="width: 35%;" ><br>
 
    <label>Contact`s Number:</label><input name="number" type="text" value="" style="width: 35%;" ><br>
 
    <label>Email Address:</label><input name="email" type="text" value=""  style="width: 35%;" ><br>
 
    <label>Comments</label><br><textarea name="comments" class="input" value="" rows="5" cols="35" style="width: 60%;" ></textarea><br>
  
    <label>best time to call</label><input name="time" type="text" value=""  style="width: 25%;" ><br>   
 
    <input name="check[]" type="checkbox" value="I am requesting a callback for someone esle" >I am requesting a callback for someone esle<br>
    
    <input type="submit" value="Submit"><input type="reset" value="Reset">
 
    </form>
 
 
 

Code: Select all

 
<?php
 
 
// ------------- CONFIGURABLE SECTION ------------------------
 
$mailto = 'passam@gmail.com' ;
$subject = " e-mail | requesting call back for: mysite.com" ;
$formurl = "http://www.mysite.com/index.htm" ;
$errorurl = "http://www.mysite.com/missing.htm" ;
$thankyouurl = "http://www.mysite.com/thankyou.htm" ;
$email_is_required = 1;
$name_is_required = 1;
$uself = 0;
$use_envsender = 0;
$use_webmaster_email_for_from = 0;
$use_utf8 = 1;
 
// -------------------- END OF CONFIGURABLE SECTION ---------------
 
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; 
 
charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
 
$envsender = "-f$mailto" ;
$name = $_POST['name'] ;
$number = $_POST['number'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;
$time = $_POST['time'] ;
$http_referrer = getenv( "HTTP_REFERER" );
foreach($_POST['check']  as  $value)  
{
$check_msg .= "$value\n";
}
 
$error ="";
 
if($name=="")$error.="Please enter a neme"."<br>";  
if($number=="")$error.="Please enter a number"."<br>";  
if($email=="")$error.="Please enter email address"."<br>"; 
if($comments=="")$error.="Please describe your interest"."<br>"; 
if($time=="")$error.="What country,city and state you are in?"."<br>"; 
 
if{$error==""){
else{
    echo"<div align='center'>".$error."</div>";
}
 
 
 
$messageproper =
"\n" .
"e-mail sent from: $http_referrer\n" .
"\n" .
"\n" .
"\n" .              
    
    
    "Contact Name:  $name\n" .
    "Contact Number: $number\n"  .
    "Contact e-Mail: $email\n" .
    "Requested Callback Time:  $time\n" .
    "\n" .
    
    "$check_msg\n" .
    "\n" .
    "\n" .
    "Sender Comments:\n\n" . $comments .
    
 
    
    "\n\n .............( sender comments ended on this line )....................\n" ;
 
 
$headers =
    "From: \"$name\" <$fromemail>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.13.0" .
    $headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
 
if ($use_envsender) {
    mail($mailto, $subject, $messageproper, $headers, $envsender );
    echo "Your request was send";
}
 
else {
    mail($mailto, $subject, $messageproper, $headers );
    echo "Messega failed to send, try again";
}
 
header( "Location: $thankyouurl" );
exit ;
 
?>
 


Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: PHP FORM | w/immediate field validation

Post by Burrito »

please use PHP tags when posting code in the forum.

I have added them for you this time.

to answer your question, if you want to do it all client side, you're going to need to use javascript. Google for javascript validators and I'm sure you'll find a million results that you can check out for code samples.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP FORM | w/immediate field validation

Post by Luke »

dude have you even looked at your own code? Even your question contained an obvious syntax error. Don't you see anything wrong with this?

Code: Select all

if{$error=="")
The PHP error told you exactly where the problem was... look at it... :roll: :?
passam
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 6:55 pm

Re: PHP FORM | w/immediate field validation

Post by passam »

I dont know PHP. So no, I do not see if I did I would not be asking I guess.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP FORM | w/immediate field validation

Post by Luke »

Dude, COME ON! LOOK AT IT. Is there anything about this that looks inconsistent. Maybe you could try to solve this problem yourself... just give it a shot. Give it your VERY best guess. what is wrong with this picture?

Code: Select all

if{$error=="")
Just TRY SOMETHING. :dubious:
passam
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 6:55 pm

Re: PHP FORM | w/immediate field validation

Post by passam »

Listen thank you for pointing out parentheses. Of cause when it is corrected I get the same error anyway. So if you dont mind, I would like to stop worry about obvious. Parentheses got errored when I adjusted the code for the post. But of cause you have a good eye for the problem I guess you new when parentheses or one of them is corrected the code doesn't work anyway.

I wouldn't mind to read your solution. That I would really thank for. So do you know how to make the code work?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: PHP FORM | w/immediate field validation

Post by Burrito »

Code: Select all

 
if($error=="")
{
  // do something here
}
else
{
  // $error isn't an empty string so do something else here
}
 
this though won't achieve what you're after. As I said in my first post, if you want 'immediate' feedback, you're going to need a javascript solution. My recommendation would be to use both (a server-side check as well as a javascript (client-side) check).

did you do as I suggested and google for javascript form validation?
passam
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 6:55 pm

Re: PHP FORM | w/immediate field validation

Post by passam »

Burrito wrote:

Code: Select all

 
if($error=="")
{
  // do something here
}
else
{
  // $error isn't an empty string so do something else here
}
 
this though won't achieve what you're after. As I said in my first post, if you want 'immediate' feedback, you're going to need a javascript solution. My recommendation would be to use both (a server-side check as well as a javascript (client-side) check).

did you do as I suggested and google for javascript form validation?
............................................................................................................................................................

Yes I know, I guess I was looking for other ideas, Thank you! I got this from other forum.

Code: Select all

 
 
change
Code: [Select]
if($error==""){
else{
   echo"<div align='center'>".$error."</div>";
}
 
to
Code: [Select]
if(!empty($error)) // NOTE THE ( at the start not {
{
   echo"<div align='center'>".$error."</div>";
   exit; // don't continue
}
 
 

I will use javascript like you sad. Thank you again.
Post Reply