PHP: Not receiving some POST variables? - php

I have a form, it passes the following values:
image_title,
image_description,
image_file
I wanted to present this form multiple times, so I did the following:
image_title_1,
image_description_1,
image_file_1
image_title_2,
image_description_2,
image_file_2
Multiple times, so I have the fields 1 - 10. I submit the form and print out the contents of the POST array, the only problem is any "image_title_#" after "image_title_1" doesn't exist in the array: but everything else does.
So the array would look something like:
image_title_1 -> "title!"
image_description_1 -> "description!"
image_file_1 -> file
image_description_2 -> "description!"
image_file_2 -> file
image_description_3 -> "description!"
image_file_3 -> file
So to work out what it is I swapped description and title with each other, however title still doesn't display for after 1. I'm not doing ANY processing, I'm literally just printing out the $_POST array before even touching it. This makes no sense, what could be causing it?
To clarify: The problem is "image_title_#" (example: image_title_3) doesn't get passed except for image_title_1, even if I re-arrange the order. I do no processing before outputting.
Edit, the html source is just:
<form method="post" action="">
<input type="text" name="image_title_1"></input>
<input type="text" name="image_description_1"></input>
<input type="text" name="image_file_1"></input>
<input type="text" name="image_title_2"></input>
<input type="text" name="image_description_2"></input>
<input type="text" name="image_file_2"></input>
<input type="text" name="image_title_3"></input>
<input type="text" name="image_description_3"></input>
<input type="text" name="image_file_3"></input>
<input type="submit" name="submit" value="submit"></input>
</form>

A better solution would be converting them to array, try this instead:
<form method="post" action="">
<input type="text" name="image_title[]"></input>
<input type="text" name="image_description[]"></input>
<input type="text" name="image_file[]"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
Now, in your PHP script, you can get their array like this:
print_r($_POST['image_title']);
print_r($_POST['image_description']);
print_r($_POST['image_file']);
.
Suffixing field name with [] converts it to array. The other good thing here is that it has shortened your code too.
Once you have the array, you can loop through them using foreach:
foreach($_POST['image_title'] as $key => $value)
{
// process them any way you want
}

The code works. I just cut and paste your form and do a test submit
Array
(
[image_title_1] => 1
[image_description_1] => 2
[image_file_1] => 3
[image_title_2] => 4
[image_description_2] => 5
[image_file_2] => 6
[image_title_3] => 7
[image_description_3] => 8
[image_file_3] => 9
[submit] => submit
)

Related

Getting a form using php

I am currently making a website where they can create their own pages and the areas where they put their information are in their own boxes. Each box is a row on my server. I want them to be able to have as many of these boxes as they want. So before I have changed the website the form looked like this
<form method="GET" action="createpage.php">
<input type="text" name="titleOne">
<input type="text" name="paraOne">
<input type="text" name="titleTwo">
<input type="text" name="paraTwo">
<button type="submit" name="button">button</button>
</form>
It also has the ability to be an image but that doesn't make a difference to what I am hoping you will help me figure out. I now want to change the form to something like
<form method="POST" action="createpage.php">
<input type="text" name="boxTitle">
<input type="text" name="boxPara">
<input type="text" name="boxTitle">
<input type="text" name="boxPara">
<button type="button"><button>
//the button above will be clicked to bring another set of inputs as seen in the two above using javascript
<button type="submit" name="button">button</button>
</form>
is there any way I can grab the whole form in one array and then split it up every 2 sets of information.
I am hoping the result would come out as something like this
$form = $_POST['(form)']
//$form would then look like this
$form = boxTitle => (aTitle), boxPara => (paragraph), boxTitle => (nextTitle), boxPara (nextParahraph)
it would continue like that for each extra time they clicked the button. Is this possible or am I going to have to rethink my design.
Thanks for any help

How to POST an associative array in PHP

