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!
i have a simple php feedback form with the following fields
name :: email :: message
my form is very simple that if anyone click on submit it generates the message that "your message has been sent successfully".
i want to make my fields required.
anyone can submit my form without writing anythings in field like no name in name field no email address in mail address or no message in message box.
but i want if anyone submit my form in blank format than it should generates an error that you must fill the required field.
here is my form code.
at the very top
Work out some logic for detecting whether form values are set and make $_SESSION['name']=$_POST['name'] etc. You could have an error variable that is set for time there is an error.
Then if no errors process it, if errors output the form again...
then you can modify the form fields so
. The first time round the session variables aren't set...
You must check user values are what you want before sending emails (or any other o/p to browser, database etc). A naughty user could use your form to send spam to the world
session_start();
if(isset($_POST['submit']){
foreach ($_POST as $key->$value) //iterate through form values
{
if (isset($value))
{
$form[$key]=$value; //fill form array if $_POST value set
}
else{
$_SESSION[$key]=$value; //if $_POST not set populate session array
}
if(!isset($_SESSION)) {
//form processing
exit();
}
if(isset($_SESSION) echo "<p>Your form had some fields missing...</p>"
//form with value fields set to output S_SESSIOn['name'] etc
Would be how i'd do it - untested!!!!
You can do all sorts of clever things like changing the colour of the form fields that are empty etc...
Thanks andym01480.
i have got a little better an easy idea for this.
please tell me is this correct method.
i am using Dreamweaver cs3 and i have used the validate form option from tag inspector pane.
from here i check the all fields required option.
and now all fields must be filled/required
and now form is working very exactly.
is it better??
No idea - don't use dreamweaver! There are a lot of ways of skinning a cat (not that I have done that )It might be using javascript validation. Which is okay for client side.
Whatever way you use, you must properly check input in your php script before sending an email using user input. I din't give you any of that!