Inserting multiple rows from multiple question radio form in PHP - php

I have a form to determine the user's personality. So each question (there are 25) has 4 possible answers to choose from:
<form method="post">
<div>
<label for="name"></label>
<input name="name" id="name" placeholder="Name">
</div>
<div>
<label for="surname"></label>
<input name="surname" id="surname" placeholder="Surname">
</div>
<br>
<div>
<label for="q1">Question 1</label><br>
<input type="radio" name="q1" id="q1" value="S"> q1c1 <br>
<input type="radio" name="q1" id="q1" value="D"> q1c2 <br>
<input type="radio" name="q1" id="q1" value="K"> q1c3 <br>
<input type="radio" name="q1" id="q1" value="I"> q1c4 <br>
</div>
<br>
<div>
<label for="q2">Question 2</label><br>
<input type="radio" name="q2" id="q2" value="D"> q2c1 <br>
<input type="radio" name="q2" id="q2" value="K"> q2c2 <br>
<input type="radio" name="q2" id="q2" value="I"> q2c3 <br>
<input type="radio" name="q2" id="q2" value="S"> q2c4 <br>
</div>
<br>
<div>
<label for="q3">Question 3</label><br>
<input type="radio" name="q3" id="q3" value="S"> q3c1 <br>
<input type="radio" name="q3" id="q3" value="I"> q3c2 <br>
<input type="radio" name="q3" id="q3" value="D"> q3c3 <br>
<input type="radio" name="q3" id="q3" value="K"> q3c4 <br>
</div>
<br><br>
<button>Submit</button>
</form>
I need to insert the name, surname, and the selection of the question in one line. The next row in the database should also contain the name, surname, and then the next question's selection for all 25 questions.
My table users in the database only has 3 columns namely: name, surname, q
I am very new to PHP and tried some previous questions but none worked for my particular scenario.

I assume you are working fully with PHP, so no JavaScript or Ajax involved.
first of all you need to specify where your request should send:
<form method="post" action="https://example.org/target.php">
in target.php, you need to read data out of $_POST. In your case, it must be something like $_POST['name'].
From there on, you can do whatever you want with the data.
Please consider this answer as a minimal question as you need to do further checks, such as security checks, prevent SQL injections and overall data validation.

Related

how to add multiple entries having the same text input name and radio buttons (html,php)

<form action="confirm.php" method="post" name="">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
<br>
<button type="submit" class="">Submit</button>
</form>
having problem with the radio buttons.
And on the confirm page I have used foreach loop. How do i also get the values for "f_status" ?
See first of all its an interesting question but unfortunately, the fact is HTML can't understand the field without different names if they are in same form.
so the only way to achieve your goal is to put all of them in three different form tags and then u can name all of the same i.e. f_hobby[]
Also, you need to add a single button to submit all three of them. To achieve this u can use onsubmit() or onclick() function.
<form action="confirm.php" method="post" name="" id="form1">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form2">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form3">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<button type="submit" class="" onclick="submitForms()">Submit</button>
<script>
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
document.getElementById("form3").submit();
alert("gajab");
}
</script>
I have given the forms an id to submit it using a single button, u can also use class instead. I am sure this will solve your problem.

count html radio buttons value with php

I have a html form with grouped radio button. This form contains questions with yes or no options
<form action="results.php" method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
I know i can count the number of radio buttons with name q
$count_cbox = count($_POST['q'])
But is it possible that when the user makes a choice i count the radio button value that ="yes" or "no".
Please change your radio button names slightly. Otherwise the grouping doesn't work:
<form method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[0]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[0]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[1]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[1]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[2]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[2]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
The PHP code:
for($i = 0; $i < count($_POST['q']); ++$i) {
if($_POST['q'][$i] == 'yes') {
++$yes;
}
}
Now $yes contains the number of radio buttons with the value yes. In this case 0-3. Please pay attention to the fact that if no radio button is selected, it will return NULL, not 0.

Post radio button value in array to php file

