Jump to content

[SOLVED] PHP FORM | w/immediate field validation | need help!


Recommended Posts

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.

 

 

 

 

__***____( html form start here) ____________

 

<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>

 

____+++__________html form end here_______

 

 

_________+++_____feedback.php start here___

<?php

 

 

// ------------- CONFIGURABLE SECTION ------------------------

 

$mailto = 'email@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 ;

 

?>

 

 

_______+++_______feedback.php ends here ______________

 

 

Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

change

if{$error==""){
else{
   echo"<div align='center'>".$error."</div>";
}

 

to

if(!empty($error)) // NOTE THE ( at the start not {
{
   echo"<div align='center'>".$error."</div>";
   exit; // don't continue
}

Well you could echo them

ie

<label>Contact`s Name:</label><input name="name" type="text" value="" style="width: 35%;" > <?php if(empty($_POST['name'])) echo "Please enter a neme";  ?><br>

 

But client side validation works well

Client side validation is good as an extra, but you should have server-side (php) validation as the default. People can turn off javascript (and some of us have it off by default), so if you don't validate server side as well as client side, then sometimes input won't be validated.

Thank you, It worked I guess if I want to have msg come up next to the field I need to find a javascript since I want to validate on client side.  There is no way to post errors next to the field using .php?

 

If you're wanting something instant, than use javascript. Otherwise, you can do it with PHP, with something like:

<?php
if(isset($_POST)) {
   // form validation
   // set any errors here.
   if(!isset($error)) {
      // submit form (or email it)
   }
}
if(!isset($_POST) || isset($error)) {
   // if form hasn't been submitted, or there was an error
   if(isset($error)) 
      echo $error.'<br />';
   ?>
   FORM HERE
   <?php
}
?>

 

Like haku said, if you did the javascript approach, make sure to do server side validation. Client side may be convenient, but not secure.

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.