Sending html array to a PHP form - php

This is my actual code. I am trying to send the array "sku" which has copied the original array of "parent..." to php with $_post. But no matter what I try it won't send.
<script>
var sku = new Array();
for (q=1;q<parent.item_num;q++)
{
sku[q] = parent.itemlist[q].code;
}
</script>
Please help.

If the name attribute is something like name="item[]", then $_POST['item'] is an array, so you can use foreach loop to go through all items.

If your form method is post you can use <?php $_POST['item'] ?> to access the array.
You can also use foreach to loop through all of the items:
<?php
foreach($_POST['item'] as $item){
... do something with $item ...
}
?>

Arrays in HTML, e.g.
<input type="text" name="arr[]" id="arr1" />
<input type="text" name="arr[]" id="arr2" />
simply create arrays under their normal $_GET/$_POST variables, so, var_dump($_REQUEST['arr']) would yield:
arr => array(
[0] => "whatever was in arr1",
[1] => "whatever was in arr2"
)

maybe this will help :
$id = 4;
if (isset($_POST['item_'.$id]))
{
item[$id] = $_POST['item_'.$id];
} else {
item[$id] = 0;
}
PS : slugonamission way seems more appropriate

Related

submit form data from a foreach loop

I have a foreach loop with a form inside for each result like so:
foreach($this->results() as $that) {
<form>
<input type="text" name="name[]">
<input type="text" name="this[]">
</form>
}
and so on. My question is how do I each forms data. I understand you can do something like the following:
$_POST['name'][0];
$_POST['name'][1];
etc, but is their a way to get this done without knowng how many forms their will be. I mean like foreach loop the $_POST data and get each form?
Many thanks
foreach ($_POST['name'] as $val) { /* do what you want, want you want with my value */ }
$_POST['name'] is just an array. Use count or any array function you want on it then.
You can do it like this:
foreach ($_POST['name'] as $val => $value) {}
assuming these rows are being generated by a while loop, and the variable is named like this
$val = $row['val'];
And then in your form you'd have something like this:
echo '<input type="hidden" value="'.$val.'" name ="val[]" /><input type="text" name="name['.$val.'] />";
basically the name variable would be identified by the value itself being generated but also appended on a named variable, and then can be fed into your foreach.
foreach ($_POST['name'] as $key => $value) {
echo 'key: '.$key.' Value : '.$value;
}
--------------------------
output key: 0 Value : nameValue1
key: 1 Value : nameValue2
$_POST['name'] is an array, if you get multiple value with key, just get with foreach value

$_POST metacharacters?

in my $_POST, I have a variable whose name changes. the name is modify_0, but the number at the end changes depending on the button that was pressed.
Is there anyway to check what the number is for that variable in $_POST?
Say:
$_POST['modify_(check for number or any character)']
You would need to iterate over all of the keys within the $_POST variable and take a look at their format:
$post_keys = array_keys( $_POST );
foreach($post_keys as $key){
if ( strpos($key, 'modify_' ) != -1 ){
// here you know that $key contains the word modify
}
}
Besides the correct answers given above, I would recommend changing your code slightly so it's easier to work with.
Instead of having inputs with the format:
// works for all types of input
<input type="..." name="modify_1" />
<input type="..." name="modify_2" />
You should try:
<input type="..." name="modify[1]" />
<input type="..." name="modify[2]" />
This way, you can iterate through your data in the following way:
$modify = $_POST['modify'];
foreach ($modify as $key => $value) {
echo $key . " => " . $value . PHP_EOL;
}
This works especially well for multiselects and checkboxes.
Try something like this:
// First we need to see if there are any keys with names that start with
// "modify_". Note: if you have multiple values called "modify_X", this
// will take out the last one.
foreach ($_POST as $key => $value) {
if (substr($key, 0) == 'modify_') {
$action = $key;
}
}
// Proceed to do whatever you need with $action.

Use POST variables without calling them specifically

All,
I have a form that has some text inputs, checkboxes, select box and a textarea. I want to submit the form with PHP using a submit button to a page for processing.
That part is fine but what I would like to do is basically get the results into an array so I can do a foreach loop to process the results.
Basically I'm trying to create a dynamic form the submits to my backend processing script and don't want to hard code in the post values like the following:
$var1 = $_POST['var1'];
echo $var1;
$var2 = $_POST['var2'];
echo $var2;
Does anyone know how to go about doing something like this or give any recommendations?
If there're no other data in your POST but these generated elements, just do
foreach( $_POST as $key => $val ) {
// do your job
}
and process what you have. If you want to mix your generated entries with predefined you may want to put these generated in nested array:
<input ... name="generated[fieldname]" />
and then you iterate
foreach( $_POST['generated'] as $key => $val ) {
// do your job
}
Just use array notation:
<input name="vars[]" value="" />
Then you will have something like this as $_POST:
Array ('vars' => Array(
0 => 'val1'
1 => 'val2'
)
)
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val";
}
Actually, the $_POST variable is an array. you just need to extract the array values by using a simple foreach loop. that's it.
I hope this example help you.
foreach($_POST as $field_name => $val)
{
$asig = "\$".$field_name."='".addslashes($val)."';";
eval($asig);
}
After running this script all the POST values will be put in a variable with the same name than the field's name.

