Need to email multiple recipients from combobox - php

I need to email multiple email addresses from each selection of a drop down box, however everything i have tried is not working.. Here is what I have so far
Im using this in the start of the form
$mailto = $_POST['unit'];
This is what I have for the drop down box
<select name="unit" size="1" id="Combobox1" style="position:absolute;left:43px;top:324px;width:61px;height:21px;z- index:18;">
<option value= 'email#email.com'; 'email#email.com'> M17</option>
<option value="email#email.com">M16</option>
</select>
The problems im having
1. on option M17 it only sends to the first email address
2. it sends the email address instead of the unit number (M17)
3. Need to figure out a way to edit the email addresses through a seperate "user friendly" page
</head>
<body>
<div id="space"><br></div>
<div id="container">
<div id="wb_Form1"
<form name="11_Dispatch" method="post" action="<?php echo basename(__FILE__); ?>" enctype="multipart/form-data" id="Form1">
<input type="hidden" name="formid" value="form1">
<div id="wb_Text1" style="position:absolute;left:10px;top:76px;width:111px;height:16px;z-index:0;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Address</span> </div>
<input type="text" id="Editbox1" style="position:absolute;left:131px;top:76px;width:198px;height:23px;line-height:23px;z-index:1;" name="LOC" value="">
<div id="wb_Text2" style="position:absolute;left:10px;top:106px;width:111px;height:16px;z-index:2;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Subdivision</span></div>
<input type="text" id="Editbox2" style="position:absolute;left:131px;top:106px;width:198px;height:23px;line-height:23px;z-index:3;" name="SUB" value="">
<div id="wb_Text3" style="position:absolute;left:10px;top:136px;width:111px;height:16px;z-index:4;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Call back # </span></div>
<input type="text" id="Editbox3" style="position:absolute;left:131px;top:136px;width:198px;height:23px;line-height:23px;z-index:5;" name="#" value="">
<div id="wb_Text5" style="position:absolute;left:10px;top:166px;width:111px;height:16px;z-index:6;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Chief Complaint</span></div>
<input type="text" id="Editbox4" style="position:absolute;left:131px;top:166px;width:198px;height:23px;line-height:23px;z-index:7;" name="CC" value="">
<div id="wb_Text6" style="position:absolute;left:10px;top:196px;width:111px;height:16px;z-index:8;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Pt name</span> </div>
<input type="text" id="Editbox5" style="position:absolute;left:131px;top:196px;width:198px;height:23px;line-height:23px;z-index:9;" name="NAME" value="">
<div id="wb_Text7" style="position:absolute;left:10px;top:226px;width:111px;height:16px;z-index:10;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Pt Age</span> </div>
<input type="text" id="Editbox6" style="position:absolute;left:131px;top:226px;width:198px;height:23px;line-height:23px;z-index:11;" name="AGE" value="">
<div id="wb_Text9" style="position:absolute;left:10px;top:256px;width:111px;height:16px;z-index:12;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Callers Name</span></div>
<input type="text" id="Editbox7" style="position:absolute;left:131px;top:256px;width:198px;height:23px;line-height:23px;z-index:13;" name="CALLER" value="">
<div id="wb_Text10" style="position:absolute;left:10px;top:286px;width:111px;height:16px;z-index:14;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:13px;">Identifying Factors</span></div>
<input type="text" id="Editbox8" style="position:absolute;left:131px;top:286px;width:198px;height:23px;line-height:23px;z-index:15;" name="NOTES" value="">
<input type="submit" id="Button2" name="" value="Dispatch" style="position:absolute;left:169px;top:357px;width:96px;height:25px;z-index:16;">
<div id="wb_Text12" style="position:absolute;left:166px;top:30px;width:133px;height:22px;z-index:17;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:19px;"><strong>911 Dispatch</strong></span></div>
<select name="unit" size="1" id="Combobox1" style="position:absolute;left:43px;top:324px;width:61px;height:21px;z-index:18;">
<option value="M17">M17 </option>
<option value="M16">M16</option>
<option value="M15">M15</option>
<option value="M14">M14</option>
<option value="M11">M11</option>
<option value="M10">M10</option>
<option value="M07">M07</option>
</select>
</form>

