Form - php - email...Help!!
- Started
- Last post
- 7 Responses
- designer101
Hi there
I'm trying to create a form that will send the collected info in an email through php.
I can get the text boxes to work but not the radio buttons. How can I get the php to tell only send the selected radio button. My php knowledge is limited I'm afraid .I've pasted the code below, thanks in advance
HTML
<form action="contact.php" method="post">Your Name:
<input name="yourname" type="text" size="40" />
Company Name:
<input name="companyname" type="text" size="40" />
Location:
<input name="location" type="text" size="40" />
Email address:
<input name="email" type="text" size="40" />
Contact Number:
<input name="contactnumber" type="text" size="40" />Area of Enquiry:
<input name="enquirys" type="text" size="40" />
Comments:
<textarea name="comments" cols="36" rows="4" class="body"></textarea>Contact me via:
email <input type="radio" value="by_email" name="emailcontact">
phone <input type="radio" value="by_phone" name="phonecontact"><input type="submit" value="Send it!" />
PHP CODE:
<?php
/* Set e-mail recipient */
$myemail = "email address here";/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$companyname = check_input($_POST['companyname... "Enter your company name");
$location = check_input($_POST['location'], "Enter your location");
$email = check_input($_POST['email'], "Enter your email");
$contactnumber = check_input($_POST['contactnumbe... "Enter your phone number");$enquirys = check_input($_POST['enquirys'], "Enter the area of enguiry");
$comments = check_input($_POST['comments'], "Write your comments");$emailcontact = check_input($_POST['emailcontact... ;
$phonecontact = check_input($_POST['phonecontact... ;
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w... $email))
{
show_error("E-mail address not valid");
}
- crillix0
The php code you pasted appears to be just some of the validation code and not the sending bit, with out more info on what your code is doing its hard to say where the issue is.
With the radio buttons, do you want them to only be able to select one? or have the option to select both? If it is only select one or the other, then you need to change the name of the radio buttons to be the same.
Either way a simple if statement of..
if($_POST['xxRadioButtonNamexx'] != "")
{ ** do something ** }... in your sending bit would work. The do something would be similar to how the other pieces of data are collected to create the body of the email.
- selecting both would be a checkbox********
- Ummm not with how the original html was done.crillix
- selecting both would be a checkbox
- designer1010
This is the full php code. I downloaded this from some site. I don't know how to hard code it myself.
<?php
/* Set e-mail recipient */
$myemail = "email here";/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$companyname = check_input($_POST['companyname'], "Enter your company name");
$location = check_input($_POST['location'], "Enter your location");
$email = check_input($_POST['email'], "Enter your email");
$contactnumber = check_input($_POST['contactnumber'], "Enter your phone number");$enquirys = check_input($_POST['enquirys'], "Enter the area of enguiry");
$comments = check_input($_POST['comments'], "Write your comments");$emailcontact = check_input($_POST['emailcontact... ;
$phonecontact = check_input($_POST['phonecontact... ;
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}/* Let's prepare the message for the e-mail */
$message = "Hello!Your contact form has been submitted by:
Name: $yourname
Company Name:Location: $location
E-mail: $email
Contact Number: $contactnumberArea of enguiry: $enquirys
Comments:
$commentsContact method:
$emailcontact
$phonecontactEnd of message
";/* Send the message using mail() function */
mail($myemail, $subject, $message);/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}function show_error($myError)
{
?>
- designer1010
The user should be able to select both if they need to
- crillix0
Ok, radio buttons can be a little bit of a pain to work with, but if you want to use them I would recommend adding an additional radio button for both... the reason being is once you select a radio button you can't un-select it.
so add this to the html portion...
Contact me via:
Contact me via:
<input type="radio" value="Email" name="contactmethod"> by Email
<input type="radio" value="Phone" name="contactmethod"> by Phone
<input type="radio" value="Email or Phone" name="contactmethod" checked="checked" /> by Email or PhoneIn the validation portion, replace...
$emailcontact = check_input($_POST['emailcontact... ;
$phonecontact = check_input($_POST['phonecontact... ;
with...
$contactmethod = $_POST['contactmethod'];and in the prepare the message, replace...
Contact method:
$emailcontact
$phonecontact
with...
Contact method:
By $contactmethodThat should do it. Having one checked by default allows you to skip validation for it, as really they have to check one anyway or there is no point contacting you.
- designer1010
Thanks a mill I'll try that
- designer1010
It worked Crillix. Thanks again for your help
- crillix0
Glad to be of assistance =)