Laravel handling form with variable number of elements - php

I need to get a variable number of nursing factors (input text) dynamically created on a form. How the controller can handle this?

Use HTML input arrays:
<input type="text" name="fields['name']" ... >
<input type="text" name="fields['age']" ... >
<input type="text" name="fields['address']" ... >
And in your controller you will grab an array via:
$arrayOfFields = Input::get('fields');

Related

How to write in 2 input at the same time

Do you have any idea how to write text in two inputs at the same time, what is written in input 1 appears in input 2 but modified for example:
hello do you have any idea how to write text in two inputs at the same time, what is written in input 1 appears in input 2 but modified for example:
Input 1: this is a text
input 2: this-is-a-text
I try to use str_replace () but I can't do it in real time
<?php
$texto = $_POST['title'];
$urlcambiado = str_replace(" ", "-", $texto);
?>
<input type="text" name="title" class="form-control" placeholder="Ejemplo: sword-art-online">
<input type="text" name="url_code" class="form-control" placeholder="Ejemplo: sword-art-online">
This will do what you want using pure javascript:
function URLChange(titlestr) {
var url=titlestr.replace(/ /g,"-");
document.getElementsByName("url_code")[0].value=url;
}
<input type="text" name="title" class="form-control" placeholder="Ejemplo: sword-art-online" onkeyup='URLChange(this.value);'>
<input type="text" name="url_code" class="form-control" placeholder="Ejemplo: sword-art-online">
This has to be done on client-side using JavaScript and goes a little bit like this:
Reference the two input fields
Both have it's name attribute set to title and url_code respectively. To get a reference to it we can use the getElementsByName() method which returns a HTMLCollection - an array. Since there's just one element for each name we can append a [0] to get the first element in the array.
var firstInput=document.getElementsByName("title")[0];
var secondInput=document.getElementsByName("url_code")[0];
Attach an input event listener
To find out if the user has typed anything into the first input we need to use this listener which invokes a callback function we can then use to get the actual text.
firstInput.addEventListener("input",process);
Modify the text inside the second input
Inside the callback function we can retrieve the text from the first input, use a regular expression to replace whitespaces by a minus sign and assign the text to the second text field.
function process(e) {
secondInput.value = e.target.value.replace(/\s/g, '-');
}
Here's a complete example:
var firstInput = document.getElementsByName("title")[0];
var secondInput = document.getElementsByName("url_code")[0];
function process(e) {
secondInput.value = e.target.value.replace(/\s/g, '-');
}
firstInput.addEventListener("input", process);
<input type="text" name="title" class="form-control" placeholder="Ejemplo: sword-art-online">
<input type="text" name="url_code" class="form-control" placeholder="Ejemplo: sword-art-online">

Laravel 5 - Multiple form inputs with the same name but keeping the order

Making a blogging system using L5 and my current set up is all ready except for the saving of the blog posts.
I have 2 buttons. One creates a textarea input and the other creates a file upload interface.
Essentially, after creating a blog post I am left with the structure like so:
<form>
<textarea name="text-update">foo</textarea>
<textarea name="text-update">foo</textarea>
<textarea name="text-update">foo</textarea>
<textarea name="text-update">foo</textarea>
<input type="hidden" value="an image url"/>
<input type="hidden" value="an image url"/>
<textarea name="text-update">foo</textarea>
</form>
Ideally I want to be able to go:
public function store()
{
foreach (INPUTS AS INPUT) {
add new row to database with the content and also the type of input.
}
}
The aim is that instead of having a single blog I instead have blog sections which will belong to a blog post.
If this isn't possible then Ill just have to increment the names of the inputs and figure something out.
Edit: When you add an element to the DOM you can define the array key with an id to preserve the array order.
You can make the inputs an array by adding [] at the end of the name:
<form>
<textarea name="text-update[1]">foo</textarea>
<textarea name="text-update[2]">foo</textarea>
<textarea name="text-update[3]">foo</textarea>
<textarea name="text-update[4]">foo</textarea>
<input type="hidden" name="image[1]" value="an image url"/>
<input type="hidden" name="image[2]" value="an image url"/>
<textarea name="text-update[5]">foo</textarea>
</form>
This will put all the values in an array that you can iterate over
foreach (Request::get('text-update') as $update) {
//add new row to database with the content and also the type of input.
}
foreach (Request::get('image') as $update) {
//add new row to database with the content and also the type of input.
}
Set your fields up like this:
<textarea name="text-update[]">foo</textarea>
using the brackets will take all the text fields and group them into an array that you can then iterate through. You will also need to do the same with hidden fields. Make sure you use the [] in the name like so:
<input type="hidden" name="somename[]" value="an image url"/>

Loop through 3 arrays at once and save values in the database

