How do you pass a multi dimensional array from view to controller? - php

It was easy passing data from controller to view. Also, to pass data from view to controller you need form tags. But, how do you pass an array from an input form? any idea guys? here's what the array looks like:
$test = array
(
array($employee_id[0],$name[0],$days_worked[0],$overtime_hours[0]),
array($employee_id[1],$name[1],$days_worked[1],$overtime_hours[1]),
array($employee_id[2],$name[2],$days_worked[2],$overtime_hours[2])
);
and from my html view i got here an input form:
<input name="test" type="text" class="form-control" id="test" value="<?php echo $test;?>">
and when i got to my model to test if it gotten the data inside the array:
$this->test = $_POST['test'];
echo $test = $_POST['test'];
all i got was a String "Array". I can't access what's inside the array. I need help.

Your questions is unclear.. you want to pass the array to you view, or to the HTML output into the INPUT element?
These are 2 different things, as one is only passing an array internally in you application (on the server), and the second is passing it into you form data, displaying it in the browser, and then sending the form to the server and getting the data there.
For the first - i see no problem there, as passing variables is no issue
The second - it is not possible by default to pass a multi-dimensional array into a form input. Becasue the input has only 1 dimension.
You could pass the data with some conversion function e.g. as a JSON string
value="<?php echo json_encode($test);?>"
and then load it like this:
$this->test = json_decode($_POST['test']);
But that does not make sense in the frontend I guess, as the user would not understant what data is presented in the input field.
To to it in a logical way, I would divide the data into groups, and then display accordingly in more input fields e.g.
<input name="test[0][employee_id]" type="text" class="form-control" id="test" value="<?php echo $test[0][0];?>">
<input name="test[0][employee_name]" type="text" class="form-control" id="test" value="<?php echo $test[0][1];?>">
<input name="test[0][employee_days_worked]" type="text" class="form-control" id="test" value="<?php echo $test[0][2]?>">
<input name="test[0][employee_overtime]" type="text" class="form-control" id="test" value="<?php echo $test[0][3];?>">
but do it more nicely. This way you can create sth which will be sent as a multi-dim. array to the php script.
as suggested in another answer here, you can see the structure with var_dump($test)

Array values cant be passed in form data directly.
You should use json_encode.
In your view file
$encoded_text = echo json_encode($test);
<input name="test" type="text" class="form-control" id="test" value="<?php echo $encoded_text ;?>">
Now in your model just decode this
$test = json_decode($test, $assoc = TRUE);

If you use serialize() on your controller and unserialize() on the view you should be able to access it in the same way. I believe that's what you're asking.

Related

How to make array from several input type text using ID of input text?

I'm new in HTML and PHP world. I try to make a multi dimensional array with HTML input type text like this (i use CodeIgniter Framework) :
<form method="POST">
<input type="text" id="myTF[0][0]" values="A"/>
<input type="text" id="myTF[0][1]" values="B"/>
<input type="text" id="myTF[0][2]" values="C"/>
<input type="text" id="myTF[1][0]" values="D"/>
</form>
And i hope when i submit that form, i can use PHP code like this :
foreach($myTF as $index_first => $val){
foreach($val as $index_second => $val2){
echo($val2);
}
}
Is the code above is correct? Or other way exist to do that more better?
Taking the code I would print out the $_POST data and look at the array structure. Doing it off the top of my head I plugged the data into https://3v4l.org and got this:
https://3v4l.org/hD8Bv
I may have the structure incorrect but it is a handy tool to figure out if a piece of logic will work or not.

Form that completes Values in PHP

I have a normal form in a PHP page, I send data to the php page from another for using POST. The PHP page runs some scripts to update data to SQL but on that page I have a second form that needs to be completed with data from the initial form prior to updating the SQL.
$recipient_nr = $_REQUEST['recipient_nr'];
Here I draw the info from the first form
Now I want to use this in a new form on the current PHP page how do I state this in the new form
<input type="text" name="recipient_nr" id="recipient_nr" value=".$recipient_nr.">
This is what I am attempting but it is not working I know I have too many "'" xxx"'" in the lines but not sure how to remidy this
Do you generate the new form in PHP? If so, where is the code where you do that?
If this is some kind of ...?> <input type="..."...> <?php ... page generation then you'll need to echo that $recipient_nr into the PHP-generated response:
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?php echo $recipient_nr; ?>">
<?php
...
Or, if you have short echos turned on,
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?= $recipient_nr ?>">
<?php
...
use this:
<input type="text" name="recipient_nr" id="recipient_nr" value="<?php echo $recipient_nr; ?>">
Something like this:
$recipient_nr = array_key_exists('recipient_nr', $_REQUEST)
? (string) $_REQUEST['recipient_nr']
: 'default value or empty string';
And output it:
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?=$recipient_nr?>">
But if you want store this data for/between other pages you can use $_SESSION global array for save it.