I'm trying to make a question paper. My code is like this:
<form action="check.php">
<span id="ques_id_12">Question 1</span>
<input type="radio" id="option_a_12" name="ques12[]" value="1">
<label for="option_a_12">Answer a</label>
<input type="radio" id="option_b_12" name="ques12[]" value="2">
<label for="option_b_12">Answer b</label>
<input type="radio" id="option_c_12" name="ques12[]" value="3">
<label for="option_c_12">Answer c</label>
<input type="radio" id="option_d_12" name="ques12[]" value="4">
<label for="option_d_12">Answer d</label>
<span id="ques_id_13">Question 2</span>
<input type="radio" id="option_a_13" name="ques13[]" value="1">
<label for="option_a_13">Answer a</label>
<input type="radio" id="option_b_13" name="ques13[]" value="2">
<label for="option_22">Answer b</label>
<input type="radio" id="option_c_13" name="ques13[]" value="3">
<label for="option_c_13">Answer c</label>
<input type="radio" id="option_d_13" name="ques13[]" value="4">
<label for="option_d_13">Answer d</label>
<span id="ques_id_14">Question 3</span>
<input type="radio" id="option_a_14" name="ques14[]" value="1">
<label for="option_a_14">Answer a</label>
<input type="radio" id="option_b_14" name="ques14[]" value="2">
<label for="option_b_14">Answer b</label>
<input type="radio" id="option_c_14" name="ques14[]" value="3">
<label for="option_c_14">Answer c</label>
<input type="radio" id="option_d_14" name="ques14[]" value="4">
<label for="option_d_14">Answer d</label>
<input type="submit">
</form>
The question is dynamic and above comes from a php and mysql code. I want to post the data to a php file where I can calculate the number of questions correct, number of questions wrong and number of questions attempted.
I'm confused how should I check the correct answers. $_POST[ans_a] would return ans_a from all questions. How can I distinguish all the questions and separately put them into array. Or should I change my way of arranging my dynamic code ?
Note what #RajdeepPaul said. Your answers are being exposed and posted along with the answers, so if you want any kind of security don't do this, and check for the answers in PHP..
$answers = ['ques12' => 4, 'ques13' => 3, 'ques14' => 2];
Now loop over all the answers. The keys in $answers are the same as in $_POST.
First of all, never put the correct answer as hidden input field in the HTML form. Anybody can see the source code and get the correct option for all the questions. Keep the correct answers(options) corresponding to particular questions in a table like this:
+-------------+----------------+
| question_id | correct_option |
+-------------+----------------+
| | |
Or, use an array like this:
$correct_options = array('ques_id_12' => 4, 'ques_id_13' => 3, 'ques_id_14' => 2);
So that later you could compare the correct answer with the user inputted option value.
Now comes to your question, you need to change the name attribute value of your checkbox elements like this,
name="ques_id_12" for all the checkbox options for question ID 12, name="ques_id_13" for all the checkbox options for question ID 13 etc.
Also, since you're sending bulk of data to the server with your form, you should use POST method instead of GET
So your form should be like this:
<form action="check.php" method="POST">
<span id="ques_id_12">Question 1</span>
<input type="radio" id="option_a_12" name="ques_id_12" value="1">
<label for="option_a_12">Answer a</label>
<input type="radio" id="option_b_12" name="ques_id_12" value="2">
<label for="option_b_12">Answer b</label>
<input type="radio" id="option_c_12" name="ques_id_12" value="3">
<label for="option_c_12">Answer c</label>
<input type="radio" id="option_d_12" name="ques_id_12" value="4">
<label for="option_d_12">Answer d</label>
<span id="ques_id_13">Question 2</span>
<input type="radio" id="option_a_13" name="ques_id_13" value="1">
<label for="option_a_13">Answer a</label>
<input type="radio" id="option_b_13" name="ques_id_13" value="2">
<label for="option_22">Answer b</label>
<input type="radio" id="option_c_13" name="ques_id_13" value="3">
<label for="option_c_13">Answer c</label>
<input type="radio" id="option_d_13" name="ques_id_13" value="4">
<label for="option_d_13">Answer d</label>
<span id="ques_id_14">Question 3</span>
<input type="radio" id="option_a_14" name="ques_id_14" value="1">
<label for="option_a_14">Answer a</label>
<input type="radio" id="option_b_14" name="ques_id_14" value="2">
<label for="option_b_14">Answer b</label>
<input type="radio" id="option_c_14" name="ques_id_14" value="3">
<label for="option_c_14">Answer c</label>
<input type="radio" id="option_d_14" name="ques_id_14" value="4">
<label for="option_d_14">Answer d</label>
<input type="submit">
</form>
And after form submission, you can use simple foreach loop to compare the user inputted option value with the correct option value for each question like this:
foreach($_POST as $question_id => $user_inputted_option){
// compare the user inputted option value with the correct option value
}
Update(1):
How I would know which questions have been answered and which have not ? And how would I pass the question id's ?
Let's say your question id array is like this:
$ques_ids = array('ques_id_12', 'ques_id_13', 'ques_id_14', 'ques_id_15', 'ques_id_16');
Then after form submission, you need to process your form like this:
$array_keys = array_keys($_POST); // all the user attempted question ids
foreach($ques_ids as $q_id){
if(in_array($q_id, $array_keys)){
// attempted question
$user_inputted_option_value = $_POST[$q_id];
// now compare the user inputted option value with the correct option value
}else{
// unattempted question
}
}
Sidenote: If you want to see the complete array structure, do var_dump($_POST);

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

email signup multiple lists

I have the following code, I want the user to be able to sign up to multiple lists at the same time. It is currently only signing up emails to one list randomly, even if I check all the lists. Is it possible to do? Maybe some sort of php echo?
<form action="" method="post">
<input name="accName" type="hidden" value="companyname">
<input name="listName" type="hidden" value="">
<input name="fullEmailValidationInd" type="hidden" value="Y">
<input name="doubleOptin" type="hidden" value="false">
<input name="successUrl" type="hidden" value="">
<input name="errorUrl" type="hidden" value="">
Email Address <input class="border" name="email" size='50' type="text" value="">
First Name <input class="border" name="First_Name" size='50' type="text" value="">
Last Name <input class="border" name="Last_Name" size='50' type="text" value="">
<label><input name="listName" type="checkbox" value="list1"></label>
<label><input name="listName" type="checkbox" value="list2"></label>
<label><input name="listName" type="checkbox" value="list3"></label>
<label><input name="listName" type="checkbox" value="list4"></label>
<label><input name="listName" type="checkbox" value="list5"></label>
<input type="submit" value="OK">
</form>
Use array for the checkboxes:
<input type="checkbox" NAME="listName[]" VALUE="list1" />
Or use different names...
You will need to grab this using, php side, a loop:
foreach ($_POST['listName'] as $selected)
Just notice that if none are selected this will fail, thus check if the array exists before:
if (isset($_POST['listName'])
{
foreach ($_POST['listName'] as selected)
{
DO YOUR STUFF
}
}

Categories