As suggested, don't put your to addresses in the form. This is just basic and the mail() part is right from the manual -> http://php.net/manual/en/function.mail.php (Example #4 Sending HTML email shows multiple email recipients using concatenation however, I am using implosion):
PHP:
if(isset($_POST['unit'])) {
// Store emails in the code or database to draw from:
// Personally I like to store this in an array (if I were to hardcode it)
// Reason being, is that if you suddenly one day get access to a database
// and you draw a bunch of email addresses from that database, those emails
// would likely be listed as an array.
// By storing each email in a same-named array, you basically
// create an easy-to-read mailing list
$units['m17'][] = 'email1#tester.com';
$units['m17'][] = 'email2#tester.com';
$units['m16'][] = 'email3#tester.com';
// if the <select> value of $_POST['unit'] equals 'm17'
// $units[$_POST['unit']] then is the same as $units['m17']
$to = implode(",",$units[$_POST['unit']]);
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com'."\r\n";
// As noted in the PHP Manual, the function implode() takes an array
// and combines the values into a string with a "glue", in this case a comma
// $to would then equal: "email1#tester.com,email2#tester.com" if
// $_POST['unit'] equals 'm17'.
mail($to, $subject, $message, $headers);
}
FORM:
<select name="unit" size="1" id="Combobox1" style="position: absolute; left: 43px; top: 324px; width: 61px; height: 21px; z-index: 18;">
<option value="m17">M17</option>
<option value="m16">M16</option>
</select>

Related

How can I use autoincrement without the help of database in php

