Is it possible to send email with single page
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>
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>
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 :)
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.
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.
精彩评论