Jump to content

[SOLVED] Can I add a required field form validation script to my current form script ...


Bluemoon

Recommended Posts

I am currently using a GREAT, easy-to-use PHP form script.  The code is below.

 

I'd like to add a required field form validation script to it.  I'd like for error messages, if any, to appear in a new window. 

 

I want to avoid javascript in case people have that turned off.  That is the whole reason I'm looking for a PHP script to accomplish this task.

 

Is this possible?  If so, what code would I use?

 

Below is my current script without any form validation:

 

// Configuration Settings

$SendFrom =    "Form Feedback <email@address>";

$SendTo =      "name@emailaddress";

$SubjectLine = "Subject";

$ThanksURL =  "thanks.htm";  //confirmation page

$Divider =    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

 

// Build Message Body from Web Form Input

$MsgBody = @gethostbyaddr($_SERVER["REMOTE_ADDR"]) . "\n$Divider\n";

foreach ($_POST as $Field=>$Value)

  $MsgBody .= "$Field: $Value\n";

$MsgBody .= "$Divider\n" . $_SERVER["HTTP_USER_AGENT"] . "\n";

$MsgBody = htmlspecialchars($MsgBody);  //make content safe

 

// Send E-Mail and Direct Browser to Confirmation Page

mail($SendTo, $SubjectLine, $MsgBody, "From: $SendFrom");

header("Location: $ThanksURL");

?>

Link to comment
Share on other sites

if you want pop ups, only javascript can do that (its a client side thing.) You can have a required field, but in order for you to have a "dynamic" one (IE one that won't let you submit the form without filling out the correct text fields) you need javascript for that also . Remember, PHP is parsed on the server, so after the page is loaded, PHP can

t do anything. In PHP you can do something like

if (!isset($_POST['required']) || empty($_POST['required']){
echo "You didnt fill out a required filed";
}

 

 

If I were you, I would implement the javascript validation, but remember to implement a PHP validation just incase someone doesn't have javascript enabled. Most people don't disable javascript, so you won't need to worry about it for the most part, but you always want the PHP validation to fall back on

Link to comment
Share on other sites

Thanks for the reply, mikesta.

 

This might sound like a stupid question, but is it possible to open the error message in a new blank page without it being a javascript popup?

 

The reason that I'm concerned about javascript is because I am currently using a javascript for form validation and getting blank responses.  So, someone or people are bypassing the javascript validation. 

 

I want to avoid that and use PHP so they can't turn that off.

Link to comment
Share on other sites

like I said, you should us javascript and PHP validation. Javascript validation is more or less bells and whistles as (as you have no doubt noticed) it can be turned off, whereas PHP cannot. Its nice to have the javascript validation, but its not fool proof.

 

As for opening a new blank page, remember, doing things with the browser (like popups, opening new windows) can only be done by the client. PHP is executed on the server, and because of that it can't do much with the browser besides detect that kind of browser it is. Any kind of popup that happens is generally javascript

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.