Accessing an array passed as a name attribute in HTML from PHP? - php

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);
}
?>

Related

How to add a new item in the drop down list of a field using HTML and PHP?

I have a webpage which has a drop down of countries, and i want to add an option that if the user does not find his/her country then he can add his country name and this country name should be added to the database table in which all names of countries are stored.
<?php
include("config.php");
$query="select name from country";
$result=mysqli_query($dbconn,$query);
?>
<html>
<head>
<title>REGISTRATION</title>
</head>
<body>
<form action="registration.php" method="POST">
<br>
name : <input type="text" name="name"/>
<br>
age : <input type="number" name="age" min="10"/>
<br>
gender :
<br>
<input type="radio" name="gender" value="male" checked> Male
<br>
<input type="radio" name="gender" value="female"> Female
<br>
<input type="radio" name="gender" value="other"> Other
<br>
Email ID : <input type="email" name="email"/>
<br>
COUNTRY :
<select>
<?php while($row = mysqli_fetch_array($result)):;?>
<option><?php echo $row[name];?></option>
<?php endwhile;?>
</select>
<br>
username : <input type="text" name="username"/>
<br>
password : <input type="password" name="password1"/>
<br>
re-enter password : <input type="password" name="password2"/>
<br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
You have syntax error here use quotes
<?php while($row = mysqli_fetch_array($result)):?>
<option><?php echo $row['name'];?></option>
<?php endwhile;?>

How to display correct answer in different color than the rest?

I am working on a quiz building application that will let a user enter a quiz question with four possible answers. They must select which answer will be correct with a radio button. I then need to display the answers in a random order with the correct one being a different color than the rest. Currently I able to display the answers in a random order, but I don't know how to make the answer that was selected with a radio button a different color. Here is what it looks like...
And this is the result page...
As you can see, the results are displayed in random order, which is what I want. How can I change the color of "Albany" on the result page to show that it is the correct answer?
Here is my form code....
<form style="text-align:center" action="QuestionReview.php" method="POST">
<br>
<label class="instructions" for="question" >Enter your question here</label><br>
<textarea name="question" rows="5" cols="40"></textarea>
<br><br>
<p class="instructions">
Please provide four answers to your question and select the
correct one.
</p>
<input type="radio" name="selection" value="answerA">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerB">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerC">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerD">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="submit" value="Submit Entry">
</form>
And here is the result page.....
<?php
// Retrieve the question and answers from the HTML form
$question = $_POST['question'];
$answers = $_POST['answer'];
$selection = $_POST['selection'];
shuffle($answers);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Entry Review</title>
<style>
.instructions {
color: #696D6E;
}
</style>
</head>
<body>
<h1 class="instructions">Entry Review</h1>
<p><em>You entered the following question:</em></p>
<p><strong><?php echo $question; ?></strong></p><br>
<p><em>These are the answers you provided:</em>
<p>
<strong>
<?php
foreach($answers as $value) {
echo $value . '<br>';
}
?>
</strong>
</p>
</body>
</html>
You could wrap each answer in <div>-s and add style to them only if they were selected.
It is a good idea to change input field names in your form from selection and answer[] to answer[selected] and answer[body] respectively. This allows you to group together the body and the correctness indicator of an answer. Now you can modify your foreach in the following way:
<?php
foreach($answers as $answer){
// Print div with style, if this answer is correct
$answer['selected'] ? print '<div style="color:green;">' : print '<div>';
echo $answer['body']
echo '</div>'
}
?>
Form
<input type="radio" name="answer[0][selected]" value="true">
<input type="text" name="answer[0][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[1][selected]" value="true">
<input type="text" name="answer[1][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[2][selected]" value="true">
<input type="text" name="answer[2][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[3][selected]" value="true">
<input type="text" name="answer[3][body]" style="width:400px">
<br><br>
PHP Code
<?php
// Retrieve the question and answers from the HTML form
$question = $_POST['question'];
$answers = $_POST['answer'];
shuffle($answers);
?>
// . . .
<?php
foreach($answers as $answer){
// Print div with style, if this answer is correct
$answer['selected'] ? print '<div style="color:green;">' : print '<div>';
echo $answer['body']
echo '</div>'
}
?>
P.S
Make sure to escape the user passed data before you use it!
You can change your input fields as below:
<input type="radio" name="selection" value="answer0">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer1">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer2">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer3">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="submit" value="Submit Entry">
Then you can modify your PHP code(foreach part) as below:
<?php
foreach($answers as $key=>$value) {
if($selection == "answer$key"){
echo "<span style='color:green'>".$value . "</span><br>";
}else{
echo $value."<br>";
}
}
?>

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>

