Bluemoon Posted October 21, 2009 Share Posted October 21, 2009 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/ Share on other sites More sharing options...
Bluemoon Posted October 22, 2009 Author Share Posted October 22, 2009 Bump Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941659 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941667 Share on other sites More sharing options...
Bluemoon Posted October 22, 2009 Author Share Posted October 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941671 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941676 Share on other sites More sharing options...
Bluemoon Posted October 22, 2009 Author Share Posted October 22, 2009 Okay, that's what I figured but wasn't sure. Thanks! I'll consider this solved. Now I just have to find a PHP validation code and figure out how to implement the javascript in it. Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941682 Share on other sites More sharing options...
mikesta707 Posted October 22, 2009 Share Posted October 22, 2009 you can mark the topic as solved by clicking the topic solved button at the bottom of the thread Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941684 Share on other sites More sharing options...
Bluemoon Posted October 22, 2009 Author Share Posted October 22, 2009 Thanks to you, I knew how to mark it 'Solved'. That was a new one for me. Quote Link to comment https://forums.phpfreaks.com/topic/178523-solved-can-i-add-a-required-field-form-validation-script-to-my-current-form-script/#findComment-941686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.