$_POST and variable indexes - php

I have a project where I create hidden input values with jQuery. These represent groups and members so I have one array for the groups themselves and arrays with the members of each groups.
For example after a few creations the code in the form includes
<input type="hidden" name="groups['group_2']" value="0">
<input type="hidden" name="groups['group_1']" value="0">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="FWNGVISkjW">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="G0t9E3C0yG">
<input type="hidden" class="mem_id_holder" name="group_2[]" value="NT-JEDVCS9">
for an example of 2 groups with 2 members in group_1 and 1 member in group_2.
Now, after the submit my code to iterate through the values I use this code:
if ($groups_no && isset($_POST['groups']) && !empty($_POST['groups'])){
$groups = $_POST['groups'];
foreach ($groups as $key => $val){
if (isset($_POST[$key]) && !empty($_POST[$key])){
$group_members = $_POST[$key];
foreach ($group_members as $member_key => $member_val){
echo 'Actions to be done here!';
}
}
}
}
The problem I have is that I get the following warning and the program fails:
Notice: Undefined index: 'group_1' in C:\ ... .php on line 31
Warning: Invalid argument supplied for foreach() ...
Notice: Undefined index: 'group_2' in C:\ ... .php on line 31
Warning: Invalid argument supplied for foreach() ...
When I use $_POST['group_1'] everything works fine but since I do not know how many and which groups I will have to insert I need the variable. I have tried some different things suggested via some Google results but nothing worked.
Any ideas? Is $_POST even capable of having variables as indexes? Also if it isn't, is there any other workaround?

In your HTML, don't put quotes around the array indexes.
<input type="hidden" name="groups[group_2]" value="0">
<input type="hidden" name="groups[group_1]" value="0">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="FWNGVISkjW">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="G0t9E3C0yG">
<input type="hidden" class="mem_id_holder" name="group_2[]" value="NT-JEDVCS9">

By doing this:
<input type="hidden" class="mem_id_holder" name="group_1[]" value="FWNGVISkjW">
Instead of this:
<input type="hidden" class="mem_id_holder" name="group_1" value="FWNGVISkjW">
When you post the values you are going to get an array, so $_POST['group_1'] would not exist as a string in the first instance, it would be $_POST['group_1'][0]. So you would have to iterate through that value as well.
Hope this helps.

Related

Laravel Blade some values in 2 dimensional input array missing

i have a form that has editable answers, which are built like the following:
#foreach ($question->answers as $answer)
<input type="text" name="answers[{{$loop->index}}][text]">
<input type="hidden" name="answers[{{$loop->index}}][id]" value="{{$answer->id}}">
#endforeach
Which generates the html
<input type="text" name="answers[0][text]" value="Somevalue">
<input type="hidden" name="answers[0][id]" value="1">
<input type="text" name="answers[1][text]" value="Somevalue">
<input type="hidden" name="answers[1][id]" value="2">
In my Controller, I try to get the values from the request like this:
$requestAnswers = $request->input('answers');
foreach($requestAnswers as $key => $value) {
$id = $value['id'];
$text = $value['text'];
// Some answer handling
}
Somehow, when I try to access my answers, even when I dd($request->input('answers')) only some values show up, sometimes not having a text, sometimes not appearing at all.
Is there an error in my code or is the problem related to something else?
Thanks in advance!

for loop is not echoing the expected values from my array

