...

View Full Version : Form, If other then show this question too.



MrTIMarshall
12-05-2010, 10:23 PM
I have a .php contact form on my site which I would like to update with a new question added but need some help.

I would like a question for example;

How did you find us

Newspaper
Television Ad
Google
Other


Which will be (although I need help with this too) a dtop down menu. But i would like an extra question to appear if they choose 'Other' to appear which asks them to specify


Thanks in advance for the help!

Tim

Lamped
12-05-2010, 11:09 PM
HTML:

<select name="wheredidyoufindus"><option>Newspaper</option><option>Television Ad</option><option>Google</option><option>Other</option></select>

jQuery (if you want proper Javascript, go to the Javascript forum):


$(function() {
$('select[name=wheredidyoufindus]').change(function() {
if ($(this).val() == 'Other') {
$(this).after('<input type="text" name="wheredidyoufindus" />');
$(this).remove();
}
}
}

PHP:


$wheredidyoufindus = $_REQUEST['wheredidyoufindus'];
// $wheredidyoufindus will be either the selected option, or the text field contents


All untested.

MrTIMarshall
12-06-2010, 12:42 AM
I tried what you said, which hasn't worked but I maybe entering things in the wrong place if the does work. Can you run me through a bit more, also I forgot to add I have a Validation system in place and want to know what to edit it so if the space says 'Please Select' it wont send.

My code for the e-mail is;

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}



Many Thanks,
Tim

Lamped
12-06-2010, 12:52 AM
It's easier if we can see what you've done, and how you've implemented my code. Also, if you're using my javascript, did you include jQuery?

Anyhoo, I'm off to bed. I'll check back in the morning.

MrTIMarshall
12-06-2010, 01:00 AM
My Sites Code (http://www.naturallytalented.co.uk/Contact.php


<?php
//If the form is submitted
if(isset($_POST['submit'])) {

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}

//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['howdidyoifindus']) == 'Tdlevision Ad') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
$wheredidyoufindus = $_REQUEST['wheredidyoufindus'];
// $wheredidyoufindus will be either the selected option, or the text field contents

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}

//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'Support@naturallytalented.co.uk'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="CssStyleSheet.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.validate.pack.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#contactform").validate();
});
</script>
<script type="text/javascript" src="sdmenu.js"></script>
<script type="text/javascript">
// <![CDATA[
var myMenu;
window.onload = function() {
myMenu = new SDMenu("my_menu");
myMenu.init();
};
</script>
<style type="text/css">
<!--
.style1 {color: #000000}
-->
</style>
</head>

<body>
<div class="ContainerMain">
<div class="ContainerTop">
<div class="ContainerHeader">
</div>
<div class="ContainerTopNav">
<div style="float: left" class="MiniNavB">
<div class="collapsed">
<span>Support
</span>
</div>
</div>
<div style="float: left" class="MiniNavB">
<div class="collapsed">
<span>
<a href="Contact.php">Contact</a>
</span>
</div>
</div>
</div>
</div>
<div class="ContainerMiddle">
<div class="ContainerLeft">
<div style="float: left" id="my_menu" class="sdmenu">
<div class="collapsed">
<span>Home
</span>
<a href="index.html">Latest News</a>
<a href="">OFFERS</a>
</div>
<div class="collapsed">
<span>Piano Lessons
</span>
<a href="">Method</a>
<a href="">Examples</a>
<a href="">Testimonals</a>
<a href="">Apply For Lessons</a>
</div>
<div class="collapsed">
<span>Function Hire
</span>
<a href="">Events</a>
<a href="">Gallery</a>
<a href="">Testimonials</a>
<a href="">Get A Quote</a>
</div>
<div>
<span>Support
</span>
<a href="">Custmour Support</a>
<a href="">FAQ's</a>
<a href="">Parental Guide</a>
<a href="Contact.php">Contact</a>
</div>
</div>
</div>
<div class="ContainerMiddleMain1">
<div class="ContainerMiddleMain2">
<h1 class="style1">Contact</h1>
<h4 class="style1">By Post</h4>
<p class="style1"> Naturaly Tallented<br />
Appartment Two<br />
59a Marine Promenade<br />
New Brighton<br />
Wallasey<br />
CH45 2JS</p>
<span class="style1"><br />
</span>
<h4 class="style1">By Phone</h4>
<p class="style1">Telephone: (0151) 639 5389<br />
Mobile: 0793 332 9771 </p>
<span class="style1"><br />
</span>
<h4 class="style1"> By E-Mail</h4>
<p class="style1">
Please complete the form below.
</p>
<br />
<h4 class="style1">By Enquiry Form;</h4>
<br />
<div style="float: left" class="SubHead">
<div class="collapsed">
<span>
Enquiry Form
</span>
</div>
</div>
<div class="ContainerMiddleInfo">
<div id="contact-wrapper">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>

<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<label for="name"><strong>Name:</strong></label>
<input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
</div>

<div>
<label for="wheredidyoufindus"><strong>Name:</strong></label>
<select name="wheredidyoufindus">
<option>Newspaper</option>
<option>Television Ad</option>
<option>Google</option>
<option>Other</option>
</select>
</div>
<div>
<label for="email"><strong>Email:</strong></label>
<input type="text" size="50" name="email" id="email" value="" class="required email" />
</div>

<div>
<label for="subject"><strong>Subject:</strong></label>
<input type="text" size="50" name="subject" id="subject" value="" class="required" />
</div>

<div>
<label for="message"><strong>Message:</strong></label>
<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
</div>
<input type="submit" value="Send Message" name="submit" />
</form>
</div>
</div>
</div>
</div>
<div class="ContainerRught">
</div>
</div>
<div class="ContainerBottom">
</div>
</div>
</body>
</html>


I've placed the JavaScript Text into one of the .js text/css files.

Lamped
12-06-2010, 10:25 AM
There's a shocking number of typos in the code. You need to check every reference to the <select> element's name "wheredidyoufindus" and correct them.

I don't usually comment on typos, but in your case, it's breaking your code.

MrTIMarshall
12-06-2010, 01:13 PM
I don;'t understand properly... you gave e that code?...

Lamped
12-06-2010, 07:15 PM
I think you want to recheck the stuff you added. It's certainly not a copy/paste of my code as it's got new typos. Don't get me wrong, I'm the undisputed king of extra brackets and such, but you put some typos in my code :p

MrTIMarshall
12-06-2010, 07:55 PM
Okay, I have fixed certain issues such as it now needs requiring if blank although I would like to edit this somehow so it says that it is required if it says 'Please choose one'... But this might be as far as I can go...


My php code is now;


//Check to make sure sure that a valid Option is submitted
if(trim($_POST['wheredidyoifindus']) == '') {
$hasError = true;
} else {
$wheredidyoufindus = trim($_POST['wheredidyoufindus']);
}


And my HTML code is;


<div>
<label for="wheredidyoufindus"><strong>Name:</strong></label>
<select name="wheredidyoufindus" id="wheredidyoufindus" class="required">
<option></option>
<option>Newspaper</option>
<option>Television Ad</option>
<option>Google</option>
<option>Other</option>
</select>
</div>


Hope I'm improving.... Any guidance to as what to do next please?

Lamped
12-06-2010, 09:32 PM
Second line of that PHP code, "yoi" should be "you". Typo, see :p

MrTIMarshall
12-06-2010, 10:17 PM
Okay, now that that has been edited, thank you, what should I do next to make the option 'Other' make a new box appear?

Lamped
12-06-2010, 10:52 PM
Post #2 contains some javascript and an amended <select> element for you to use. You need to place the javascript either in a separate file, or in a <script type="text/javascript"></script> section. This javascript will detect when the selection is changed, and when Other is chosen, will remove the select box, and replace it with a text box.

You also need to include JQuery - Google that.

MrTIMarshall
12-07-2010, 12:32 AM
I added;

<script src="OptionOther.js" type="text/javascript"></script>

For the JQuery - Google, do you mean;


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>


As I had this all the long :p

It still doesn't add a new field yet (unless there's more to do, then sorry)

Lamped
12-07-2010, 09:34 AM
Can you give me a link to the site it's implemented on. If it's private, PM it to me.

MrTIMarshall
12-07-2010, 06:53 PM
I don't understand what you are asking for...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum