PDA

View Full Version : Can someone help me with this PHP form?



lilymc
September 11th, 2007, 05:59 AM
For a website I'm working on I need to create a quote request form. So I got a script for a basic PHP/feedback form and I'm just trying to add some additional fields.

I know it's not that difficult, and I could probably figure it out myself with enough time :blush: .... but if someone could help with this, it would be a lot quicker, and I'd really appreciate it.

Here's the code the way it is now:


<?
// ------------- CONFIGURABLE SECTION ------------------------

// $mailto - set to the email address you want the form
// sent to, eg
//$mailto = "youremailaddress@example.com" ;

$mailto = 'me@myemail.com' ;

// $subject - set to the Subject line of the email, eg
//$subject = "Feedback Form" ;

$subject = "Quote Form Reply" ;

// the pages to be displayed, eg
//$formurl = "http://www.example.com/feedback.html" ;
//$errorurl = "http://www.example.com/error.html" ;
//$thankyouurl = "http://www.example.com/thankyou.html" ;

$formurl = "http://www.starryskymedia.com/bnp/index.html" ;
$errorurl = "http://www.starryskymedia.com/bnp/error.html" ;
$thankyouurl = "http://www.starryskymedia.com/bnp/thankyou.html" ;

$uself = 0;

// -------------------- END OF CONFIGURABLE SECTION ---------------

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;
$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}
if (empty($name) || empty($email) || empty($comments)) {
header( "Location: $errorurl" );
exit ;
}
if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) ) {
header( "Location: $errorurl" );
exit ;
}

if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}

$messageproper =

"This message was sent from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Name of sender: $name\n" .
"Email of sender: $email\n" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper,
"From: \"$name\" <$email>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.07" );
header( "Location: $thankyouurl" );
exit ;

?>
The additional fields I want to add are:
Number of Pages?
Number of Images?
Will You Need BNP to Write Original Content?It might be better to have a drop-down box with numbers, but if that's too much trouble, then never mind.

I don't want to mess it all up, so if you could just show me where/what to add exactly, that would be great.


thank you!!

icio
September 11th, 2007, 08:47 AM
The first thing that you need to do is build your form. For that you'll probably need a text-input field for the number of pages, a text-input field for the number of images and a checkbox for "Will you need BNP to write original content?"


<form action="..." method="post">
...
# Pages: <input type="text" name="pages" value="1" /><br />
# Images: <input type="text" name="images" value="1" /><br />
<input type="checkbox" name="written" /> I need BNP to write original content
</form>

You could also easily put a drop-down into the HTML form like so:

<select name="images">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>

One the user has submitted that, all that you need to do is validate in the PHP form:

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

// The new inputs
$pages = intval($_POST['pages']);
$images = intval($_POST['images']);
$written = isset($_POST['written']) ? 'Yes' : 'No';

And then to build the email with this new information you could format it like

$messageproper =

"This message was sent from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Name of sender: $name\n" .
"Email of sender: $email\n" .
"Number of pages $pages" .
"Number of images: $images" .
"Require original content to be written: $written" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;

And that should be about it.

I'd like to point out that the sample script doesn't check to see if the post variables are set, which is pretty bad. I'd recommend inserting something like the following:


// Do some checking
if (!isset($_POST['name'], $_POST['email'], $_POST['comments'], $_POST['pages'], $_POST['images'])) {
header("Location: $errorurl");
exit;
}

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

Hope that helps :thumb:
PS, I've not tested it :/

lilymc
September 11th, 2007, 06:34 PM
Wow, excellent... thank you so much! I'll try it out. Btw, yesterday I had started to build a simple form, but just with the name, email and message fields. But I'll work on this and hopefully I can do it without messing it up! :D

Thanks again, I appreciate you taking the time to help with that!

lilymc
September 11th, 2007, 09:37 PM
icio - thanks again, I tested it and the form is working... although i'm not quite finished with it.

But for some reason I am having a problem with the HTML, which is odd because it seems so simple and I didn't have this problem the last couple times I made a contact form.


edit: never mind, I think i solved the html/css problem.

I might have another question later though. :p

I need to make a pop-up window that says "thank you, blah blah blah"

I normally make another HTML page, but he wants a small pop-up window. I haven't worked on that yet, but if I have any problems with it, i'll be back.

lilymc
September 12th, 2007, 01:40 AM
If anyone can help me... I made it so a pop-up window comes up that lets them know they successfully sent the info... but right now when you press 'send', the form doesn't clear - the text stays there and the pop-up window comes up, but it doesn't look like it went through. (it does work though, I tested it)

How do I get the text to clear when they press send? I don't think I want a reset button, I just want the text to go away when they press send.

any help with this would be great right now. Thank you!

simplistik
September 12th, 2007, 09:25 AM
on success just have the form go back to the same page...


header("Location: http://www.link.com");

lilymc
September 12th, 2007, 05:49 PM
on success just have the form go back to the same page...


header("Location: http://www.link.com");


Thanks! I'll try that. But (forgive the n00b question) where exactly does that code go? :blush:

And I hope reloading the page doesn't interfere with the little pop-up window that comes up. But I did want it to just go back to the same page, (reloaded) so if that works that would be perfect.

lilymc
September 14th, 2007, 01:40 AM
bump

thanks to whoever can help with that code.