I have a form that is a list of check boxes. Next to each check box is a text field that goes with the check box. Below is an image of what I am talking about
The text in red is the name attribute. My question is based off of whatever the user check marks, how do I match the paired text field without doing a bunch of if statements?
I know how to just post the check boxes and just post the text fields..separately. I guess I am stuck at how to pair the results. This is what I am using to $_POST the check boxes
if (isset($_POST['family_medical_history']))
{
$_SESSION['patient_form']['family_medical_history'] = array();
foreach ($_POST['family_medical_history'] as $key => $value)
{
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST['family_medical_history'][$key];
}
}
else
{
$_SESSION['patient_form']['family_medical_history'] = "none";
}
Example HTML form
<div class="form-group col-sm-4">
<label><input type="checkbox" name="family_medical_history[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes">
</div>
You can pair them by naming the inputs similarly for each checkbox/relationship pair to produce a two-member array for each item.
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]">
</div>
In the PHP, they will become pairs with the 0 element equal to the value of the checkbox input, and the 1 element equal to the text input. You can create an array of conditions and loop through each of the named inputs. You'll have to change your if statement to something else, for example the submit button:
$conditions = array("rel_blindness" => "Blindness", "rel_cataracts" => "Cataracts", "rel_crossed_eyes" => "Crossed Eyes", "rel_glaucoma" => "Glaucoma");
if (isset($_POST['submit'])){
foreach ($conditions as $key => $value){
if ($_POST[$key][0] == $value){//if the checkbox is checked
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST[$key][1];
}
}
}
Related
i have a form that i increase the id of checkbox through a foreach with unknown size now when i submit i want to receive all of the sent data here is my code :
<form action="/somecontroller" method="post">
<div id="checkboxes" class="col-lg-2 text-center">
<input type="checkbox" name="rGroup" name="d{{$index}}" value="{{verta($pdates->date)->format('Y/m/d')}}" id="d{{$index}}"/>
<label class="whatever mt-3" for="d{{$index}}"> {{verta($pdates->date)->format('Y/m/d')}}
<hr>
{{$pdates->price}}</label>
</div>
</form>
and here is the controller :
$recived_data = $request->d{{$index}}; here i want to get all the checkboxes send by user
so how can i recive the data that user send which i dont know the number of checkboxes
You can use array syntax of the input element, and receive the array with selected items in the controller, so change your input checkbox to this:
<input type="checkbox" name="d[{{$index}}]" value="{{verta($pdates->date)->format('Y/m/d')}}" id="d{{$index}}"/>
Then in your controller:
$request->input('d'); // returns an array of indexes of all the selected checkboxes.
I need to save multiple checkbox to single field in the database.
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="1">Raíz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="3">tronco
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="4">corteza
</label>
</div>
Controller:
$ficha_tecnica = new Ficha_Tecnica();
$options = $request->get('expresion_vegetal_id');
$ficha_tecnica->expresion_vegetal_id = $options;
$ficha_tecnica->save();
this is trying to save, the values in [""], I need only save the numbers
insert into `fichas_tecnicas` (`expresion_vegetal_id`) values (["1","3","4"])
When I try to save, show the next message
1366 Incorrect integer value: '["1","4"]' for column 'expresion_vegetal_id'
You'r cannot add that because
You can try to loop the expresion_vegetal_id still in array format.
I see in your question is you need to add that in string format.
You must loop that array first and save one by one or you can used created
I give you the sample using loop.
You code will looks like this
$ficha_tecnica = new Ficha_Tecnica();
foreach ($$request->expresion_vegetal_id as $expresion_vegetal_id) {
$ficha_tecnica->fresh();
$ficha_tecnica->expresion_vegetal_id = $expresion_vegetal_id;
$ficha_tecnica->save();
}
i hope you can find the other same way....
I am creating a webpage with from-controls (2 in this exemple).
My code is the following :
<body>
<div class="option_choice_global">
Select your options
<div class="col-xs-2">
<label> Application </label>
<select class="form-control" id="application">
<?php
$applications = get_apps();
foreach ($applications as $key => $value) { echo '<option value='.$key.'>'.$value.'</option>'; }
?>
</select>
</div>
<div class="col-xs-1">
<label> Version </label>
<select class="form-control" id="version">
<?php
$versions = get_versions();
foreach ($versions as $key => $value) { echo '<option value='.$key.'>'.$value.'</option>'; }
?>
</select>
</div>
<div class="col-xs-12">
<button type="submit" class="btn btn-default" onclick="fonction_submit_graph(this)"> <b> Submit </b> </button>
</div>
</div>
</body>
But I would like the second one (the version) to appear only when a value on the first controller (the application) is chosen. And my function get_versions() will then depend on the selected application : get_versions(application_number).
How can I do to show the second controller only when a value on the first one is selected ? And to get the selected value ?
Thank you!
If I understand you right, you want to show the second select dropdown after a value is chosen from the first.
This can easily be done with JavaScript and CSS:
In the second select tag, add these attributes: style="{display: none;}". This will hide the dropdown. Also give it id="select2" for easy identification.
In the first select tag, add this attribute: onchange=showHiddenSelect(); This causes a function to be called when the value changes ie an option is selected. Also give it an id="select1".
Somewhere in your document, define the showHiddenSelect function like this:
function showHiddenSelect() {
document.getElementById("select2").style.display="block";
}
If you also wish to get the selected value, you'd just add this in that function: var select1value = document.getElementById("select1").value; and do what you want with it.
If you need the selected option from select1 to be sent to a backend and then used to calculate the options for select2, then your best bet would be to use AJAX to send and receive data from a script and then populate the select with JavaScript.
I have a large form where 3 arrays are sent to the controller from the form: product_code | quantity | cost.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
The output from the request on all three arrays is here: PasteBin
My problem is I can not figure out how best to loop through each of these three arrays so that I can insert into MySQL the values in the correct order. I have tried both a foreach, a nested foreach and a for loop and I have not managed yet to get all three values inserting onto a single row.
I don't think the HTML is very relevant, but here is a sample anyway:
<div class="well form-group ">
<div class="col-sm-2 col-md-2">
<label for="nails_staples">Nails & Staples:</label>
</div>
<div class="col-sm-4 col-md-4">
<select class="form-control product_id" name="product_code[10]">
<option selected="selected" value="">None</option>
<option value="8769">1 1/4 Coil Nails - box | $26.00</option>
<option value="6678">2" Hot Dipped Shake Nails | $135.00</option>
</select>
</div>
<div class="col-sm-1 col-md-1">
<label for="nails_req">Quantity:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control quantity" name="quantity[10]" type="text">
</div>
<div class="col-sm-1 col-md-1">
<label for="cost">Cost:</label>
</div>
<div class="col-sm-2 col-md-2">
<input class="form-control cost" readonly="readonly" name="cost[]" type="text">
</div>
</div>
There are a few issues with this approach. Ideally you'd have separate calls to a controller for each entity you want to change, using AJAX to make each individual call as the user changes the entity. This solves the issue of attributes not being strongly tied to the entity ID (in your case, product_code).
If you're willing to make the assumption that all of the arrays match up, you should at least check that they are the same length.
Obviously I don't know all the specifics of your Model or exactly what you're looking for, but based on your paste bin, this should work.
$id = $request->get('product_code'); // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity'); // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost'); // GRAB COST DATA FROM POST
$count_ids = count($id);
if(count($qty) != $count_ids || count($cost) != $count_ids) throw new Exception("Bad Request Input Array lengths");
for($i = 0; $i < $count_ids; $i++){
if (empty($id[$i])) continue; // skip all the blank ones
$newProduct = new Product();
$newProduct->code = $id[$i];
$newProduct->qty = $qty[$i];
$newProduct->cost = $cost[$i];
$newProduct->save();
}
I have a button on a page that when a user pushes it, it creates another "Contact" field on the page. The Contact field allows them to add a new contact to their profile. Also, they can click the button as many times as they want, and it will create that many "Contact" fields.
The problem though is that I am having a hard time figuring how many "Contact" fileds have been added. Here is some HTML that is generated when the button is clicked:
<div class="item">
<label for="in-1v">First Name <span>*</span></label>
<div class="text">
<input type="text" id="in-1-0" name="member[0][fname]" value="" />
</div>
</div>
<div class="item">
<label for="in-2-0">Last Name <span>*</span></label>
<div class="text">
<input type="text" id="in-2-0" name="member[0][lname]" value="" />
</div>
</div>
Each time the button is clicked, name="member[0][lname]" will become name="member[1][lname]" and will continue to increment each time the button is clicked. As stated earlier, the user can do this as many times as they want on the page.
I am using PHP to loop through the multidimensional array:
$array = $_POST['member'] ;
foreach($array as $array_element) {
$fname = $array_element['fname'];
$lname = $array_element['lname'];
}
How can I use PHP to determine how many fileds have been added so I can loop through them?
Any help is greatly appreciated!
You can simply get a count like so:
$count = count($_POST['member']);
You could also then modify your loop to look like this:
// First check to see if member is set and is a valid array
if (!empty($_POST['member']) && is_array($_POST['member'])) {
$count = count($_POST['member']);
for ($i = 0; $i < $count; $i++) {
$fname = $_POST['member'][$i]['fname'];
$lname = $_POST['member'][$i]['lname'];
}
}