Im trying to store data in this table:
newspaper_id
article_id
comment (string)
This is how my HTML looks like:
<div class="newspaper-options">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="3"> Active
</div>
<div class="newspaper-options">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="1"> Active
</div>
This is the result im getting back when i only fill in the second newspaper options and check it:
"comment" => array:2 [▼
0 => ""
1 => "option 2"
]
"article_id" => array:1 [▼
0 => "1"
]
The problem im having is how can i group the input values so i can store it inside my table. The value of my checkbox is that of the article_id. and the text field is used to fill the comment inside the database.
Any help with this would be greatly appreciated!
The information that is missing to correctly put that info into the database is the mapping of the array position to the article_id.
E.g. put
<input name="article[]" type="hidden" value="...">
in each <div class="newspaper-options">
Like so
<div class="newspaper-options">
<input name="article[]" type="hidden" value="3">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="3"> Active
</div>
<div class="newspaper-options">
<input name="article[]" type="hidden" value="1">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="1"> Active
</div>
Then in your Controller you can loop through Input::get('article'), check the active checkbox and add the comment.
E.g.
foreach(Input::get('article') as $pos => $article_id){
if(in_array($article_id, Input::get('article_id')){
add $article_id and Input::get('comment')[$pos] to DB
}
}
Apart from that you should get a better name for your article_id field, like "active" and change "data" to "comment".
If you could add the input fields with keys in the first place you could handle them more easily. I guess it is possible to do
<input class="form-control" name="data[3]" type="text">
<input type="checkbox" class="article-option" name="article_id[3]" value="3"> Active
Related
i wanna use laravel collective for my input form,im using this to input datas for pivot table in laravel eloquent many-to-many,and i wanna use input data using checkbox element(hoby),the problem is idont know why we type at the first parameter of laravelCollective as string and way we have to type as array,anyone can explain me?in theory,thanks in advance for your help
<div class="form-check">
#if (count($list_hobi)>0)
#foreach ($list_hobi as $key => $value)
<div class="checkbox">
{{Form::checkbox('hobi[]',$key,null)}}
<label>{{$value}}</label>
</div>
#endforeach
#endif
</div>
Take for example these checkboxes:
<input type="checkbox" name="food" value="apple" /> 1
<input type="checkbox" name="food" value="pear" /> 2
<input type="checkbox" name="food" value="banana" /> 3
All three have the same name. When I check all three and submit the form and see what has been submitted with dd($request->input()), the output is:
"food" => "banana"
It appears only the last input with the same name is saved, even though I selected all three.
When I instead use food[]:
<input type="checkbox" name="food[]" value="apple" /> 1
<input type="checkbox" name="food[]" value="pear" /> 2
<input type="checkbox" name="food[]" value="banana" /> 3
the output is:
"food" => array:3 [▼
0 => "apple"
1 => "pear"
2 => "banana"
]
I have a group of checkboxes in a from and I need to get values with php. Note that div class="group" can also be duplicated onruntime (with jquery) so I may end with multiple div class="group" of checkboxes.
Is there a way to set checkboxes name so form data gets grouped together for each "group", or whatever is easier to handles afterwards? There is also a problem of checkboxes that were not checked, so they wont be present in a post data.
I tried using names like this but I dont like the results.
<form>
<div class="group">
<input type="checkbox" name="ev[][time]">
<input type="checkbox" name="ev[][space]">
<input type="checkbox" name="ev[][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[][time]">
<input type="checkbox" name="ev[][space]">
<input type="checkbox" name="ev[][money]">
</div>
</form>
When defining checkbox groups its much better to be verbose with input names and avoid using the [] notation.
A un-ticked checkbox is not sent in post data, and your selections will become misaligned.
<form>
<div class="group">
<input type="checkbox" name="ev[0][time]">
<input type="checkbox" name="ev[0][space]">
<input type="checkbox" name="ev[0][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[1][time]">
<input type="checkbox" name="ev[1][space]">
<input type="checkbox" name="ev[1][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[2][time]">
<input type="checkbox" name="ev[2][space]">
<input type="checkbox" name="ev[2][money]">
</div>
</form>
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);
I am writing an sql embedded in php database.
I am facing a problem when dealing with user inputs. I have a php file where the user can select the type of search. For example, select all data entries or limit the search to a given id num, name, last name...
This the code where the user selects type of search
<select name="type" >
<option value="fm"> Faculty Member Search </option>
<tr><td>List all Faculty Members<input type="checkbox" name="list[]" value="listAll" >
<div>
<label class="textClick"> List By Id<input type="checkbox" name="list[]" value="listId" > </label>
<input class="text" type="text" size = "8" placeholder="Insert Id" name="listById" > </div>
<div>
<label class="textClick">List By First Name <input type="checkbox" name="list[]" value="listName" ></label>
<input class="text" type="text" placeholder="Insert Name" name="list" >
</div>
<div>
<label class="textClick">List By Last Name<input type="checkbox" name="list[]" value="listFname" ></label>
<input class="text" type="text" placeholder="Insert Last Name" name="listLast" >
</div>
</select>
<input type="submit" value="Get Info">
Now in the other php file (where search is actually done) i can't seem to get the values of the user's inputs.. This the part where my code goes wrong
if($_POST["type"]=="fm"){
Print "LIST IS" . $_POST["list"];
if($_POST['list']=="listAll")
{
?>
<h1> Listing ALL Faculty Members </h1>";
<?php
this code gave me the following output:
LIST IS
I tried both $_POST['list'] and $_POST["list"] but my code just doesn't seem to pass the if condition
$_POST['list'] is going to be an array of the submitted values. You will need to loop through the array to get the values or access them like any other array value (i.e. $_POST['list'][0]) to get the first element.
Do var_dump($_POST['list']) and you'll see the array and its values.
What you probably want is only one value and should change the checkboxes to radio buttons and remove the array syntax from the input field names:
<label class="textClick">List By First Name <input type="rado" name="list" value="listName" ></label>
I saw something on this site:
Handling array of HTML Form Elements in JavaScript and PHP
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
It said to put the array in the name property and how to get the input collection's value. For example, name="education[]"
But as I know, an HTML input element is array-ready by name.
On the client-side (GetElementsByName) or server-side ($_POST in PHP or Request.Form in ASP.NET).
For example: name="education", so what is the different with or
without the []?
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]" you will get an array when you do this:
$educationValues = $_POST['education']; // Returns an array
print_r($educationValues); // Shows you all the values in the array
So for example:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
Will give you all entered values inside of the $_POST['education'] array.
In JavaScript, it is more efficient to get the element by id...
document.getElementById("education1");
The id doesn't have to match the name:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1">
</p>
If you have checkboxes, you can pass an array of checked values.
<input type="checkbox" name="fruits[]" value="orange"/>
<input type="checkbox" name="fruits[]" value="apple"/>
<input type="checkbox" name="fruits[]" value="banana"/>
Also multiple select dropdowns
<select name="fruits[]" multiple>
<option>apple</option>
<option>orange</option>
<option>pear</option>
</select>
It's different.
If you post this form:
<input type="text" name="education[]" value="1">
<input type="text" name="education[]" value="2">
<input type="text" name="education[]" value="3">
you will get an array in PHP. In this example you will get $_POST['education'] = [1, 2, 3].
If you post this form without [],
<input type="text" name="education" value="1">
<input type="text" name="education" value="2">
<input type="text" name="education" value="3">
you will get the last value. Here you will get $_POST['education'] = 3.