tags:

views:

90

answers:

3

Hi,

Is it possible to get form field values into array? EX:

 <?php

   array('one', 'two', 'three');    
    ?>

    <form method="post" action="test.php">
        <input type="hidden" name="test1" value="one" />
        <input type="hidden" name="test2" value="two" />
        <input type="hidden" name="test3" value="three" />
        <input type="submit" value="Test Me" />
    </form>

So is it possible to pass all form values no matter the number of them to the array in php ?

+3  A: 

It already is done.

Look at the $_POST array.

If you do a print_r($_POST); you should see that it is an array.

If you just need the values and not the key, use

$values = array_values($_POST);

http://php.net/manual/en/reserved.variables.post.php

Daniel A. White
thank you for your quick answer, can you direct me to which portion of code should I look at ?
Gandalf StormCrow
What would you like to know?
Daniel A. White
Great, how could I get rid of the submit button from the array being posted? And how could I add some elements to the array?
Gandalf StormCrow
I wouldn't add anything to that array. As for buttons, just leave the name field off.
Daniel A. White
tnx you're a champ :=)
Gandalf StormCrow
+3  A: 

This is actually the way that PHP was designed to work, and one of the reasons it achieved a large market penetration early on with web programming.

When you submit a form to a PHP script, all the form data is put into superglobal arrays that are accessible at any time. So for instance, submitting the form you put in your question:

<form method="post" action="test.php">
    <input type="hidden" name="test1" value="one" />
    <input type="hidden" name="test2" value="two" />
    <input type="hidden" name="test3" value="three" />
    <input type="submit" value="Test Me" />
</form>

would mean that inside test.php, you would have a superglobal named $_POST that would be prefilled as if you had created it with the form data, essentially as follows:

$_POST = array('test1'=>'one','test2'=>'two','test3'=>'three');

There are superglobals for both POST and GET requests, ie. $_POST, $_GET. There is one for cookie data, $_COOKIE. There is also $_REQUEST, which contains a combination of all three.

See the doc page on Superglobals for more info.

zombat
+1  A: 

Yes, just name the inputs the same thing and place brackets after each one:

<form method="post" action="test.php">
        <input type="hidden" name="test[]" value="one" />
        <input type="hidden" name="test[]" value="two" />
        <input type="hidden" name="test[]" value="three" />
        <input type="submit" value="Test Me" />
</form>

Then you can test with

<?php
print_r($_POST['test']);
?>
Xeoncross