PDA

View Full Version : PHP Form



hoferchr
June 14th, 2005, 07:15 PM
I used the tutorial located at http://www.kirupa.com/web/php_contact_form.htm to create an e-mail form for our church website. If I wanted to create multiple questions each with its own set of radio buttons, how would I code that in PHP? I understand how to code it with one set of radio buttons (based on the tutorial), but I don't know how to code more than one set of radio buttons. Any help would be great! Thanks!

blindlizard
June 14th, 2005, 07:33 PM
Just give the next set of radio buttons a different name.

HTML:
<input type="radio" name="radio" value="1">
<input type="radio" name="radio" value="2">
<input type="radio" name="radio2" value="a">
<input type="radio" name="radio2" value="b">

PHP:
$option = $_POST['radio'];
$option2 = $_POST['radio2'];

hoferchr
June 14th, 2005, 09:19 PM
Great! Thanks! That worked really well. I have another question now.

Right now when a form is submitted to the Webmaster (that's me) at our church, I get an e-mail address from "Apache". If I try and reply to that e-mail, the "Apache" e-mail address appears in the "To:" field. Is there some PHP code I can add that would put the sender's e-mail address (a required field in the form) in the "To:" box when I push the Reply button?

Chris


Just give the next set of radio buttons a different name.

HTML:
<input type="radio" name="radio" value="1">
<input type="radio" name="radio" value="2">
<input type="radio" name="radio2" value="a">
<input type="radio" name="radio2" value="b">

PHP:
$option = $_POST['radio'];
$option2 = $_POST['radio2'];

blindlizard
June 14th, 2005, 09:48 PM
You can add headers to the PHP mail function. Here you can add the senders email address to the From field.

<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

hoferchr
June 15th, 2005, 12:05 AM
o.k. I think this is sort of along the lines that I am looking for. What if I want the "Reply to" e-mail address to be dynamic. The way you've explained it, every time a user sends an e-mail through the mail form on the web, I could reply to it using Outlook, but the Reply To field would always have webmaster@example.com. What if I wanted it to be the e-mail address of the person who entered it in the form initally?

Chris


You can add headers to the PHP mail function. Here you can add the senders email address to the From field.

<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

blindlizard
June 15th, 2005, 04:29 AM
Ok, well I assume your form is like the one in the tutorial. With that said, you have a textbox in the HTML form called email like
<input type="text" name="email" size="19"> so your php is like this
$email_field = $_POST['email'];
now, change the header like this
$headers = 'From: ' . $email_field . "\r\n" .
'Reply-To: ' . $email_field . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

hoferchr
June 16th, 2005, 12:54 AM
Thanks again! I'm really learning a lot here! Another question I have... If I have a question on a form with a set of radio buttons to choose from. Is there a way to make the radio buttons a required field (like a required text field) before people push the "Send" button to send the e-mail through the web form?

Chris


Ok, well I assume your form is like the one in the tutorial. With that said, you have a textbox in the HTML form called email like
<input type="text" name="email" size="19"> so your php is like this
$email_field = $_POST['email'];
now, change the header like this
$headers = 'From: ' . $email_field . "\r\n" .
'Reply-To: ' . $email_field . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

blindlizard
June 16th, 2005, 01:17 AM
You will want to do that with JavaScript on the client side. Add this between the <head> </head> tags and change myradiobuttonname with the name of your radio buttons.


Validate(thisform) {
myOption = -1;
for (i=0; i<thisform.myradiobuttonname.length; i++) {
if (thisform.myradiobuttonname[i].checked) {
thisform.submit();
}
}
alert('You must select a radio button.');
return false;
}

Then on your submit button, do this and change myform with the name of your form

<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Submit" />

This will make sure one of the radio buttons is selected before it will allow the form to be submitted.

hoferchr
June 29th, 2005, 09:04 PM
I'm notcing that when I fill out a form that I've created (using the tutorials and help from you above), that if there is a single or double quotation mark in the body of the message, i.e. ' or ", that when the e-mail gets to my Inbox, there's always a backslash (i.e. \) in front. For example:

- - - - -
This form isn\'t \"live\" yet, so I\'ll wait for your approval before I link it anywhere.
- - - - -

How come this is happening, and can I do anything to remove the backslash from the messages?

Chris


You will want to do that with JavaScript on the client side. Add this between the <head> </head> tags and change myradiobuttonname with the name of your radio buttons.


Validate(thisform) {
myOption = -1;
for (i=0; i<thisform.myradiobuttonname.length; i++) {
if (thisform.myradiobuttonname[i].checked) {
thisform.submit();
}
}
alert('You must select a radio button.');
return false;
}

Then on your submit button, do this and change myform with the name of your form

<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Submit" />

This will make sure one of the radio buttons is selected before it will allow the form to be submitted.

hl
June 29th, 2005, 11:18 PM
use the stripslashes() function of PHP www.php.net/stripslashes