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>
Related
I have made a simple HTML form from which I want to obtain all the ticked checkboxes' names via PHP. However when I try to do this with the superglobal $_POST[] variable nothing is assigned to the corresponding variable. I would like to ask how can easily accomplish this?
Thanks.
<!DOCTYPE html>
<html>
<head>
<title>Questionaire</title>
</head>
<body>
<p>Please, enter your information:</p>
City: <input type="text" name="">
Month: <input type="text" name="">
Year: <input type="text" name="">
<p>Please, choose the kinds of weather you experienced from the list below.</p>
<br>
<input type="checkbox" name="weather[]" id="o1"><label for="o1">Sunshine</label>
<br>
<input type="checkbox" name="weather[]" id="o2"><label for="o2">Clouds</label>
<br>
<input type="checkbox" name="weather[]" id="o3"><label for="o3">Rain</label>
<br>
<input type="checkbox" name="weather[]" id="o4"><label for="o4">Hail</label>
<br>
<input type="checkbox" name="weather[]" id="o5"><label for="o5">Sleet</label>
<br>
<input type="checkbox" name="weather[]" id="o6"><label for="o6">Snow</label>
<br>
<input type="checkbox" name="weather[]" id="o7"><label for="o7">Wind</label>
<br>
<input type="checkbox" name="weather[]" id="o8"><label for="o8">Cold</label>
<br>
<input type="checkbox" name="weather[]" id="o9"><label for="o9">Heat</label>
<br>
<br>
<button>Go</button>
</body>
</html>
<?php
$ar[] = $_POST[weather[]];
// I want to threat the $ar variable as an array, but I cannot
print_r($ar);
//. . . .
?>
You have to correct the code as well as add form field value. Here is the short example form.
<form action="" method="post">
<input type="checkbox" name="weather[]" id="o1" value="Sunshine"><label for="o1">Sunshine</label>
<br>
<input type="checkbox" name="weather[]" id="o2" value="Clouds"><label for="o2">Clouds</label>
<br>
<input type="checkbox" name="weather[]" id="o3" value="Rain"><label for="o3">Rain</label>
<button>Go</button>
</form>
<?php
if (isset($_POST['weather'])) {
$ar = $_POST['weather'];
print_r($ar);
}
?>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am trying to create an event registration form for my event (my first time using PHP), but am having trouble with the input text for my HTML/PHP form. I can submit the form and it sends me an email with the names of the inputs, but not the actual entered text/information.
Here is my HTML code:
<div class="WMF-form">
<form method="post" action="myform.php">
Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
Family Count: <textarea name="familycount" rows="5" cols="40"></textarea>
Family Names:: <textarea name="familynames" rows="5" cols="40"></textarea>
Volunteer:
<input type="radio" name="volunteer" value="female">Female
<input type="radio" name="volunteer" value="male">Male
<input type="radio" name="volunteer" value="other">Other
Fee Options:
<input type="radio" name="feeoptions" value="female">Female
<input type="radio" name="feeoptions" value="male">Male
<input type="radio" name="feeoptions" value="other">Other
Lodging:
<input type="radio" name="lodging" value="female">Female
<input type="radio" name="lodging" value="male">Male
<input type="radio" name="lodging" value="other">Other
<br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</section>
Here is my PHP code:
PHP code
PHP code
PHP $_POST values are case sensitive. Your HTML has
<input type="text" name="name">
But you access it in the PHP file as
$Name = $_POST["Name"];
Make sure the cases match.
Try
$Name = $_POST["name"];
Capitalize your names in your form. the post variables have a capital Name Email FamilyNames etc and the form doesnt
Good morning,
I have a submit form,s code and it works perfectly. But when i add more inputs or radio buttons it stops working. I'm a front-end developer and i'm not very good at back-end development. Can anybody please help me to sort it out.
Thanks
<html>
<head></head>
<body>
<form action="invia.php" method="POST">
Nome: <input type="text" name="nome">
<br><br>
Email: <input type="email" name="email">
<br><br>
Messaggio:
<br>
<textarea name="message" rows="10" cols="50" ></textarea>
<br><br>
<input type="submit" value="Invia">
</form>
</body>
</html>
<?php
$client = $_POST['nome'];
$email = $_POST['email'];
$message = $_POST['message'];
mail("someone#gmail.com","Contact from the site",$message,"From: $email\r\n");
?>
If we say we have this:
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
Then only the last value will be taken because names overlap each other.
I've got some data posted to a php file, and I need to save/append that data separated by commas to a text file. That's all fine and dandy and should be straightforwards, except for when I check the file I see that every comma has been written twice: once where it needs to be, and then again after the appended data but as a group of commas.
Here's what I've got:
<form action="signup-submit.php" method="post">
<fieldset>
<legend>New User Signup:</legend>
<label>Name:</label>
<input type="text" name="name" size="16" autofocus required/> <br/>
<label>Gender:</label>
<label><input type="radio" name="gender" value="m" /> Male</label>
<label><input type="radio" name="gender" value="f" checked /> Female</label> <br />
<label>Age: <input type="text" name="age" size="6" maxlength="2" required></label><br/>
<label>Personality Type: <input type="text" name="pType" maxlength="4" size="6" required/></label><br/>
<label>
Favorite OS:
<select name="os">
<option value="windows">Windows</option>
<option value="mac">Mac OS X</option>
<option value="linux">Linux</option>
</select>
</label><br/>
<label>
Seeking age:
<input name="min" type="text" size="6" maxlength="2" placeholder="min" required/>
to
<input name="max" type="text" size="6" maxlength="2" placeholder="max" required/>
</label><br/>
<input type="submit" value="Sign up"/>
</fieldset>
</form>
^that code posts the "user data" to signup-submit.php where it is stored into variables by the same var names.
$name = $_POST["name"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$pType = $_POST["pType"];
$os = $_POST["os"];
$min = $_POST["min"];
$max = $_POST["max"];
$c = chr(44);
$s = "$name$c$age$c$gender$c$pType$c$os$c$min$c$max";
file_put_contents("single.txt", $s, FILE_APPEND);
?>
and the text file will unfailingly duplicate the commas as such:
Byron,21,m,INTP,windows,18,23,,,,,,
the variables are simply data posted to this file from a previous page. I've also tried every which way under the sun to save the data. In fact, if I write only text directly, I get no problems. When I used the csv function I got just commas and no data despite or so I believed formatting it correctly.
That's probably because sometime all your variables are empty, and you FILE_APPEND just commas.
To solve this do:
if (!empty($name) && !empty($age) && ...) {
$s = "$name,$age,$gender,$pType,$os,$min,$max";
file_put_contents("single.txt", $s, FILE_APPEND);
}
If these values are user input always double check them. PHP offers standard functions to sanitize: Sanitize Filters, also you will need input validation with custom rules (for example to check if a variable is within a certain range, but this is a different question).
First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.
I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
That is then submitted to a php file with this code:
if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
$newsletter is then inserted into a database field.
The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.
Any help would be greatly appreciated! Thanks!
Here's the full form minus the option list for the sake of brevity
<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form all fields are required, thanks!</legend>
<label for=firstName accesskey=F><span class="required">*</span>First Name</label>
<input name="firstName" type="text" id="firstName" size="30" value="" />
<br />
<label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
<input name="lastName" type="text" id="lastName" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span>Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=city accesskey=C><span class="required">*</span>City</label>
<input name="city" type="text" id="city" size="30" value="" />
<br />
<label for=state accesskey=S><span class="required">*</span>State</label>
<select name="state" type="text" id="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
<br />
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
<br />
<p><span class="required">*</span> Are you human?</p>
<label for=verify accesskey=V> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?
This has just dawned on me.
If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.
Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.
Edit 2:
I've tested this to death. Change your code to:
if (isset($_POST['newsletter'])){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".