Checkbox in html

I have these group of checkboxes
upon submit .. i want to echo the chosen checkboxes in another page (pay.php)
for example: if I chose park and wash .. in the pay.php page i want to echo park and wash BUT I only get wash (the last chosen checkbox) so how to make all chosen checkboxes printed??
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service" value="park" checked>Park Only <br/>
<input type="checkbox" name="service" value="wash">Wash <br/>
<input type="checkbox" name="service" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
IN PAY.php:
<?php
//$servicetext=$_POST["service"];
// echo $servicetext;
////THE ARRAY PART////
echo "<table border='0'>
<tr>
<th> //PRINT THE ARRAY HERE </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<tr/>";
?>
since the check boxes are multiple you need to create an array of name for that same name of that input type.
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service[]" value="park" checked>Park Only <br/>
<input type="checkbox" name="service[]" value="wash">Wash <br/>
<input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
in PAY.PHP, you can access each check box value in the below formats
<?php
if(!empty($_POST['service'])) {
foreach($_POST['service'] as $service) {
echo $service;
//rest of your code
}
}
Edited
in PAY.php
<?php
if(!empty($_POST['service'])) {
$i = 0;
$selArr = array(); //i took an array that will store all these check box values
foreach($_POST['service'] as $service) {
$selArr[$i] = $service;
$i++;
}
}
Edited2
<?php
if(!empty($_POST['service'])) {
$i = 0;
$selArr = array(); //i took an array that will store all these check box values
?>
<table>
<?php
foreach($_POST['service'] as $key=>$service) {
?>
<tr><td><?php echo $key; ?></td><td><?php echo $service; ?></td></tr>
<?php
}
?>
</table>
<?php
}
I hope this helps you.
You need to make the checkbox name as an array as like this service[]
Modify your form as like below :
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service[]" value="park" checked>Park Only <br/>
<input type="checkbox" name="service[]" value="wash">Wash <br/>
<input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
Add the checkbox values into array and then access all the posted values via this array:
HTML:
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service[]" value="park" checked>Park Only <br/>
<input type="checkbox" name="service[]" value="wash">Wash <br/>
<input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
PHP:
Now all posted values are stored in $service[] array.
<?php
$servicetext=$_POST["service"];
var_dump($servicetext); // show all the posted values (content of posted array)
?>

PHP error - Inserting a user form into a database

