0

My php form`s action goes to another page and it doesnt look good, I want to show form and whenever user fills out the form send it from same page is some inputs are empty I want to give an error.

Here is my from;

       <form method="post" action="#" name="emailform">
                <div class="rel">
            <div class="confor nab"><input class="cname" type="text" name="name"  value=""/></div>
            <div class="confor emb"><input class="cmail" type="text" name="email"  value=""/></div>
            <div class="confor phb"><input class="cphone" type="text" name="phone"  value=""/></div>
            <div class="confor evb"><input class="ceven" type="text" name="event"  value=""/></div>
            <div class="confor wdb"><input class="cwed" type="text" name="wdate"  value=""/></div>
            <div class="confor whb"><input class="chow" type="text" name="where"  value=""/></div>
            <div class="confort nob"><textarea class="ctho" name="notes" cols="" rows=""></textarea></div>
            <input name="submit" class="contactsend" type="submit" value="SEND" /></div>
         </form> 
8
  • What you are looking for is javascript / ajax.
    – jeroen
    Jan 29, 2011 at 17:00
  • he does not need ajax/javascript for this, but it would make the user experience a lot nicer!
    – Alfred
    Jan 29, 2011 at 17:06
  • 1
    @Alfred You could be right, but that´s just my interpretation of the question; I thought of another page in the sense of a page reload / refresh.
    – jeroen
    Jan 29, 2011 at 17:11
  • I need only for "name, email, phone, where" inputs thats it if all of them are filled out than email will be sent otherwise It will give window.alert error. Thats it, I need very simple thing. Jan 29, 2011 at 17:22
  • 1
    In that case Extelliqent, you'd want to perform some routine validation when they click the submit button, or while they are filling out the form, with JavaScript. If it passes validation, then submit the form, otherwise don't submit. You'll still want to perform server side validation of the data with PHP, because JS can be turned off or circumvented by the end user, for instance with an XSS attack, which might not be that dangerous with an email form, unless it's saved to a DB as well as sent in a mail.
    – Phoenix
    Jan 29, 2011 at 17:51

4 Answers 4

3

Simply change action to:

<?=$_SERVER['PHP_SELF'];?>

As this will send the form to the same page it's on and you can do your PHP validation and email sending code at the top, like so:

<?
// Here's your PHP validation and email sending code
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <link type="text/css" rel="stylesheet" href="/assets/css/styles.css" />
        <title></title>
    </head>
    <body>
        <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
            <!-- Here's all your form items -->
        </form>
    </body>
</html>
6
  • if you don't filter this $_SERVER['PHP_SELF'] you can get into trouble! I thought that just using action="." is enough.
    – Alfred
    Jan 29, 2011 at 17:05
  • 1
    @Ahmed Nuaman You might want to add that your example code requires short tags to be enabled.
    – jeroen
    Jan 29, 2011 at 17:15
  • guys I give a error if the input is empty so it shouldnt work before form submit but this way whenever i go that page it says please fill out the name area. Jan 29, 2011 at 17:16
  • I need only for "name, email, phone, where" inputs thats it if all of them are filled out than email will be sent. Jan 29, 2011 at 17:18
  • You have to check if the form is submitted. Something like if($_POST['submit']) should work, and then handle your form if it's set.
    – Andrew
    Jan 29, 2011 at 18:44
1

As @joren pointed out, javascript / ajax is a good tool for this. See this jQuery example for a working example and other instructions.

Of course it requires using jQuery, but unless this is the only web form you'll ever write, it's probably worth the investment to learn it (or another full-featured javascript library :)

1

Here's how I would do it without JavaScript validation, kinda stripped down because I'm not gonna write the whole thing for you:

<?php
//various form validation functions for checking if an input is numeric, or a valid email, or whatever

function Post_Error(&$error,$name)
{
     if(is_array($error) && array_key_exists($name,$error))
     {
         echo '<span class="error">' . $error[$name] . '</span>';
     }
}
/* or you could do this
function Post_Error($name)
{
    global $error;
    if(is_array($error) && array_key_exists($name,$error))
    {
        //yadda yadda
    }
}
and then not pass the $error array each time*/

function Post_Fill($field)
{
     if(is_array($_POST) && array_key_exists($field,$_POST))
     {
          echo $_POST[$field];
     }
}

$error = array();  //Initialize error array outside of if statements, important because we are gonna be passing it below if there are errors or not, and if it is created inside one of the ifs then it might not be there when needed.
if($_POST['submit'])
{
     if(!Valid_Name($_POST['cname'])) //Valid_Name() is one of the fictional validation functions from above, could also do it manually
     {
          $error['name'] = 'Name not valid.  Field Required.  Blah blah blah.';
     }
     if(!Valid_Email($_POST['cmail']))  //Could also have a single validate() function that validates everything sent through $_POST, and returns an array of errors
     {
          $error['email'] = 'Email not valid.  Field Required.  Must be longer than 5 characters. Whatever.';
     }
     //etc. etc. for all the fields
}

if($_POST['submit'] && count($error) == 0)
{
     //Send Mail
     echo '<h1>Mail Sent!</h1>';  //Or whatever message to user that mail was sent
}
else
{
?>
       <form method="post" action="#" name="emailform">
       <div class="rel">
            <div class="confor nab"><input class="cname" type="text" name="name"  value="<?php Post_Fill('name');?>"/><?php Post_Error($error,'name');?></div>
            <div class="confor emb"><input class="cmail" type="text" name="email"  value="<?php Post_Fill('email');?>"/><?php Post_Error($error,'email');?></div>
            <div class="confor phb"><input class="cphone" type="text" name="phone"  value="<?php Post_Fill('phone');?>"/><?php Post_Error($error,'phone');?></div>
            <div class="confor evb"><input class="ceven" type="text" name="event"  value="<?php Post_Fill('event');?>"/><?php Post_Error($error,'event');?></div>
            <div class="confor wdb"><input class="cwed" type="text" name="wdate"  value="<?php Post_Fill('wdate');?>"/><?php Post_Error($error,'wdate');?></div>
            <div class="confor whb"><input class="chow" type="text" name="where"  value="<?php Post_Fill('where');?>"/><?php Post_Error($error,'where');?></div>
            <div class="confort nob"><textarea class="ctho" name="notes" cols="" rows=""><?php Post_Fill('notes');?></textarea><?php Post_Error($error,'notes');?></div>
            <input name="submit" class="contactsend" type="submit" value="SEND" /></div>
         </form> 
<?php
} //Close else
?>

Oh, also, this is kinda a drop-in type thing, you'd include it in the page where the form would go, so you already have all the <head> stuff and the rest of the layout of the page external to the form PHP.

What this code would do is, the form is submitted to the same page, which performs validation, and either removes the form and gives a message that the mail was sent to the user if the input had no errors, or display errors (in a span class="error") out beside the inputs for those inputs that were not properly validated while preserving their previous input for all fields.

1
  • Nah, I already have stock form code that I wrote that I can quickly modify for people.
    – Phoenix
    Jan 29, 2011 at 19:09
0

To avoid leaving the current page and just change the section of the page where the form is located, you can use javascript / ajax to send in the form.

A javascript library like jQuery makes ajax easier, so I would recommend checking out the .post() section of the jQuery documentation.

The drawback is that the visitor needs to have javascript enabled, but if you already have a working form, it will degrade to the new page load you have now so that´s no problem.

1
  • hi, I need very simple thing right now. just dont want it to go another page and if input empty give me window.alert thats it. I want to learn in the future but I dont have a time now.. if anybody can help me with the code that would be awesome Jan 29, 2011 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.