Forum Moderators: coopster

Message Too Old, No Replies

php post array

php post array

         

xiongbin

1:18 pm on May 27, 2008 (gmt 0)

10+ Year Member



<form name="form1" method="post" action="c.php">
<input type="hidden" name="x" value=" <? print_r(array(1,2,3));?>">
<input type="submit" name="Submit" value="送出">
</form>

c.php

<?

foreach($_POST[x] as $key)
echo $key;

?>

help me to runing this...

eelixduppy

1:31 pm on May 27, 2008 (gmt 0)



Your HTML would need to be something like the following for this to work as you want:

<input type="hidden" name="x[]" value="1">
<input type="hidden" name="x[]" value="2">
<input type="hidden" name="x[]" value="3">

xiongbin

2:51 pm on May 27, 2008 (gmt 0)

10+ Year Member



<form name="form1" method="post" action="c.php">
<input type="hidden" name="x" value=" <? print_r(array("1"=>1,"2"=>2,"3"=>3));?>">
<input type="submit" name="Submit" value="送出">
</form>
c.php

<?

foreach($_POST[x] as $key)
echo $key;

?>

help me to runing this... ?

jatar_k

2:58 pm on May 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld xiongbin,

are you sure that you don't need what eelix posted?

<form name="form1" method="post" action="c.php">
<input type="hidden" name="x[]" value="1">
<input type="hidden" name="x[]" value="2">
<input type="hidden" name="x[]" value="3">
<input type="submit" name="Submit" value="送出">
</form>

if not then you may need to add some more info

do you need to build the form from an array of values?

xiongbin

1:46 am on May 28, 2008 (gmt 0)

10+ Year Member



how can i post special array ...

jatar_k

3:41 am on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could post it as a string and separate somehow

<?
$thearray = array("1"=>1,"2"=>2,"3"=>3));
$thestring = implode('-',$thearray);
?>
<form name="form1" method="post" action="c.php">
<input type="hidden" name="x" value=" <? echo $thestring; ?>">
<input type="submit" name="Submit" value="送出">

then

$thearray = explode('-',$_POST['x'];
print_r($thearray);

should work, didn't test it

xiongbin

3:51 am on May 28, 2008 (gmt 0)

10+ Year Member



post array from form ,should easy to get array_key and array_value.

as like $_SESSION

xiongbin

3:51 am on May 28, 2008 (gmt 0)

10+ Year Member



any way? haha

jatar_k

3:59 am on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe serialize it then

cookie?

I assume you can't use sessions

i don't completey understand the reason you are doing what you're doing

xiongbin

6:11 am on May 28, 2008 (gmt 0)

10+ Year Member



if have a array
$array=array("a"=>array("b"=>3,"d"=>"this"),"xs"=>2,"m"=>array(array(1,2,3)));

how can i use form post the $array to another web page

coopster

1:41 pm on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



serialize [php.net] it and put it in a hidden form field, or use sessions, or perhaps even a cookie. The key here is to serialize the array before and unserialize it before you go to use it once again.

xiongbin

3:17 pm on May 28, 2008 (gmt 0)

10+ Year Member



please give me a bit code ..

ahaha

jatar_k

3:19 pm on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at the manual

serialize() [php.net]

xiongbin

3:30 pm on May 28, 2008 (gmt 0)

10+ Year Member



tks...

xiongbin

3:36 pm on May 28, 2008 (gmt 0)

10+ Year Member



can you help me any another way...

ahaha ..

coopster

4:21 pm on May 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean serialize is not going to work for you? You may have to offer some more details ...

eelixduppy

11:11 pm on May 28, 2008 (gmt 0)



If you are looking for a serialize code example, this is the best I can give you. If this isn't what you are looking for you are going to have to elaborate on your issue.

The following will add the proper data to the hidden input field:


<form name="form1" method="post" action="c.php">
<?php
$array = array('one','two','three');
printf('<input type="hidden" name="x" value="%s" />', [url=http://www.php.net/htmlentities]htmlentities[/url]([url=http://www.php.net/serialize]serialize[/url]($array)));
?>
<input type="submit" name="Submit" value="送出">

And then on c.php, your action page, you are going to have to get the data back in the following way:


$theArray = [url=http://www.php.net/unserialize]unserialize[/url]([url=http://www.php.net/html-entity-decode]html_entity_decode[/url]($_POST['x']));
echo '<pre>'; [url=http://www.php.net/var-dump]var_dump[/url]($theArray); echo '</pre>';