I have a form which user submits and is sent directly to the e-mail. The problem is that I'm not storing these values in database and directly mailing them. It is like a complaint registering system. What I want to do is that when the user submits a complaint and is redirected to success page, a complaint number is generated which should be obviously incremental by 1 for the next submission. Also there is no separate user account as anyone visiting the website can submit complaints. I tried using a field option as unique id but it didn't really work.
The html form is,
<form method="post" action="handler.php">
<div>
<label for="first_name"><span class="labelname"><strong>First Name:</strong></span></label>
<input type="text" maxlength="50" size="50" name="first_name" id="first_name" value="" class="required" />
</div>
<div>
<label for="last_name"><span class="labelname"><strong>Last Name:</strong></span></label>
<input type="text" maxlength="50" size="50" name="last_name" id="last_name" value="" class="required" />
</div>
<div>
<label for="telephone"><span class="labelname"><strong>Telephone Number:</strong></span></label>
<input type="text" maxlength="20" size="50" name="telephone" id="telephone" value="" class="required" />
</div>
<div>
<label for="email"><span class="labelname"><strong>E-mail: (Optional)</strong></span></label>
<input type="email" maxlength="30" size="50" name="email" id="email" value="" class="" />
</div>
<div>
<label for="com_type"><span class="labelname"><strong>Complaint Type:</strong></span></label>
<select name="com_type" id="com_type" class="required">
<option value=""></option>
<option value="Electrician">Electrician</option>
<option value="Plumber">Plumber</option>
<option value="Mason">Mason</option>
<option value="Miscellaneous">Miscellaneous</option>
</select>
</div>
<div>
<label for="flat_no"><span class="labelname"><strong>Flat No.:</strong></span></label>
<input type="text" maxlength="10" size="50" name="flat_no" id="flat_no" value="" class="required" />
</div>
<div>
<label for="block_no"><span class="labelname"><strong>Block Number:</strong></span></label>
<select name="block_no" id="block_no" class="required">
<option value=""> </option>
<option value="A-1">A-1</option>
<option value="A-2">A-2</option>
<option value="A-3">A-3</option>
<option value="A-4">A-4</option>
<option value="A-5">A-5</option>
<option value="A-6">A-6</option>
<option value="A-7">A-7</option>
<option value="B-1">B-1</option>
<option value="B-2">B-2</option>
<option value="B-3">B-3</option>
<option value="B-4">B-4</option>
<option value="C-1">C-1</option>
<option value="C-2">C-2</option>
</select>
</div>
<div>
<label for="message"><span class="labelname"><strong>Describe your problem:</strong></span></label>
<textarea rows="10" cols="50" maxlength="2000" name="message" id="message" class="required"></textarea>
</div>
<button class="submit" type="submit" name="submit" value="Send Email">Submit Complaint</button> <button class="reset" type="reset">Reset</button>
php code,
<?php
if(!isset($_POST['submit']))
{
die("Error. You need to submit the form.");
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$telephone = $_POST['telephone'];
$visitor_email = $_POST['email'];
$com_type = $_POST['com_type'];
$flat_no = $_POST['flat_no'];
$block_no = $_POST['block_no'];
$message = $_POST['message'];
$email_from = $visitor_email;
$email_subject = "New Complaint";
$email_body = "message\n\n". "First Name: $first_name\n\n".
"Last Name: $last_name\n\n".
"Telephone: $telephone\n\n". "Complaint Type: $com_type\n\n".
"Flat No.: $flat_no\n\n". "Block No.: $block_no\n\n".
"Complaint: $message";
$to = "my email.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
try{
mail($to,$email_subject,$email_body,$headers);
//success, redirect to thank
header('Location: http://mywebsite.com/thank.php');
} catch(Exception $e){
//problem, redirect to fail
header('Location: http://mywebsite.com/fail.php');
}
?>
I just want a complaint number on the successful submission page and the complaint number should also go in the mail also with other details. Can I do it without using the database. Please help.
If you have the numbers stored as a primary key, then you can do something like this:
SELECT COUNT(*) FROM `table`;
Or if you have the auto_increment or PRIMARY KEY set, you can use:
SELECT MAX(`id`) FROM `table`;
And then add + 1 to the result and insert it as new. If you aren't using a database, then use a flat file named count.txt for it and put in the current number and increment:
<?php
$count = file_get_contents("count.txt");
$count++;
file_put_contents("count.txt", $count);
?>
But this option is not so good. So please use a mechanism to lock the file while updating the count.

HTML Form - PHP Email with custom messages and tickbox

I have a HTML form with radio buttons, option boxes (drop down boxes), checkboxes, text fields and so on. I have the form pointed to another file called send.php to email the form on but how would I do this with the tickboxes and radio buttons and input text in between each answer? I would kind of like to format it like this:
Welcome: {name}
Your age group it between: {radio button with age groups}
And so on. I can't give you the actual code as it is private but I can give this instead which uses the kind of code and format:
<form action="send.php">
<input type="radio" name="AgeGroup" value="AgeGroup1"> 0-18<br>
<input type="radio" name="AgeGroup" value="AgeGroup2"> 19-29<br>
<input type="radio" name="period" value="AgeGroup3"> 30-39<br>
<input type="radio" name="period" value="AgeGroup4"> 40-49<br>
<input type="radio" name="period" value="AgeGroup5"> 50+<br>
<br><br><br><br>
<select name="Country">
<option value ="UK">United Kingdom</option>
<option value ="USA">United States</option>
</option></select>
<br><br><br><br>
<input type="text" name="PostCode" size="5">
<br><br><br><br>
<input type="text" name="HouseNumber" size="5">
<br><br><br><br>
<textarea id="Family" class="input" name="FamilyNumber" rows="10" cols="60"></textarea>
<br><br><br><br>
<input type="checkbox" name="Delievery" value="NextDay"> Next Day Delievery
<br>
<input type="checkbox" name="Delievery" value="TwoToFive"> 2-5 Day
<br>
<input type="checkbox" name="Outcome" value="Dismissed"> Dismissed
<br><br><br><br><br><br>
<center><button id="Send" type="submit" style="height:25px; width:100px; background-color: grey">Send</button></center>
</form>
Sorry it's so random. I ran out of ideas! Also sorry for my coding abilities, I don't normally do HTML!
Thanks.
I'm hoping this will help...if I'm understanding your question correctly.
Make sure to add a method to your form tag. For example:
<form action="send.php" method="post">. In send.php, you want to grab your variables by name attribute, for example:
$name = $_POST['Name'];
$ageGroup = $_POST['AgeGroup'];
And then you want to build out your email. PHP allows variables to be parsed in double quote strings, which can help you build your message the way you want it. (Reference)
$to = "person#example.com";
$subject = "Subject goes here";
$message = "Welcome: $name. Your age group is between: $ageGroup";
//note: headers are optional
$headers = "From: you#example.com" . "\r\n" . "CC: anotherperson#example.com";
mail($to, $subject, $message, $headers);
This is just a simple example, but this might be able to get you started.
you need to add method="POST" attribute to tag.
then, in send.php to read values, for example PostCode or HouseNumber:
echo "Post code: ".$_POST["PostCode"]."<br />";
echo "House number: "$_POST["HouseNumber"]";
for age:
if(isset($_POST["AgeGroup"]) {
echo "Age group: ".$_POST["AgeGroup"];
}
if(isset($_POST["period"]) {
echo "Age group: ".$_POST["period"];
}
for more info go to manual: http://php.net/manual/en/reserved.variables.post.php
This way you can group the radio buttons into one name, and you can get the values in the php as $_POST['name']
<form action="send.php" method="post">
<input type="radio" name="AgeGroup" value="0-18"> 0-18<br>
<input type="radio" name="AgeGroup" value="19-29"> 19-29<br>
<input type="radio" name="AgeGroup" value="30-39"> 30-39<br>
<input type="radio" name="AgeGroup" value="40-49"> 40-49<br>
<input type="radio" name="AgeGroup" value="50+"> 50+<br>
<select name="Country">
<option value ="UK">United Kingdom</option>
<option value ="USA">United States</option>
</option></select>
<input type="text" name="PostCode" size="5">
<input type="text" name="HouseNumber" size="5">
<textarea id="Family" class="input" name="FamilyNumber" rows="10" cols="60"></textarea>
<input type="checkbox" name="Delievery" value="NextDay"> Next Day Delievery
<br>
<input type="checkbox" name="Delievery" value="TwoToFive"> 2-5 Day
<br>
<input type="checkbox" name="Outcome" value="Dismissed"> Dismissed
<center><button id="Send" type="submit" style="height:25px; width:100px; background-color: grey">Send</button></center>
</form>

intermittent duplicate emails from php contact form

First time posting here. I have a php contact form on a CMS site that is sending me duplicate emails. The problem is intermittent. Sometimes it send two copies, sometimes it sends as many as 8 copies. Sometimes it works normally. The issue started after removing some fields from the form.
The form submits to a separate page that sends the email to 3 recipients. This page also serves as a thank you page that displays the information submitted by the user. "Thanks for contacting us the following information has been received".
The embeds are expresionengine code. Thanks for your help!!!
Here is the form:
<form id="Form1" name="Form1" method="post" action="http://myprocessingpage.com">
<label for="fname">Name:</label> <input type="text" size="42" id="fname" name="fname"><br>
<label for="email">Email Address:</label> <input size="42" type="text" id="email" name="email"><br>
<label for="telephone">Telephone:</label> <input size="42" type="text" id="telephone" name="telephone"><br>
<br>
<div style="float: left; margin-right: 20px; margin-bottom: 30px;">
<label>How did you hear about us?</label> <br>
<input type="hidden" id="arrayfix" name="how[]" value="">
<input type="checkbox" id="website" name="how[]" value="website"> Website<br>
<input type="checkbox" id="referral" name="how[]" value="referral"> Referral<br>
<input type="checkbox" id="tradeshow" name="how[]" value="tradeshow"> Tradeshow<br>
</div>
<div style="float: left; margin-bottom: 30px;">
<label >Shelter Type:</label><br>
<input type="hidden" id="arrayfix2" name="type[]" value="">
<input type="checkbox" id="safe4x6" name="type[]" value="Safe_Room_4X6"> 4'X6' Shelter<br>
<input type="checkbox" id="safe4x8" name="type[]" value="Safe_Room_4X8"> 4'X8' Shelter<br>
<input type="checkbox" id="custom" name="type[]" value="Custom_Size_Safe_Room"> Custom Size Shelter<br>
</div>
<div style="clear: both;"></div>
<label for="question"> Questions or Comments:</label><br> <textarea rows="7" maxlength="500" name="question" id="question" cols="50"></textarea><br>
<br>
<input type="submit" class="btnimage" id="submit" name="submit" value="">
</form>
Here is the processing page (http://myprocessingpage.com):
<?php
if (isset($_POST['fname']))
{
?>
<!DOCTYPE html>
<head>
<title>Thank you for contacting us!</title>
{embed="page/headtags"}
</head>
<body>
{embed="page/header"}
{embed="page/drop"}
<div id="contentbg">
<div id="content">
<?php
$name = $_POST['fname'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$question = $_POST['question'];
$howarray = $_POST['how'];
$howimplode = implode("\n", $howarray);
$how = str_replace("_", " ", "$howimplode");
$typearray = $_POST['type'];
$typeimplode = implode("\n", $typearray);
$type = str_replace("_", " ", "$typeimplode");
//sent to us
$to = "mail1#mail.com, mail2#mail.com, mail3#mail.com";
$subject = "Info Request";
$message = "INFO REQUEST:\n\nName: $name\n\nEmail: $email\n\nTelephone: $telephone\n\nHow they heard about us:\n$how\n\nShelter type:\n$type\n\nQuestions or Comments:\n$question";
$from = "info#mysite.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
<div id="form">
<p style="margin-top: 0px; margin-bottom: 40px; color: green; font-weight: bold;">Thank you for contacting us! The following information has been received:</p>
<div style="margin-left: 30px;">
<?php
echo "<p><b>Name:</b> ".$name."</p>";
echo "<p><b>Email:</b> ".$email."</p>";
echo "<p><b>Telephone:</b> ".$telephone."</p>";
$thankshowimplode = implode("</br>", $howarray);
$thankshow = str_replace("_", " ", "$thankshowimplode");
$thankstypeimplode = implode("</br>", $typearray);
$thankstype = str_replace("_", " ", "$thankstypeimplode");
echo "<p><b>How you heard about us:</b></br> ".$thankshow."</p>";
echo "<p><b>Type of shelter(s):</b></br> ".$thankstype."</p>";
echo "<p style='word-wrap:break-word;'><b>Questions or Comments:</b> ".$question."</p>";
?>
</div>
</div>
</div>
{embed="page/footer"}
</body>
</html>
<?php
}
else
{
?>
<script type="text/javascript">
window.location = 'http://mycontactpage.com';
</script>
<?php
}
?>
If someone double-clicks (or clicks it 8 times) your submit button, your script could be run more than once. Try disabling the submit button on click (using Javascript).
Another, probably unnecessary, solution would be to lock your form script (e.g. using semaphores), create a database entry with the provided info, then check to see if you've sent the same info recently (e.g. within the last five minutes), and don't sent it again if you have.
I'm sure the Javscript disable functionality will suffice :)
Or, if you want to find out what's really going on, you could just create a log file that logs "sent [some distinct piece of data that might tell these posts apart] at [timestamp]". If you see a couple of these, with the same [distinct info] entry and really close timestamp, then your problem is multiple clicks to the submit button.

Tutorials for passing value data to email with PHP

I have built a html form in which a user has to specify which product he likes to order and depending on his answer other input fields arise. I have used some javascript for that. But now i want to data the user enters to be passed on to email via php. I know very little of php and scripting in general (let's say about nothing) but i did find some tutorials and templates on it. The only thing is that they all seem slightly different and specific to a form (mostly contact forms). And if i follow them closely it works fine, it's just that when i want to customize a little i seem to get terribly lost.
I have posted this before (Can i post data from a select drop down to email using PHP?) and have gotten some answers. It's just that i didn't formulate my initial problem well enough and edited it afterwards, but i don't know if that's the way to go on this forum. So i posted a new question (sorry if that's overkill, i'm pretty new to all of this). This is my html:
<form method="post" name="contact_form" action="contact.php">
<div class="form-div1">
<label for="profile" class="label1">Select your profile</label>
<br>
<select id="user-type" class="selectlist">
<option value="option1">I'm a first-time user</option>
<option value="option2">I would like to renew or upgrade</option>
</select>
</div>
<div class="form-div2">
<label for="SEN" class="label2">SEN</label>
<br>
<input type "text" name="input1" class="input1">
</div>
<div class="form-div3">
<label for="email" class="label3">Email Address</label>
<br>
<input type "text" name="input2" class="input2">
</div>
<div class="form-div4">
<label for="product_choice" class="label4" name="select_menu">Select your product</label>
<br>
<select id="product" class="selectlist">
<option value="option1">JIRA</option>
<option value="option2">Confluence</option>
<option value="option3">JIRA Service Desk</option>
<option value="option4">Stash</option>
<option value="option5">Other</option>
</select>
</div>
<div class="form-div42">
<label for="product" class="label42">Specify your product</label>
<br>
<input type "text" name="input2" class="input2">
</div>
<div class="form-div5">
<label for="license_choice" class="label5">Select your license</label>
<br>
<select id="select" class="selectlist">
<option value="option1">25 users</option>
<option value="option2">50 users</option>
<option value="option3">100 users</option>
</select>
</div>
<div class="input_box_atlassian">
<input type="submit" name="submit" value="Submit" class="submit-button-atl" />
</div>
</form>
Now i want to build working php so i can pass the data to an email. Can somebody point me in the right direction or preferably show me some tutorials that tackle this issue more closely.
I havent dont any validation, try client side & server side validation along with code
Change the HTML as below
<form method="post" name="contact_form" action="contact.php">
<div class="form-div1">
<label for="profile" class="label1">Select your profile</label>
<br>
<select id="user_type" name="user_type" class="selectlist">
<option value="I'm a first-time user">I'm a first-time user</option>
<option value="I would like to renew or upgrade">I would like to renew or upgrade</option>
</select>
</div>
<div class="form-div2">
<label for="SEN" class="label2">SEN</label>
<br>
<input type "text" name="SEN" class="SEN">
</div>
<div class="form-div3">
<label for="email" class="label3">Email Address</label>
<br>
<input type "text" name="email" class="email">
</div>
<div class="form-div4">
<label for="product_choice" class="label4" name="select_menu">Select your product</label>
<br>
<select id="product" name="product" class="selectlist">
<option value="JIRA">JIRA</option>
<option value="Confluence">Confluence</option>
<option value="JIRA Service Desk">JIRA Service Desk</option>
<option value="Stash">Stash</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-div42">
<label for="product" class="label42">Specify your product</label>
<br>
<input type "text" name="product_specify" id="product_specify" class="input2">
</div>
<div class="form-div5">
<label for="license_choice" class="label5">Select your license</label>
<br>
<select id="license_choice" name="license_choice" class="selectlist">
<option value="25">25 users</option>
<option value="50">50 users</option>
<option value="100">100 users</option>
</select>
</div>
<div class="input_box_atlassian">
<input type="submit" name="submit" value="Submit" class="submit-button-atl" />
</div>
</form>
And make another PHP file by the name contact.php & paste the below code.
<?php
if($_REQUEST['submit']=='Submit'){
$user_type=$_REQUEST['user_type'];
$sen=$_REQUEST['SEN'];
$email=$_REQUEST['email'];
$product=$_REQUEST['product'];
$product_specify=$_REQUEST['product_specify'];
$license_choice=$_REQUEST['license_choice'];
$to = 'nobody#example.com'; //to whom the email have to besnd
$message = "User Type: $user_type \n Sen: $sen \n Email: $email \n Product: $product \n Product Specified: $product_specify \n License: $license_choice"; //content of the email
$subject = 'hello'; //shown as subject of the email
$headers = 'From: webmaster#example.com' . "\r\n" . //eamil header information
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
echo "Mail Sucessfully Sent";
} else {
echo "Something went wrong";
}
}
?>
This will get the content from the form HTML & email its contents, however only if the email server is configured correctly
Consider the following URL http://www.freecontactform.com/email_form.php
Cheers!!

How to use jquery to post dropdown values to another php form

Thank you for your time, I have spent a good part of my day googling every way I can think of and I cant find a clear simple answer. Ive tried everything I can think of and have found great answers here. I sure hope someone can help me. Ive done most of my research on jquery on there site and to no avail I'm still looking for answers. Ive found some help full articles here also, I just must not be fully understanding or am overlooking some simple facts. I'm definitely new to all of this so lets take it nice and easy ! To start I am taking user form data and validating with a php script that will hopefully be talking to a data base and storing form info.
So here are the forms guts:
<form method="post" action="test.php" name="contactform" id="contactform">
<div class="grid_6" id="register">
<center><h4>Required Information</h4></center>
<p>
<label for="name">Name:</label>
<input name="name" id="name" type="text" />
</p>
<p>
<label for="email">Your Email:</label>
<input name="email" id="email" type="text" />
</p>
<p>
<label for="trew">Contact Phone:</label>
<input name="txtAreaCode" id="txtAreaCode" style="width: 30px;" maxlength="3" value=""type="text">
<span style="color: rgb(255, 200, 46);"> - </span>
<input name="txtPrefix" id="txtPrefix" style="width: 30px;" maxlength="3" value=""type="text">
<span style="color: rgb(255, 200, 46);"> - </span>
<input name="txtPhone" id="txtPhone" style="width: 45px;" maxlength="4" value=""type="text">
<span style="color: rgb(255, 200, 46);"> - </span>
<input name="txtPhoneExt" id="txtPhoneExt" style="width: 35px;" maxlength="10" value=""type="text">
ext.
</p>
<p>
<label for="zip">Zip Code:</label>
<input name="zip" id="zip" type="text" />
</p>
<p>
<label for="school">Name of School:</label>
<input name="school" id="school" type="text" />
</p>
<p>
<label for="title">Affiliation</label>
<select name="title">
<option selected="NULL">Please Select</option>
<option value="student">Student</option>
<option value="parent">Parent</option>
<option value="teacher">Teacher</option>
<option value="booster">Booster</option>
<option value="clubpres">Club President</option>
<option value="principal">Principal</option>
<option value="ptsa">PTSA</option>
</select>
</p>
</div>
<div class="grid_6" id="contactinfo">
<center><h4>Additional Information</h4></center>
<p>
<label for="color">School Colors:</label>
<input name="color" id="color" type="text" />
</p>
<p>
<label for="mascot">Mascot:</label>
<input name="mascot" id="mascot" type="text" />
</p>
<p>
<label for="tagline">Tagline/Motto:</label>
<input name="tagline" id="tagline" type="text" />
</p>
<p>
<label for="sbsize">Approximate Student Body Size:</label>
<input name="sbsize" id="sbsize" type="text" />
</p>
<p>
<label for="level">Interest Level:</label>
<select name="level">
<option value="1">Interested</option>
<option value="2">Slightly Interested</option>
<option value="3">Moderately Interested</option>
<option value="4">Highly Interested</option>
<option value="5">Extremely Interested</option>
</select>
</p>
<p>
<label for="verify">1 + 3 =</label>
<input name="verify" id="verify" class="small" type="text" />
</p>
<button class="fr" type="submit" id="submit">Send</button>
</form>
</div>
I take this form and make it all pretty with jquery now this is where I am getting the most headache of my life, I spent an hr rewriting from scratch thinking I had syntax errors or something. Come to find out this little gem was the problem, its all good we all make mistakes. Here is the jquery form file, this isn't my project i not even sure if this is needed but here is the source of my problems, Ive figured them all out but 1 (hopefully !).
jQuery(document).ready(function(){
$('#contactform').submit(function(){
var action = $(this).attr('action');
$("#message").slideUp(750,function() {
$('#message').hide();
$('#submit')
.after('<img src="./img/form/ajax-loader.gif" class="loader" />')
.attr('disabled','disabled');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
txtAreaCode: $('#txtAreaCode').val(),
txtPrefix: $('#txtPrefix').val(),
txtPhone: $('#txtPhone').val(),
txtPhoneExt: $('#txtPhoneExt').val(),
zip: $('#zip').val(),
school: $('#school').val(),
title: singleValues = $("#title").val(),
color: $('#color').val(),
mascot: $('#mascot').val(),
tagline: $('#tagline').val(),
sbsize: $('#sbsize').val(),
level: $('#level').val(),
verify: $('#verify').val()
},
function(data){
document.getElementById('message').innerHTML = data;
$('#message').slideDown('slow');
$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
$('#contactform #submit').attr('disabled','');
if(data.match('success') != null) $('#contactform').slideUp('slow');
}
);
});
return false;
});
});
Alright now here is where I see the problem, im trying to get the values of the multiple choice to post to the last and final piece of code my php file that preforms the verification, Ive stripped down the fluff for debugging . But am sure that ive provided more than enough to fix. so here is the php...
<?php
if(trim($_POST['name']) == '') {
echo '<div class="error-message">Attention! You must enter your name.</div>';
exit();
} else {
$name = trim($_POST['name']);
}
if(trim($_POST['email']) == '') {
echo '<div class="error-message">Attention! You must enter your email.</div>';
exit();
} else {
$email = trim($_POST['email']);
}
if(trim($_POST['txtAreaCode']) == '') {
echo '<div class="error-message">Attention! You must enter your txtAreaCode.</div>';
exit();
} else {
$txtAreaCode = trim($_POST['txtAreaCode']);
}
if(trim($_POST['txtPrefix']) == '') {
echo '<div class="error-message">Attention! You must enter your txtPrefix.</div>';
exit();
} else {
$txtPrefix = trim($_POST['txtPrefix']);
}
if(trim($_POST['txtPhone']) == '') {
echo '<div class="error-message">Attention! You must enter your txtPhone.</div>';
exit();
} else {
$txtPhone = trim($_POST['txtPhone']);
}
if(trim($_POST['zip']) == '') {
echo '<div class="error-message">Attention! You must enter your zip.</div>';
exit();
} else {
$zip = trim($_POST['zip']);
}
if(trim($_POST['school']) == '') {
echo '<div class="error-message">Attention! You must enter your school.</div>';
exit();
} else {
$school = trim($_POST['school']);
}
if(trim($_POST['title']) != 'NULL') {
echo '<div class="error-message">Attention! You must enter your title.</div>';
exit();
} else {
$title = trim($_POST['title']);
}
if(trim($_POST['verify']) == '') {
echo '<div class="error-message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($_POST['verify']) != '4') {
echo '<div class="error-message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
echo 'working';
?>
Now im sure there are many workarounds, I would like to know what I need to do to get this all working. I cant scrap jQuery as its a must for the project, Im sure it should be a simple fix. Fingers crossed as always forgive me for going overboard, i just feel I should let you guys see what my problem is. Ive noticed that if I dont use the 2nd piece of code at all it works wonders, but like i said I need to use it....
From what I gather im clearly doing something wrong in the .post action section as it isnt posting values of the dropdown.
I do a lot of these type forms for my company's application, and there's definitely some shortcuts you can take.
Serialize the data rather than using pure ID's. That way, you don't have to reference EVERY id to submit a form.
Use a validation script on the front end, it'll cut down on the back-end validation you have to worry about reporting back on after the submit. You can't get rid of back end validation, but using a front-end validation tool allows you to quickly and effectively warn the user of a potential problem without the "cost" of a submit. I really like the inline validator script.
If you post your data via an Ajax call rather than just a post, you can use the success callback to deal with issues like validation, success modal windows, etc.
When I do have to do a back-end validation alert, I ususally just make one common alert div at the top of the form and report back to that via the success element of the Ajax call (usually sending JSON) Remember, success on Ajax means the transaction happened, you can still have errors report back through from PHP and have to deal with an error case. By only doing one alert box, I save myself a ton of work and syntax, since most all errors get dealt with on the front end and the back-end is simply redundancy.
So, here's a sample of how I'd do a form on my site:
<div id="error" style="display:none"></div> <!-- error div, hidden -->
<form id="form">
<input type="text" id="name" name="name" class="validator"> <!-- don't forget names! -->
<input type="text" id="name" name="name2" class="validator">
<button id="submit">Send</button>
</form>
<script type="text/javascript">
$('#submit').click(function() { // onclick of submit, submit form via ajax
$.ajax({
url: "url_to_submit_to.php", //url to submit to
timeout: 30000,
type: "POST",
data: $('#form).serialize(), //gets form data, sends via post to processing page
dataType: 'json', //what to do with returned data, in this case, it's json
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("An error has occurred making the request: " + errorThrown)
},
success: function(outstuff){
if (outstuff.success == 1) { //
alert('success')
} else {
$('#error').html('there is an error, do something');
//Do other stuff
}
}
});
});
</script>
Yes, you should add and id to the drop down menu, otherwise you can't call it as $("#title") in jquery
and in your jquery, it will try to submit when the page loads as you're calling it like
jQuery(document).ready(function(){
Try to change it to $(function(){
Hope it will work
It's weird isn't it, that you've nicely set IDs for all the other items in your form except for your drop downs, where you've only set names.
Forms submit the values of the controls using name, but you're referencing the value in your jquery code using id ('#').
So just try changing your dropdown declarations to have ids... like this:
<form method="post" action="test.php" name="contactform" id="contactform">
<div class="grid_6" id="register">
<center><h4>Required Information</h4></center>
<p>
<label for="name">Name:</label>
<input name="name" id="name" type="text" />
</p>
<p>
<label for="email">Your Email:</label>
<input name="email" id="email" type="text" />
</p>
<p>
<label for="trew">Contact Phone:</label>
<input name="txtAreaCode" id="txtAreaCode" style="width: 30px;" maxlength="3" value=""type="text">
<span style="color: rgb(255, 200, 46);"> - </span>
<input name="txtPrefix" id="txtPrefix" style="width: 30px;" maxlength="3" value=""type="text">
<span style="color: rgb(255, 200, 46);"> - </span>
<input name="txtPhone" id="txtPhone" style="width: 45px;" maxlength="4" value=""type="text">
<span style="color: rgb(255, 200, 46);"> - </span>
<input name="txtPhoneExt" id="txtPhoneExt" style="width: 35px;" maxlength="10" value=""type="text">
ext.
</p>
<p>
<label for="zip">Zip Code:</label>
<input name="zip" id="zip" type="text" />
</p>
<p>
<label for="school">Name of School:</label>
<input name="school" id="school" type="text" />
</p>
<p>
<label for="title">Affiliation</label>
<select name="title" id="title">
<option selected="NULL">Please Select</option>
<option value="student">Student</option>
<option value="parent">Parent</option>
<option value="teacher">Teacher</option>
<option value="booster">Booster</option>
<option value="clubpres">Club President</option>
<option value="principal">Principal</option>
<option value="ptsa">PTSA</option>
</select>
</p>
</div>
<div class="grid_6" id="contactinfo">
<center><h4>Additional Information</h4></center>
<p>
<label for="color">School Colors:</label>
<input name="color" id="color" type="text" />
</p>
<p>
<label for="mascot">Mascot:</label>
<input name="mascot" id="mascot" type="text" />
</p>
<p>
<label for="tagline">Tagline/Motto:</label>
<input name="tagline" id="tagline" type="text" />
</p>
<p>
<label for="sbsize">Approximate Student Body Size:</label>
<input name="sbsize" id="sbsize" type="text" />
</p>
<p>
<label for="level">Interest Level:</label>
<select name="level" id="level">
<option value="1">Interested</option>
<option value="2">Slightly Interested</option>
<option value="3">Moderately Interested</option>
<option value="4">Highly Interested</option>
<option value="5">Extremely Interested</option>
</select>
</p>
<p>
<label for="verify">1 + 3 =</label>
<input name="verify" id="verify" class="small" type="text" />
</p>
<button class="fr" type="submit" id="submit">Send</button>
</form>
</div>

Categories