I need help making a PHP form parse code lighter

I have a PHP form which allows users to enter up 99 items if they do so desire. I was hoping that PHP doesn't need me to parse each individule item and it can handle doing a loop or something for when there are many items enter.
currently my php looks like this
$item1 = $_POST['Item1'] ;
$item2 = $_POST['Item2'] ;
$item3 = $_POST['Item3'] ;
$item4 = $_POST['Item4'] ;
$item5 = $_POST['Item5'] ;
// etc, etc
But I don't want 99 lines of code if only 5% of people enter more than one item in the form.
Have all the inputs named items[] (note the []). You can then access them all in an array called $_POST['items']. You can then iterate through all the values:
foreach($_POST['items'] as $item)
{
// ...
}
change the input-names like this:
<input type="text" name="items[]"/>
<input type="text" name="items[]"/>
<input type="text" name="items[]"/>
and you'll get an array:
$items = $_POST['items'] ;
foreach($items as $item){
// walk throug items and do something
}
You need to name your input elements like this:
<input type="text" name="Item[]" value="A" />
<input type="text" name="Item[]" value="B" />
<input type="text" name="Item[]" value="C" />
And then in PHP you will see this in $_POST as an
array(
0 => 'A',
1 => 'B',
2 => 'C'
)
This is a standard PHP trick, and you can use it to get any elements automatically inside the same array when reading them from $_POST and $_GET.
Another alternative :
foreach($_POST as $index => $value) {
$item[$index] = $value;
}
for ($i = 1;$i<100;$i++)
{
${"item".$i} = $_POST['Item'.$i];
}
//or you can use variables directly
//echo ($_POST['Item1']);
or you can change Item1, Item2, .... in form to Items[] and then call it like
$items = $_POST['Items'];
print_r($items);
/*
array
(
[0] => "some"
[1] => "text"
[2] => "another"
[3] => "text"
)
*/
foreach ($_POST as $key=>$value) {
if (substr($key, 0, 4)=="Item") {
$item[substr($key, 4)]=$value;
}
}

How to get PHP $_GET array?

Is it possible to have a value in $_GET as an array?
If I am trying to send a link with http://link/foo.php?id=1&id=2&id=3, and I want to use $_GET['id'] on the php side, how can that value be an array? Because right now echo $_GET['id'] is returning 3. Its the last id which is in the header link. Any suggestions?
The usual way to do this in PHP is to put id[] in your URL instead of just id:
http://link/foo.php?id[]=1&id[]=2&id[]=3
Then $_GET['id'] will be an array of those values. It's not especially pretty, but it works out of the box.
You could make id a series of comma-seperated values, like this:
index.php?id=1,2,3&name=john
Then, within your PHP code, explode it into an array:
$values = explode(",", $_GET["id"]);
print count($values) . " values passed.";
This will maintain brevity. The other (more commonly used with $_POST) method is to use array-style square-brackets:
index.php?id[]=1&id[]=2&id[]=3&name=john
But that clearly would be much more verbose.
You can specify an array in your HTML this way:
<input type="hidden" name="id[]" value="1"/>
<input type="hidden" name="id[]" value="2"/>
<input type="hidden" name="id[]" value="3"/>
This will result in this $_GET array in PHP:
array(
'id' => array(
0 => 1,
1 => 2,
2 => 3
)
)
Of course, you can use any sort of HTML input, here. The important thing is that all inputs whose values you want in the 'id' array have the name id[].
You can get them using the Query String:
$idArray = explode('&',$_SERVER["QUERY_STRING"]);
This will give you:
$idArray[0] = "id=1";
$idArray[1] = "id=2";
$idArray[2] = "id=3";
Then
foreach ($idArray as $index => $avPair)
{
list($ignore, $value) = explode("=", $avPair);
$id[$index] = $value;
}
This will give you
$id[0] = "1";
$id[1] = "2";
$id[2] = "3";
When you don't want to change the link (e.g. foo.php?id=1&id=2&id=3) you could probably do something like this (although there might be a better way...):
$id_arr = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $tmp_arr_param) {
$split_param = explode("=", $tmp_arr_param);
if ($split_param[0] == "id") {
$id_arr[] = urldecode($split_param[1]);
}
}
print_r($id_arr);
I think i know what you mean, if you want to send an array through a URL you can use serialize
for example:
$foo = array(1,2,3);
$serialized_array = serialize($foo);
$url = "http://www.foo.whatever/page.php?vars=".urlencode($serialized_array);
and on page.php
$vars = unserialize($_GET['vars']);
Put all the ids into a variable called $ids and separate them with a ",":
$ids = "1,2,3,4,5,6";
Pass them like so:
$url = "?ids={$ids}";
Process them:
$ids = explode(",", $_GET['ids']);
Yes, here's a code example with some explanation in comments:
<?php
// Fill up array with names
$sql=mysql_query("SELECT * FROM fb_registration");
while($res=mysql_fetch_array($sql))
{
$a[]=$res['username'];
//$a[]=$res['password'];
}
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
?>

Categories