/    Sign up×
Community /Pin to ProfileBookmark

Passing array like a variable in a form?

I know how to pass a variable in a form:

<?php
$display_variable = $_POST[variable_name];
echo $display_variable;
?>

<form method=”POST” action”<?php $_SERVER[PHP_SELF]; ?>”>
<input type=”text” name=”variable_name” value=”This Value” >
<input type=”submit”>
</form>

Submission will output:

This Value

My question is, is there a simple way to pass an array like this?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 13.2010 — Not sure what you mean. Perhaps [url=http://php.net/serialize]serialize[/url]() is what you're looking for? Perhaps you'd be better off using PHP sessions to maintain that data between pages instead of passing it via the form?
Copy linkTweet thisAlerts:
@anothenauthorSep 13.2010 — okay lets say I have an array:

[code=php]<?php
$stuff = array();
$stuff[0] = "shoes";
$stuff[1] = "paper";
$stuff[2] = "table";
?>
[/code]


and I want to pass this array on in a form, just like a regular variable.

[code=php]<form method="POST" action="<?php $_SERVER[PHP_SELF]; ?>">
<input type="hidden" name="mystuff" value="<?php echo $stuff; ?>">
<input type="hidden" name="display" value="1">
<input type="submit" >

<?php
if($_POST[display] == 1){
$stuff = $_POST[mystuff];
echo $stuff;}
?>[/code]


Now I know this is not legal. It only works with each individual

array content.

I want to pass the array and it's contents on to the next page.

I have a round about way of doing it, but I figured there may be a

simpler way.
Copy linkTweet thisAlerts:
@Jarrod1937Sep 13.2010 — As mentioned earlier you'll want to use serialize for this type of task. Arrays by their nature are multidimensional and serialize will take that structure and output a linear representation that will allow you to print it out as a single string. Though keep in mind to not use this for anything important, or verify the data at each step, as passing the data via a hidden input is wide open to abuse.
Copy linkTweet thisAlerts:
@NogDogSep 14.2010 — You could do something like this, though I'd still prefer to use sessions if at all possible (or something else that keeps the data on the server).
[code=php]
foreach($stuff as $value) {
if(!is_scalar($value) {
error_log("non-scalar value in stuff");
continue;
}
// note use of brackets in input element's name:
printf("<input type='hidden' name='stuff[]' value='%s' />n",
htmlspecialchars($value, ENT_QUOTES));
}
[/code]
Copy linkTweet thisAlerts:
@anothenauthorSep 14.2010 — thx y'all

I'm still hashing through serialize()

and I've considered using sessions

I'll study through that one Nogdog.
Copy linkTweet thisAlerts:
@eval_BadCode_Sep 14.2010 — I would suggest serialize(). Not that this is the only method, but this method was specifically designed for this type of job.

Coding the variable directly into the form submission means it will be there when you come back from vacation. If you use a session, it may not be.

Here's a great snippet I found from php.net, first post for serialize()

[CODE]

serialize can be a handy tool for protecting against page expirations if you are using POST's.

In my code I call the following function whenever I detect that the page has expired.

<?php
function savePost() {
// Save this in the login form
if (isset($_POST['savePost'])) {
echo "<input type='hidden' name='savePost' value="{$_POST[savePost]}">";
} else {
$savePost = htmlspecialchars(serialize($_POST));
echo "<input type='hidden' name='savePost' value="$savePost">";
}
}
?>

and when I load each page I call the function:

<?php
function restorePost() {
if (isset($_POST[savePost])) {
$_POST = unserialize(stripslashes(htmlspecialchars_decode($_POST[savePost])));
}
}
?>

That way if a user takes a long time doing something, they can re-authenticate and have their work restored.

[/CODE]


Wish I knew that two weeks ago.
Copy linkTweet thisAlerts:
@anothenauthorSep 14.2010 — Well, I can see that at some point I'll refer back to this thread.

I'm still trying to muddle my way through serialize, but I'll just get

back to it later.

I know sessions() would be the simplest, but because I may

be transferring the code to someone else, it might generate

issues.

I went ahead and dug up an old method I developed last year,

and I managed to get it working so for now I'll go with that.

If you want to see a sample:

[code=php]










<?php
if($_POST[control] == 1){

$a = 0;
while($a < $_POST[quantity]){
$rankresults = $_POST[rank .$a];

echo $rankresults;

$a++;
} // while a<quantity

} else { ?>






<?php // set the array ranking
$ranking = array
("JPG", "EUR", "GBP", "AUD", "NZD", "USD", "CAD", "CHF");
$quantity = count($ranking);

?>

<form method="POST" action="<?php $_SERVER[PHP_SELF]; ?>">
<?php
// $a is the index of the array

?>

<?php $a = 0; $_name="rank"; ?>

<table>
<?php while($a < $quantity){ ?>

<?php // copy contents of $ranking into multiple forms


?>

<tr><td>
<input type="text" name="<?php echo $_name.$a; ?>"
value="<?php echo $ranking[$a]; ?>">
<input type="hidden" name="quantity" value="<?php echo $quantity; ?>">
<input type="hidden" name="control" value="1">

</td>
</tr>

<?php $a++; ?>
<?php } ?>
</table>

<input type="submit">
</form>

<?php } ?>

[/code]


It's a trick method and for me, I thought it was quite innovative.

(change <input type="text" to type="hidden">) the "text" was temporary
×

Success!

Help @anothen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...