form_multiselect gives only one option - php

Sorry, but this is kind a homework question...
I have to make a webpage with codeigniter and I have to use multiple select component.
So my code.
Part in *view.php file:
<br>Keywords:<br>
<?php echo form_multiselect('keywords', $keys); ?>
Also there is submit button, and after it pressed I take POST data. For debugging tried:
var_dump($_POST['keywords']);
This always shows, that there is only one option selected, for example, string(1) "2"
Can someone advice how should I modify my code to get all selected items.

Please try:
<?php echo form_multiselect('keywords[]', $keys); ?>
A multiselect form field must have a name with array notation.
You would expect codeignitors function to accommodate this, but it doesnt (well not when i last used CI in 2010)

From the Codeigniter documentation:
form_multiselect()
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected. The parameter usage is identical to using form_dropdown() above, except of course that the name of the field will need to use POST array syntax, e.g. foo[].
The last sentence states you need to use POST array syntax, so the name of the select should be, in your case
name="keywords[]"

Related

Use a dynamic variable to access a specific value within a PHP array

I am trying to dynamically set the first dimension of a multidimensional array using a value from an ACF input field in Wordpress with the ID of fund_data_id. The array is stored within the variable $fund_array and when manually drilling down to the specific data point I want to access within the array, it works just fine. An example of manually drilling down successfully is: $fund_array[1][5].
Ideally, I'd like to be able to set the first dimension of the array (which is currently [1]) to read the input from the ACF field fund_data_id in Wordpress, which I set to a number value of 1. However, the first dimension of the array isn't recognizing the ACF input from Wordpress. I've tried:
<?php
echo $fund_array[the_field('fund_data_id')][5] ;
?>
and...
<?php
$num = the_field('fund_data_id');
echo $fund_array[$num][5] ;
?>
The second dimension will stay constant, so there's no need to change that one, just the first dimension via an input from an ACF field.
Thanks in advance to everyone for any help or suggestions!
the_field() does an echo, which you do not want.
You need to use get_field('fund_data_id')
So this should work:
<?php
echo $fund_array[get_field('fund_data_id')][5] ;
?>

CakePHP form - remove month field name

I'm setting up a Stripe payment form, so I need to remove the names of my month and year fields so they aren't sent to my server. The following code though, still gives the field a name of '[month]' and if the text of the array's name variable were 'xyz', the field would be named 'xyz[month]'. How can I remove the entirety of the field's name?
echo $this->Form->month('expiration_month', array('name' => '', 'data-stripe' => 'exp_month', 'default' => 'January'));
According to the documentation, the name of the <select> element is derived from the first function argument ("expiration_month" in your example.) If you take a look at the code, you can see the value "month" is hard-coded.
The only way around this is to manually build your own <select> element, or just ignore the value when it comes to your server. But why make users fill out a form element that isn't going to be processed by your server?
So, the quick and dirty way around this is to find the select field with js and overwrite the name attribute.

posting 2 values to array from single checkbox

I have a situation where I'd like to post two values to a search query string array from a single checkbox. Because of the design, I can't just add another checkbox.
The checkbox in question looks like so:
<input name="wpp_search[property_type][]" value="rental_home" type="checkbox" id="choice_c"/>
<label for="choice_c">For Rent</label>
what I now get in the query string is ...
{url}?wpp_search[property_type][0]=rental_home
but what I need is to tie two values to that one check so I end up with this:
{url}?wpp_search[property_type][0]=rental_home&wpp_search[property_type][1]=building
Any simple solutions? There is only one other checkbox that already feeds that array, so I could force this one to be
{url}?wpp_search[property_type][0]=rental_home&wpp_search[property_type][2]=building
You can seperate values with for example "|" like this value="value1|value2". Then later you can use the function explode: $p = explode("|", $value); and you get 2 values.
Generally it's not possible to send one value as two values.
One way I could imagine is that you reconfigure the ini-setting of arg_separator.input:
arg_separator.input = ";&"
This will allow you to use ; as well to separate values which then would allow you to inject the second value per that value:
<input name="wpp_search[property_type][0]"
value="rental_home;wpp_search[property_type][1]=building"
...
/>
If you make use of the value ; in other form values you naturally need to properly escape them to make this compatible.
Another way is that you insert a hidden field with that value and if your checkbox is checked, you change the name of it to the right name. If unchecked, you change the name of it back to something not right:
<input type="hidden" name="---wpp_search[property_type][1]" value="=building" />
Take the javascript reference of your choice and do the needed DOM manipulation on click of the other checkbox to remove the three --- at the beginning of the name.

Html: For Select multiple, only one value is submitted but .val() returns array of two values

When rendering the page, the val() of a multiple select is set with a single value. For example, $("#my_select_box").val(1);
Then the user selects an additional value in the multiple select box.
When the form is submitted, only the newly selected value is submitted and not the previously set one. Whereas while debugging in Firefox, the .val() function returns an array of two values (both the previous one and the newly selected one). Why does this happen?
If you are programming in PHP, the problem lies there. When using multiselect inputs, PHP requires you to have an empty pair of brackets after input name. For example:
<select name="select[]" multiple="multiple">
rather than just
<select name="select" multiple="multiple">
If you can't or won't do this (perhaps you don't have access to the underlying HTML), then you can alternatively capture the POST data like this:
$data_from_post = file_get_contents("php://input");
var_dump($data_from_post);
And you'll see a string like this that you can use:
select=one&select=two&select=three&button=submit
Well, without code we can't be sure. But when you want multiple params to be passed in you have to give a name value of that input the [ ] brackets. Ex. "name[]" so in order for the server to know that there are in fact two values.
Hard to tell what to do without the code.
However, if you want both the option that you marked selected and another that was selected, you could access them as follows:
option[selected="selected"] for the one you set
and
option:selected for the one chosen by the user
So something like this...
$('#submit').click(function(){
var s = $('#my_select_box option[selected="selected"]').val();
var t = $('#my_select_box option:selected').val();
alert("Set: " + s + ", Chosen: " + t);
});
Example: http://jsfiddle.net/jasongennaro/pZesG/1/

What's the correct way to access a field in an array element with php?

I'm getting a few rows from the DB with an ajax call in PHP and I save the result to an array with javascript.
Then I make some changes in the data and wish to update the DB.
So I use another ajax call for that but I can't manage to access the fields inside the rows correctly.
When I try this I get nothing:
echo $bArray[$i].branchId;
When I try this:
echo json_encode($bArray[$i].branchId);
I get ArraybranchId instead of the field value.
What's the correct way to access the field with php?
Try either for array:
$bArray[$i]['branchId']
or for object:
$bArray[$i]->branchId
depending which type $bArray[$i] is (array or object). You have not written in your question, so I showed both ways.
I take it branchId is the name of the field, and you want the value for that field?
If so, it's:
echo $bArray['branchId']; or
echo $bArray[$i]['branchId']
Edit: Also, you'll need to make sure you're using mysql_fetch_assoc not mysql_fetch_array!

Categories