I have three inputs type text in an HTML page and a button which if clicked duplicate each text box (Javascript) making them 6.
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
<button>Add more</button>
Which generate same inputs again:
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
A piece of code in Cakephp I have been trying:
$data = $this->request->data;
foreach($data['category'] as $index => $value){
$this->ModelName->save($value);
}
Trying to get two rows inserted at once with quantity, category and amount as columns. But it is not inserting and not giving any error.
Is there a way I can achieve this?
Thanks.
I'm not sure how your model works in cakephp, but you should be able to get a complete grouping of data like:
foreach($data['category'] as $index => $value){
$category = $value
$quantity = $data['quantity'][$index];
$amount = $data['amount'][$index];
// use the above 3 variables however you need to to persist the model
//$this->ModelName->save($value);
}
On a side note, you may want to consider reordering your html inputs to be like:
<input type="text" name="item[0][category]">
<input type="text" name="item[0][quantity]">
<input type="text" name="item[0][amount]">
And then maintain the next index, incrementing the numeric index of item for each additional group
This will allow you to iterate like:
foreach($data['item'] as $index => $group){
//$group['category'];
//$group['quantity'];
//$group['amount'];
}

get custom attribute value in php

I am try to get the value of the input field with a custom attribute I have created using PHP. This is my code:
<form action="uploadform.php" method="post">
<input type="text" name="email" id="email" mynewattribute="myemail">
<input type="submit" name="submit">
</form>
//uploadform.php
<?php
//I know $name = $_POST['email']; will give me the value but I would like to get the value of the input field with "mynewattribute" and not name. Is it possible?
?>
The web browser doesn't know what to do with your custom attribute, so will simply ignore it. The only data sent when you submit the form is the values of "successful" elements. So your custom data will never be sent, and can never be read by the receiving script.
The best place to put such data is into hidden input fields. One possibility is to use names with square brackets in, which PHP automatically converts into arrays. e.g.
<input type="text" name="email[value]">
<input type="hidden" name="email[magic]" value="true">
Populates an array like this:
$_POST['email']['value'] = '';
$_POST['email']['magic'] = 'true';

Iterating through set $_POST variables by type?

I have user inputs as follows:
<form action="special.php" method="post">
<input name="first1"> <input name="last1"> <input name="age1">
<input name="first2"> <input name="last2"> <input name="age2">
<input name="first3"> <input name="last3"> <input name="age3">
<input name="first4"> <input name="last4"> <input name="age4">
<input name="first5"> <input name="last5"> <input name="age5">
<input name="first6"> <input name="last6"> <input name="age6">
...
N
</form>
The amount of user inputs in the form is determined by the user; meaning, the user can add 5,10,20 additional lines to the code above, creating new input elements (following the pattern above) as they fit.
My question is, once the form gets submitted, what is an easy way to iterate and print out all the SET POST variables?
Something like:
for($i=0; $i < $numPostVars; $i++){
if(isset($_POST['first".$i."'])){
//echo all first names post variables that are set
}
}
// do the same from last names & age in separate loops
I think the trick is to name your variables slightly different, and take advantage of PHP's feature which will unpack them as arrays for you. Just use the syntax: first[1]. Then in PHP, $_POST['first']['1'] is where you will find it. You can then iterate all your "first" inputs with
foreach($_POST['first'] as $first_input) {
// ...
}
Also keep in mind that browsers may not send the field if it is empty when the user submits.
Here is what the inputs should look like in HTML:
<input name="first[1]"> <input name="last[1]"> <input name="age[1]">
As noted by user #DaveRandom, consider also a more hierarchical structure (think "rows" like from your db):
<input name="people[1][first]"> <input name="people[1][last]"> <input name="people[1][age]">
Inputs can be treated as arrays with a syntax very similar to that used in PHP:
<input name="name[1]" value="value 1">
<input name="name[2]" value="value 2">
This would result in a $_POST['name'] that looks like this:
array(
1 => "value 1",
2 => "value 2"
);
This principle can be expanded to incorporate multi-dimensional and associative arrays. So if you were to name your inputs like this:
<input name="rows[1][first]"> <input name="rows[1][last]"> <input name="rows[1][age]">
<input name="rows[2][first]"> <input name="rows[2][last]"> <input name="rows[2][age]">
...you would be able to easily iterate over $_POST['rows'] with a foreach construct. The data structure will be very similar to a set of database results.
foreach ($_POST['rows'] as $row) {
// do stuff with $row['first'], $row['last'] and $row['age'] here
}
A couple of things to note:
Unlike PHP, associative array keys in HTML do not require quotes, and using them will produce a result you may not expect. It will work, but not in the way you might think. You still need to use quotes in PHP though.
As far as I am aware, this syntax is not a W3C standard. PHP, however, always handles it as expected.

Categories