How to post array[][] in codeigniter - php

in codeigniter I have to post an two-dimensional array array[$index][$index] to some variable.
How do I post the value from the array on $val[$index][$index];?
I have to try with some code like this, but didnt work corectly :
$val[$x][$y] = $this->input->post('criteria'[$i][$j]);
Any thoughts on how I should try to get this working?
Thanks!

In an html Form you can add a field with name having double square brackets. Like:
<input name="myVal[][]" />
Then post this into a form to php.
In you php file write:
echo '<pre>';print_r($_POST);'</pre>';
You will see posted fields there. You can get this posted data in codeigniter using:
$this->input->post('myVal');

Related

html multiple select unable to get the values with php

I know this question was asked before but the answers don't work for me.
My Code:
I have this html select box:
<select name="usergroups[]" id="usergroups" multiple="multiple" style="width:210px;">
<?php
$result = dbselect("SELECT * FROM Groups");
foreach($result as $row){
echo "<option value='".$row['GroupName']."'>".$row['GroupName']."</option>";
}
?>
</select>
The options for the select box come from a database and it worked fine. It shows what should be displayed.
Then i want the selected options to work with them in my php code.
$usGroups = $_POST['usergroups[]'];
$usgr = implode(",",$usGroups);
I convert the array to a string to save the values in my database.
My problem:
It seems that with $_POST I don't get the values and I really don't know why?
Is it because I fill in the options in a dynamic way?
Or is a mistake in the code which I'm not able to see?
It seems so simple but I don't get it.
Thank's for any help! (and sorry for my bad english...)
after submit use:
var_dump($_POST)
die();
or:
var_dump($_REQUEST)
die();
you can as well check like that:
if(isset($_POST['usergroups']) {
//do something
}
to see if the value is in those super globals.
If it's not there then you're not passing it right from the form.
Remember that you need to set method of FORM to POST for this to work ($_REQUEST will have values of either post or get).
Depending on which data you're sending from your form you might need to add enctype="multipart/form-data", i.e.
<form action="/someaction" method="post" enctype="multipart/form-data">
You're using the name of POST variable wrong, you should do it like this:
$usGroups = $_POST['usergroups'];
$usgr = implode(",",$usGroups);
Don't use array brackets to fetch the variable in PHP
Hope this helps!

Form variable is blank

I'm trying to pass some variable's values through the form. But in the URL I get: coniugazione= and the value passed is blank.
this is my code:
<form action="risultato.php?coniugazione=<?php $_POST['coniugazione'] ?>" " method="post">
NB: the coniugazione variable is correct valorized from the user. The problem is only the passage to url. What's wrong in my code?
You have to ouput the variable actually in your form with the echo-keyword, like: <?php echo $_POST['coniugazione'] ?>
Notice: This only works if you want to pass the variable via the GET-method. If you want to use POST, you have to use e.g. a hidden field as #alexander correctly suggested in his comment.

how do I grab the text inside of a form_label?

I currently have aform_labelin my view as follows:
echo form_label('cat', 'cat');
I want to be able to set a variable in the controller to equal 'cat' as is displayed in the label.
In my controller I have tried using post and get to access the contents, but got done reason it only works got form_input and not form_label.
Thank you!
An easy way is to pass the same text into a hidden element in the form so you can grab it from the post object.

Why doesnt my <form> work with REQUEST but works with POST

Hello I have the following form that collects data entered and later I output it. It works just fine when I use POST but when I use REQUEST like the teacher said to do, the echo $word comes back empty. Any ideas guys? please?
<Form name ="form1" Method ="REQUEST" Action ="">
<Input Type = "text" Value ="<?php echo $word ?>" Name ="word">
<Input Type = "Submit" Name = "Submit1" Value = "Submit">
<?php
if (isset($_POST['Submit1'])) {
$word = $_POST['word'];
$book = $_POST['book'];
}
?>
There is no method called REQUEST on a Form. It should be either GET or POST
Maybe your teacher is confused with the $_REQUEST in PHP.
I think you are looking for GET, not REQUEST.
GET will include the contents of the form submission in the URL itself, so it's suitable for things that should be able to be bookmarked, like search form submissions.
Here's more: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
Not sure why your teacher asked you this, but "REQUEST" is not a standard HTTP method so I don't think there's any shortcut in PHP to retrieve the data. I found that even using PATCH sometime causes problems.
What you could try is to read the raw data directly using:
file_get_contents("php://input")
There is NO method named REQUEST. You can use only two methods : POST and GET.
If you are using POST as method, you can get the values using only POST OR REQUEST.
If you are using GET as method, you can get the values using only GET OR REQUEST.
For more information please refer to this page: http://www.w3schools.com/tags/ref_httpmethods.asp

PHP: How to POST value of an input which contains [] in the name attribute?

I swear i couldn't find a simple working solution for this.
On a form i have inputs that have names containing "[]" and i cant change the names of the inputs because they are part of a script.
I want to php POST the values of those inputs at the next page, after the form submit.
Example of input
<input type="text" name="CustomFields[13]" id="CustomFields_13_1" value="">
Anyone knows how to accomplish it?
I want to do it using PHP only
If the name is CustomFields[13], then you can access it at $_POST['CustomFields']['13'].
You "cannot" POST something with PHP. It's always the client that POSTs to the server. PHP is running on server side.
I recommend that you use sessions and save there the values that you need to have available in next pages.
This is how you set a session:
session_start();
$_SESSION['CustomField'] = "test";
And this is how you get it:
session_start();
echo $_SESSION['CustomField']; //Should display "test"

Categories