I am writing some code for a module for Zen Cart. $stores_id is an array containing 3 values:
$stores_id[0]="1";
$stores_id[1]="2";
$stores_id[2]="3";
With the following code I am trying to echo a hidden input field, filled with data from the array
for ($i=0, $n=sizeof($stores_id); $i<$n; $i++)
{
echo zen_draw_hidden_field('stores_id['. $stores_id[$i]['stores_id'] .']', htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']]), ENT_COMPAT, CHARSET, TRUE));
}
the echoed result is:
<input type="hidden" value="2" name="stores_id[1]">
<input type="hidden" value="3" name="stores_id[2]">
<input type="hidden" name="stores_id[3]">
while I expected it to be:
<input type="hidden" value="1" name="stores_id[1]">
<input type="hidden" value="2" name="stores_id[2]">
<input type="hidden" value="3" name="stores_id[3]">
Can anyone tell me what I am doing wrong?
It looks like you are nesting your 2nd parameter 1 depth too far -
$stores_id[$stores_id[$i]['stores_id']]
So when $i == 0, you are getting $stores_id[1], which is 2, instead of $stores_id[0] which is 1. And when you get to $i == 2 you have $stores_id[3] which is not in the array.
So either remove the outer array -
htmlspecialchars(stripslashes($stores_id[$i]['stores_id'])
or subtract 1 from the inner array returned value
htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']-1])

handle html input elements with numeric id javascript

i have html form
<form name="updatefrm" id="updatefrm" action="" method="post">
<input type="hidden" name="98" id="98" value="" />
<input type="hidden" name="99" id="99" value="" />
<input type="hidden" name="100" id="100" value="" />
<input type="hidden" name="101" id="101" value="" />
<input type="hidden" name="102" id="102" value="" />
<input type="hidden" name="updateqty" id="updateqty" value="1" />
</form>
now i want to assign value to this elements
i m using following javascript code for element with id 98
document.updatefrm."98".value = elements[0].value;
but its giving me error in console.
can any one help me with this ?
You should use document.formname to access forms as not all browsers support this(actually I'm not sure if any does). use document.forms.formname instead. Also to access a numeric property of an object use bracket notation instead of dot notation.
document.forms['updatefrm']['98'].value = elements[0].value;
Why can't I have a numeric value as the ID of an element?
// Change your numeric id.
var myValue=document.getElementById('your_changed_id').value;
alert(myValue)
Go for simplicity..Instead of writing this complex code.why not try a simple code which does the same thing
document.getElementById('your_changed_id').value
You can also use getElementById to set the value. Beside this also check Javascript Naming Convention
document.getElementById('98').value = "YOUR VALUE";
You should not give numbers as ID include any kind of character..
Try like this:
document.forms["updatefrm"]["s98"].value = elements[0].value;
Fiddle

php specifying array values? Variable passed to each() is not an array or object

beginner PHP programmer here trying to use the mailchimp API...
I keep getting this PHP error with regard to my first line of code here.... I am posting a value from a form, as you can see, but I am not sure what is going on.
The error:
Warning: Variable passed to each() is not an array or object in xxxxxxx on line 24 (which is the first line)
while (list($key, $val) = each($_POST['MCgroup'])) {
$interest .= ($count>0 ? ", " : "");
$interest .= $val;
$count++;
}
echo $interest; //echos NOTHINGblank
$mergeVars = array(
'INTERESTS'=>$interest
);
echo $mergeVars; //echos the word ARRAY
From what I have researched, OBVIOUSLY I am not passing an actual ARRAY... but also from what I have found, my syntax all seems correct. (But I could be overlooking something).
Here is the part of the form code that is passing the values
<input type="hidden" name="MCgroup" id="MCgroup" value="My Interest One" />
<input type="hidden" name="MCgroup" id="MCgroup" value="My Interest Two" />
or I COULD change it to
<input type="hidden" name="MCgroup" id="MCgroup" value="My Interest One, My Interest Two" />
I just am not sure how to make THAT into an array.
Help please! Thanks!
You can set your <input> to have an array name by suffixing [], do something like this:
<input type="hidden" name="MCgroup[]" id="MCgroup" value="My Interest One" />
<input type="hidden" name="MCgroup[]" id="MCgroup" value="My Interest Two" />
then check it by:
print_r($_POST['MCgroup']);
Where I assume you set form method to POST.
Change the HTML name to an array:
<input type="hidden" name="MCgroup[]" id="MCgroup1" value="My Interest One" />
<input type="hidden" name="MCgroup[]" id="MCgroup2" value="My Interest Two" />
<input type="hidden" name="MCgroup[]" id="MCgroup3" value="And another sparkling interest" />
Also only use id if it is going to be unique.

reading two form elements with same name

<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same

Categories