/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP FORM | w/immediate field validation

[B]Hi [/B]

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 [COLOR=”Red”]red colored error description[/COLOR] 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 [B] if{$error==””) [/B] 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.

[COLOR=”Blue”] __***____( html form start here) ____________[/COLOR]

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

[COLOR=”Blue”]____+++__________html form end here_______[/COLOR]

[COLOR=”Blue”]—–+++———–feedback.php start here———–[/COLOR]
<?php

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

$mailto = ‘[email protected]‘ ;
$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)) ? “rn” : “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 .= “$valuen”;
}

$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_referrern” .
“n” .
“n” .
“n” .

“Contact Name: $namen” .
“Contact Number: $numbern” .
“Contact e-Mail: $emailn” .
“Requested Callback Time: $timen” .
“n” .

“$check_msgn” .
“n” .
“n” .
“Sender Comments:nn” . $comments .

“nn ………….( 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 ;

?>

? [COLOR=”Blue”] _______+++_______feedback.php ends here ______________[/COLOR]

[B][SIZE=”4″]Thank you[/SIZE][/B]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pruneytoesJan 20.2009 — Typo. Use a parentheses instead of a curly bracket on line 42, like this:
[code=php]//BAD
if{$error=="")
//GOOD
if($error=="")[/code]
Copy linkTweet thisAlerts:
@passamauthorJan 20.2009 — Still doesnt work. Parentheses got an error when I edited the code for the post.

With parentheses " corrected " I get the same problem on line 42. I am thinking about using JavaScript to validate on client side. Can anyone suggest a good script?
Copy linkTweet thisAlerts:
@pruneytoesJan 20.2009 — looking closer, it looks like you are also missing a closing bracket on the next line. Try this:
[code=php]if($error=="") {
//do nothing
} else {
echo"<div align='center'>".$error."</div>";
}[/code]
Copy linkTweet thisAlerts:
@passamauthorJan 20.2009 — [B]pruneytoes[/B] Thank you, still I get an error now on the line 91 which is the very last one after the ?> on the page. I will look for the javascript I think it will have batter appearance I need my form to look professional this is why I didnt want to redirect to error page. I was given this at the other forum:

[code=php]
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
}

[/code]



It works but msg does not come up next to the field in the .htm form.
×

Success!

Help @passam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...