I'm getting a strange error when I try to submit user-generated data to a database via PHP commands. When I hit the submit button below, instead of the PHP page running its' function I am presented with a display of the raw code on my browser. I have a command at the bottom of my HTML page that looks like this:
<form action="insert.php" method="post">
<input type="submit">
</form>
So that when the user hits the submit button, the PHP file insert.php (detailed below) is called to input the answers onto a database, separating each answer into it's own field.
Here is the code I'm working with:
<?php
$con=mysqli_connect("host","username","password","database");
// Check connection
if (mysqli_connect())
{
echo "Failed to connect to MySQL: " . mysqli_errno();
}
$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
Right now, the questions are in a and not a (is there a functional difference in this case?). They look like:
<form class="testAns" id="widthAns">
<input type="radio" name="answer" value="skinny">-25%
<input type="radio" name="answer" value="skinny">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="fat">+10%
<input type="radio" name="answer" value="fat">+25%
</form>
<form class="testAns" id="spaceAns">
<input type="radio" name="answer" value="small">-25%
<input type="radio" name="answer" value="small">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="wide">+10%
<input type="radio" name="answer" value="wide">+25%
</form>
<form class="testAns" id="weightAns">
<input type="radio" name="wanswer" value="light">-25%
<input type="radio" name="answer" value="light">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="heavy">+10%
<input type="radio" name="answer" value="heavy">+25%
</form>
<form method="post" action="insert.php" class="testAns" id="heightAns">
<input type="radio" name="answer" value="short">-25%
<input type="radio" name="answer" value="short">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="tall">+10%
<input type="radio" name="answer" value="tall">+25%
</form>
The important part is for the "value" associated with each button to be logged into the database. For example, if a user selects "+10%" I want be able to log the word "heavy".And then there are two text input fields:
<form id="intro">
City: <input type="text" name="answer"><br>
Why you are using this tool:<input type="text" name="answer">
</form>
So for these text fields I need the user input logged as the answer.
I see you got the PHP thing fixed.
Now you need to fill your form with data. This:
<form action="insert.php" method="post">
<input type="submit">
</form>
sends only the submit value. You need to add input fields inside that form tag, otherwise, nothing else will get sent. So, since you're sending an answer array, you should add those (adding them as text fields, as an example):
<form action="insert.php" method="post">
<input type="text" name="answer[]" />
<input type="text" name="answer[]" />
etc...
<input type="submit" />
</form>
And make sure you filter all user inputs before writing anything into the database, as otherwise my buddy Bobby Tables might come to visit you.
Make sure in your XAMPP Control Panel that Apache and MySQL are running. Then check if your input fields are inside the <form action='insert.php' method='POST'> input fields </form>
Your HTML code would look like this:
<html>
<body>
<form action='insert.php' method='POST'>
<table>
<tr><td>Width: </td><td>
<input type="radio" name="width" value="skinny">-25%
<input type="radio" name="width" value="skinny">-10%
<input type="radio" name="width" value="mid">normal
<input type="radio" name="width" value="fat">+10%
<input type="radio" name="width" value="fat">+25%
</td></tr>
<tr><td>Spacing: </td><td>
<input type="radio" name="spacing" value="small">-25%
<input type="radio" name="spacing" value="small">-10%
<input type="radio" name="spacing" value="mid">normal
<input type="radio" name="spacing" value="wide">+10%
<input type="radio" name="spacing" value="wide">+25%
</td></tr>
<tr><td>Weight: </td><td>
<input type="radio" name="weight" value="light">-25%
<input type="radio" name="weight" value="light">-10%
<input type="radio" name="weight" value="mid">normal
<input type="radio" name="weight" value="heavy">+10%
<input type="radio" name="weight" value="heavy">+25%
</td></tr>
<tr><td>Height: </td><td>
<input type="radio" name="height" value="short">-25%
<input type="radio" name="height" value="short">-10%
<input type="radio" name="height" value="mid">normal
<input type="radio" name="height" value="tall">+10%
<input type="radio" name="height" value="tall">+25%
</td></tr>
<tr><td>City: </td><td><input type="text" name="city"></td></tr>
<tr><td>Why you are using this tool: </td><td><input type="text" name="tool"></td></tr>
<tr><td></td><td><input type='submit'></td></tr>
</table>
</form>
</body>
</html>
What are you using in creating your php files? Dreamweaver? Notepad? Try this: SAVE AS your file, Save As Type: All Files and name it insert.php.
<?php
$con=mysqli_connect("localhost","YourUsername","YourPassword(if any)","NameOfYourDatabase");
// Check connection
if (mysqli_connect())
{
echo "Failed to connect to MySQL: " . mysqli_errno();
}
$width=$_POST['width'];
$spacing=$_POST['spacing'];
$weight=$_POST['weight'];
$height=$_POST['height'];
$city=mysqli_real_escape_string($con,$_POST['city']);
$tool=mysqli_real_escape_string($con,$_POST['tool']);
/* REAL ESCAPE STRING WOULD PREVENT A BIT OF SQL INJECTION */
$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$city','$tool','$width','$height','$spacing','$weight')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>

Categories