View Full Version : How to send a form with PHP?
Blonde Jon
11-25-2005, 06:12 PM
I don't know PHP very well I was trying to make a form that people can fill out and send to my email for link exchanges. I'm having some issues can someone help? This is the code that I'm using, but it won't work since it only allows you to have 5 "variables" I think that is the word. But here's my code:
<?
$email = $_REQUEST['email'] ;
$url = $_REQUEST['url'] ;
$title = $_REQUEST['title'] ;
$description = $_REQUEST['description'] ;
$linkback = $_REQUEST['linkback'] ;
mail( "jonl@littlemanpedalcars.com", "From www.littlemanpedalcars.com",
$url, $title, $description, $linkback, "From: $email" );
header( "Location: http://www.littlemanpedalcars.com/thankyou.html" );
?>
sufyaaan
11-26-2005, 09:02 AM
[QUOTE=Blonde Jon]I don't know PHP very well I was trying to make a form that people can fill out and send to my email for link exchanges. I'm having some issues can someone help? This is the code that I'm using, but it won't work since it only allows you to have 5 "variables" I think that is the word. But here's my code:
<?
$email = $_REQUEST['email'] ;
$url = $_REQUEST['url'] ;
$title = $_REQUEST['title'] ;
$description = $_REQUEST['description'] ;
$linkback = $_REQUEST['linkback'] ;
mail( "jonl@littlemanpedalcars.com", "From www.littlemanpedalcars.com",
$url, $title, $description, $linkback, "From: $email" );
header( "Location: http://www.littlemanpedalcars.com/thankyou.html" );
?>[/QUOTE]
The syntax of mail() function is as follows:
mail(to,subject,message, additional headers)
Now re-check your script to correct it according to the correct syntax
dddougal
11-28-2005, 01:05 PM
Try changing the $REQUEST to the method used as well, $POST or $GET...
mgscox
12-18-2005, 02:55 AM
Try this - I haven't tested it so there *might* be the odd bug, but you should get the idea.
<?
if (!isset($_POST))
$_POST=&$HTTP_POST_VARS; // backwards compatible with old PHP
// submit button pressed?
if (isset($_POST['linkrequested'])) { // if the variable is defined, the button was pressed
// check that each form field was completed. If it isn't set then append to error string
$errStr="";
$errStr.=(isset($_POST['email']))?"":"You must provide your Email address\n";
$errStr.=(isset($_POST['url']))?"":"You must provide the URL of your link\n";
$errStr.=(isset($_POST['title']))?"":"You must provide a title for your link\n";
$errStr.=(isset($_POST['description']))?"":"You must provide a description of your link contents\n";
$errStr.=(isset($_POST['linkback']))?"":"You must provide a link-back address\n";
// were any errors found?
if ($errStr!="")
// yes, so print them
print nl2br($errStr);
else {
// no errors found
$to="you@yourdomain.com";
$subject="New link request";
// construct message from form fields.
// Remember to use full carriage-return and line-feed (\r\n) for new lines in Emails
$message=
"New link request received\r\n".
"Email: {$_POST['email']}\r\n".
"URL: {$_POST['url']}\r\n".
"Title: {$_POST['title']}\r\n".
"Description: {$_POST['description']}\r\n";
// really only need From in the header, but what the heck!
$headers=
"From: {$_POST['email']}\r\n".
"Reply-To: {$_POST['email']}\r\n" .
"X-Mailer: PHP/".phpversion();
// mail it out
mail($to,$subject,$message,$headers);
print "Your link request has been logged. I'll get back to your shortly.<br>".nl2br($message)."<br>";
}
}
// Dump out the form - the action ? just posts back to this same page
print "
<form method=post action='?'>
Email: <input name=email size=40><br>
URL: <input name=url size=40><br>
Title: <input name=title size=40><br>
Description: <input name=description size=40><br>
Linkback: <input name=linkback size=40><br>
<input type=submit name=linkrequested value='Requst a link exchange'>
";
?>
Rather than keep writing $_POST['index'] you could make shorthand versions like this;
// loop through the $_POST array and extract each index and its
// corresponding value
foreach ($_POST as $key=>$value) {
$key="p_".$key; // add a prefex of "p_" to the extracted array index
$$key=$value; // double $ means "use the contents of the variable $key"
}
// This will take every variable that was posted and declare it as a PHP
// variable with a "p_" prefix (the prefix prevents spoofing of other
// variables you may have declared by malicious users).
print "URL: $p_url, Title: $p_title"; // etc.
Hope this helps.
vBulletin v3.0.3, Copyright ©2000-2013, Jelsoft Enterprises Ltd.