hidden array returns OK but text array is empty html to PHP

Form:
<form method="POST" action="edit_work.php">
<input type="hidden" name="wid[]" size="1" value="<?php echo "$wid1" ?>" >
<input type="text" name="course[]" size="15" value="<?php echo "$course1" ?>" >
PHP:
extract($_POST);
for($i=0;$i<$count;$i++) {
echo $wid[$i];
echo $course[$i];
}
gives the wid values OK but not the text entered for the course names.
I have been through all forums for 2 days now. Any help? Thanks.
If you want your PHP to retrieve your data from the form, can't you name your text input "course", then get it inside your PHP with $_POST['course'] ?
What is your $count ?
Using brackets with your name attribute inside your input tag may be dangerous.
If you're using a list of inputs maybe you can define a text format like name="course#" where # is your index and then access it form your $_POST variable using $_POST['course'.$index]
You don't need to extract($_POST) in that case.

Reading multiple textfield values using array in php

Can you please help me in this context.
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
</form>
I am using this code in a loop and I need to get all the values in another page.
How can I get these value as array in another page.
you should be able to use $_POST array in the sample.php page
PHP's $_POST Array, taken from the docs:
An associative array of variables passed to the current script via the HTTP POST method.
first, add s submit button to your form, it should look like this:
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
<input type="submit">
</form>
and in sample.php:
<?php
print_r($_POST);
?>
This is the most simple example,
once you get the hang of it, I recommend you read the docs on topics such as:
htmlspecialchars
htmlspecialchars — Convert special characters to HTML entities
You can access it as array
$_POST['first'][0] // 0 will be index for single
to list all just loop
foreach($_POST['first'] as $first) {
...
}
Your values will end up in $_POST, and because you're using the [] syntax, you'll get an array. So, to get your first array, do:
$first = $_POST['first'];
You could then iterate over it like so:
foreach ($first as $item) {
...
}
I think this will help you.
$first=$_POST['first'];
$second=$_POST['second'];
foreach($first as $key=>$first){
echo 'Your first value'.$first.'Your second value'.$second[$key];
}

PHP send array to another PHP file

This may be a stupid question but I'm lost here. I need to send array with some data in it to another PHP file using POST variable. This is my form:
<form action="test.php" method="post">
<label name="html[]" hidden><?php echo $array; ?></label>
<input type="submit" value="submit">
</form>
And this is test.php
<?php
$html = $_POST['html'];
for($i = 1; $i<=9; $i++){
echo $html[$i];
}
?>
So this is what I tried, but it's not displaying anything. please help
You need to create a number of input elements with the same name, each of which will have one array item as its value:
<?php foreach ($array as $item) : ?>
<input type="hidden" name="html[]" value="<?= htmlspecialchars($item); ?>" />
<?php endforeach; ?>
Important points to keep in mind:
$item must always be a scalar value (string, integer, etc). You cannot pass in arrays piecemeal with this technique.
Never forget that since you are injecting variables into HTML output you must escape and/or sanitize them properly. In this case this is done with htmlspecialchars, which must know about your output encoding to work correctly in general (look up its third parameter).
There is also an alternative approach that can be used to pass arrays piecemeal through serialization:
<input type="hidden" name="html"
value="<?= htmlspecialchars(serialize($array)); ?>" />
And you would then unserialize it on the receiving end:
$html = unserialize($_POST['html']);
I 'm mostly including this option for completeness, as in practice session variables are a much better way of passing complex state between requests.
Is it necessary to put the data of the array in a hidden field? You can store the array in $_SESSION and access it. Btw, I think you have a problem, labels can be submitted, in that case you must put the data into an input field with type="hidden".

Categories