I have the following form:
<form action="options.php" method="post">
<input type="text" name="deptid" id="deptid" />
<input type="text" name="deptname" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
EDIT
Is it possible to pass the two values into one associative array BEFORE submission ?
I would like to pass it in this form:
array('deptid'=>'deptname')
I need this because I avoid to modify the script of the destination php file(options.php)
Thanks.
Here is a method using pure HTML that get's you nearly exactly where you want to be, and only uses HTML:
<form action="options.php" method="post">
<input type="text" name="options[deptid]" id="deptid" />
<input type="text" name="options[deptname]" id="deptname" />
<input type="submit" name="submit" id="submit" value="save" />
</form>
Which would give you in PHP:
$post_options = array(
'options' => array(
'deptid '=> '[that input element value]',
'deptname' => '[that input element value]'
)
);
Which you can then (including sanitizing) access such as this:
$post_options = array('options');
if (is_numeric($post_options['deptid'] && $post_options['deptid'] > 0) {
// Do whatever
}
if (is_string($post_options['deptname'] && strlen($post_options['deptname'] > 2)) {
// Do whatever
}
EDIT
Or... You want to reference the deptid in the input name attribute and use it to modify the row for a department name? Which seems to indicate something like this:
<?php
$deptid = 1;
$deptname = 'Department of Silly Walks';
?><input type="hidden" name="options[<?=$deptid?>]" value="<?=$deptname?>">
Which outputs:
<input type="hidden" name="options[1]" value="Department of Silly Walks">
http://codepad.org/DtgoZGe7
The problem with this is that the $deptid value becomes a value that's not actually directly named or referenced. I think this is potentially problematic to implement due to this abstraction of the value from the server to the client and back, so I would recommend what I have at the top instead. It's not much of a difference in practice, but it's more or less self-documenting.
Note, if you wanted to serialize a list of departments, it's a little trickier. You might, for instance, try this:
<input type="text" name="options[][deptid]" id="deptid" />
<input type="text" name="options[][deptname]" id="deptname" />
Which would add an indexed value for every input. However... They were would not be directly associated. So you would get, instead, two zero-indexed arrays for each key.
What I would suggest in this case is to use Javascript to add each new department's input elements, so you can give each a number like:
<input type="text" name="options[0][deptid]" id="deptid" />
<input type="text" name="options[0][deptname]" id="deptname" />
<br/>
<input type="text" name="options[1][deptid]" id="deptid" />
<input type="text" name="options[1][deptname]" id="deptname" />
<br/>
<input type="text" name="options[2][deptid]" id="deptid" />
<input type="text" name="options[2][deptname]" id="deptname" />
<br/>
<input type="text" name="options[3][deptid]" id="deptid" />
<input type="text" name="options[3][deptname]" id="deptname" />
Or do the old-school POSTBACK method and use PHP to count $POST['options'] and "manually" add a new "row" of inputs with the same index. It's a common trap, so you just have to think about it if this is what you're after at some point.
$_POST is already an associative array and I recommend you not to complicate things beyond that because $_POST already holds the data came from your form.
$myassoc = $_POST;
print_r($myassoc);
and the associative array that you will receive is organized and named same in the name attribute of the input elements in your form (including textarea)
Other Insights
As I see your code you want to put the deptname data to deptid as it reaches the PHP server-side code. well the thing you can do with is is just assign it to the key deptid
$_POST['deptid'] = $_POST['deptname'];
$myassoc = $_POST;
print_r($myassoc);
<form method="post">
<input type="text" name="formdata['deptid']" />
<input type="text" name="formdata['deptname']" />
<input type="submit" />
</form>
<?php
if(isset($_POST['formdata']))
{
$deptid = $_POST['formdata']['deptid'];
$deptname = $_POST['formdata']['deptname'];
}
?>
Build a JS object with the appropriate structure, convert it to JSON with JSON.stringify(), POST it, and then do json_decode($_POST['data'],true).
You'll have an exact copy from JS object, to PHP associate array. Drop the second parameter of true to get a PHP object.
$_POST is already an associative array.
You can rebuild an array of the form you need from this by just assigning $_POST to a variable
$myarray = $_POST;
Now $myarray is what you require. Do var_dump($myvar);.
Why would you want to do that?
But, you CAN send "arrays" through forms like this:
<form method="post">
<input type="text" name="textboxes[]" />
<input type="text" name="textboxes[]" />
<input type="submit" />
</form>
<?php
if(isset($_POST['textboxes']))
var_dump($_POST['textboxes']);
?>
$deptid = $_POST['deptid'];
$array = array($$deptid => $_POST['deptname']);
print_r($array);

POST an array from an HTML form without javascript [duplicate]

This question already has answers here:
How to get an array of data from $_POST
(3 answers)
Closed 1 year ago.
I have a form that is a little complex and I am hoping to simplify the server-side (PHP) processing by natively POSTing an array of tuples.
The first part of the form represents a User:
First Name
Last Name
Email
Address
etc
The second part of the form represents a Tree:
Fruit
Height
etc
The problem is that I need to be able to POST multiple Trees for a single User in the same form. I would like to send the information as a single User with an array of Trees but this might be too complex to do with a form. The only thing that comes to mind is using javascript to create some JSON message with a User object and an array of Tree objects. But it would be nice to avoid javascript to support more users (some people have scripts turned off).
check this one out.
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">
<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">
<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">
<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">
it should end up like this in the $_POST[] array (PHP format for easy visualization)
$_POST[] = array(
'firstname'=>'value',
'lastname'=>'value',
'email'=>'value',
'address'=>'value',
'tree' => array(
'tree1'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree2'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree3'=>array(
'fruit'=>'value',
'height'=>'value'
)
)
)
You can also post multiple inputs with the same name and have them save into an array by adding empty square brackets to the input name like this:
<input type="text" name="comment[]" value="comment1"/>
<input type="text" name="comment[]" value="comment2"/>
<input type="text" name="comment[]" value="comment3"/>
<input type="text" name="comment[]" value="comment4"/>
If you use php:
print_r($_POST['comment'])
you will get this:
Array ( [0] => 'comment1' [1] => 'comment2' [2] => 'comment3' [3] => 'comment4' )

Should posting one of two forms in a page post all fields in both forms, or have I got a bug in my code

I have two forms in a page. Have decided to update my post with more realistic code from the page itself...
<form action="test.php" method="POST">
<strong>Details of work carried out</strong>
<textarea name="detailsOfWorkCarriedOut"></textarea>
<strong>Materials used</strong>
<textarea name="materialsUsed"></textarea>
<input type="hidden" name="submitted" value="true">
<input type="submit" value="Save">
<form/>
<br />
<form action="test.php" method="POST">
<strong>Details of work not carried out</strong>
<textarea name="detailsOfWorkNotCarriedOut"></textarea>
<input type="hidden" name="submitted" value="true">
<input type="submit" value="Save">
</form>
"test.php" simply contains:
<?php
print_r($_POST)
?>
No matter which form I post, I always get the same array returned:
Array ( [detailsOfWorkCarriedOut] => [materialsUsed] => [submitted] => true [detailsOfWorkNotCarriedOut] => )
Why is this?
Your problem is the
<form/>
at line 8: replace it with
</form>
:)
Yes, browsers submit only the fields in the form in which the submit button is nested. You could use JavaScript to monitor form submissions and include values from the other form in the submission, but you're seeing the expected behavior right now.
Your submit button is inside one form, so, it only submits that one form.
So, no, $_POST should not contain data that comes from both forms : it should only contain data coming from the submitted one.
(but, why don't you try it ? )
Something ugly like this should do the trick, if you're only willing to test :
<form method="get">
<input name="a" type="text">
</form>
<form method="get">
<input name="b" type="text">
</form>
<?php
var_dump($_GET);
(Yeah, it's ugly, I said ^^ )
If you type something into the first field and submit by pressing ENTER, you'll get :
array
'a' => string 'aa' (length=2)
And, with the second field :
array
'b' => string 'second field' (length=12)
So, only one form each time :-)

Is it possible to group $_POST variables?

I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )
Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:
$_POST['form1']['datax']
or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:
$array1 = array($_POST['datax'],$_POST['datay'],...);
$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);
since there are around 60 fields.
I hope I was able to explain this well and as always thank you very much..
If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:
<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />
Goes to:
print_r($_POST)
foo => array (
0 => a
1 => b
2 => c
bar => d
baz => array(
0 => e
1 => f
)
)
If you name your inputs properly, you can do that. Example:
<input type="text" name="textInput[]" />
<input type="text" name="textInput[]" />
That will populate an array in $_POST named textInput. That is:
$_POST['textInput'][0] == "whatever the first was set to be"
$_POST['textInput'][1] == "whatever the second was set to be"
Using square brackets after the input name will cause it to be grouped in PHP:
<input name="foo[]" type="text" value="1" />
<input name="foo[]" type="text" value="2" />
You can also make an associative array:
<input name="foo[bar]" type="text" />
I think multi-dimensional arrays would also work, but I'm not sure if I've actually tried it.
Edit: Here's the same thing answered in the PHP FAQ.
you can use your form fields like this:
<input type="text" name="form1['datax']"/>

Categories