PDA

View Full Version : Tweaking php form (adding fields in script)


Dolphin123
12-09-2005, 12:42 AM
o.k added a form to my website using http://www.thesitewizard.com/wizards/
which is absolutely great!!!

However...

Now I want to add some fields and I am getting just a bit stuck:

Here is the script:

<?
/*
CHFEEDBACK.PHP Feedback Form PHP Script Ver 2.04
Generated by thesitewizard.com's Feedback Form Wizard.
Copyright 2000-2005 by Christopher Heng. All rights reserved.
thesitewizard and thefreecountry are trademarks of Christopher Heng.

$Id: phpscript.txt,v 1.4 2005/04/12 10:55:01 chris Exp $

Get the latest version, free, from:
http://www.thesitewizard.com/wizards/feedbackform.shtml

You can contact me at:
http://www.thesitewizard.com/feedback.php

LICENCE TERMS

1. You may use this script on your website, with or
without modifications, free of charge.

2. You may NOT distribute or republish this script,
whether modified or not. The script can only be
distributed by the author, Christopher Heng.

3. THE SCRIPT AND ITS DOCUMENTATION ARE PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
IMPLIED WARRANTY OF MECHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE. YOU AGREE TO BEAR ALL RISKS AND
LIABILITIES ARISING FROM THE USE OF THE SCRIPT,
ITS DOCUMENTATION AND THE INFORMATION PROVIDED BY THE
SCRIPTS AND THE DOCUMENTATION.

If you cannot agree to any of the above conditions, you
may not use the script.

Although it is NOT required, I would be most grateful
if you could also link to thesitewizard.com at:

http://www.thesitewizard.com/

*/

// ------------- CONFIGURABLE SECTION ------------------------

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

$mailto = 'test@test.com' ;

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

$subject = "Feedback Form" ;

// 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.test.com/contact.php" ;
$errorurl = "http://www.test.com/error.html" ;
$thankyouurl = "http://www.test.com/verzonden.html" ;

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

$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 ;
}
$name = strtok( $name, "\r\n" );
$email = strtok( $email, "\r\n" );
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}

$messageproper =

"This message was sent from:\n" .
"$http_referrer\n" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;

mail($mailto, $subject, $messageproper, "From: \"$name\" <$email>\r\nReply-To: \"$name\" <$email>\r\nX-Mailer: chfeedback.php 2.04" );
header( "Location: $thankyouurl" );
exit ;

?>

1. Now I want to add more fields to this form, and have tried adding:

$fname = $_POST['fname'] ;
$fname = strtok( $fname, "\r\n" );

to the correct places

and

$fname to comments

Which works.

2.

Adding a checkbox to the feedback page, I get "Array" in the comments mail.

<input type="checkbox" value="feedbackbox" name="feedbackbox[]">feedbackbox<br />

The questions

a. Can I just add fields as shown in 1? (field fill in does not have to be necessary)
b. How do I add checkboxes?
c. How do I add a line space in the comments field. Use /n ?

All parts of the form work great so many thanx to sitewizards.

Any apologies if the solution seems blatainly obvious, but do not have too much experience
with this, and looking at faqs about this on the net and here. :confused: :o

Velox Letum
12-09-2005, 01:20 AM
Snippit:



$name = $_POST['name'] ;
$job = $_POST['job'] ; // Example extra field. HTML field would be named job.
$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 ;
}
$name = strtok( $name, "\r\n" );
$email = strtok( $email, "\r\n" );
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}

// Changing format of $messageproper to make it easier to manipulate.
$messageproper .= 'This message was sent from:' . "\n";
$messageproper .= $http_referrer . "\n";
$messageproper .= 'Job: ' . $job . "\n"; // Added a line here
$messageproper .= '------------------------- COMMENTS -------------------------' . "\n\n";
$messageproper .= $comments . "\n\n";
$messageproper .= '-----------------------------------------------------------' . "\n";

mail($mailto, $subject, $messageproper, "From: \"$name\" <$email>\r\nReply-To: \"$name\" <$email>\r\nX-Mailer: chfeedback.php 2.04" );
header( "Location: $thankyouurl" );
exit ;

?>

As for the checkboxes...though you could always just give them separate names rather than an array so you could use the simple format as above.

Snippit (example):

foreach ($_POST['feedbackbox'] as $key => $value) {
echo $key . ': ' . $value;
}


Now its your job to integrate this into your script. :)

If you decided to just name the checkboxes all something different, you could just use $_POST['checkboxname'] for the value.

Dolphin123
12-09-2005, 04:11 AM
That did it thanx :) :) :thumbsup:

mike77pl
12-14-2009, 12:52 PM
It worked fine with me too. Thanks Velox.
What about adding select list to the form - do I also need to use foreach or there is an other way?

EDIT: Could anyone help me with editing the code above? I need to add select list to it.
Thanks