Page 1 of 1

Posting array information into php

Posted: Mon Apr 13, 2009 2:33 pm
by DavidJF
Hi,

I am new to PHP and I am trying to import information from an Array using the POST method, below is my code;

The variables I am posting in are style names of shirts coming from a shopping cart.
My intention is to create a new variable - $stylename0, $stylename1, $stylename2 etc etc for each of the items I am posting in from the shopping cart array(externally).
I will then post these into my SQL database.

The problem I am having is indexing the new $stylename variables so that they have the number that the loop is currently at in the name.

Any help would be greatly appreciated.

<?php
//register.php
include "connect.php";

if (isset($_POST['username']))
{
$arraylength = ($_POST['arraylength']);

for ($num=0; $num <= $arraylength; $num++ )
{
$stylename.$num = $_POST['stylename'.$num];
echo "result=$stylename.$num";
}
}

?>

Re: Posting array information into php

Posted: Mon Apr 13, 2009 3:10 pm
by McInfo
A lesson by example:

Code: Select all

<form method="post" action="">
    <input type="text" name="posted[]" value="one" /><br />
    <input type="text" name="posted[]" value="two" /><br />
    <input type="text" name="posted[]" value="three" /><br />
    <input type="text" name="posted[]" value="four" /><br />
    <input type="submit" value="Submit Array" />
</form>
 
<pre><b>Posted: </b><?php
if (!empty($_POST['posted'])) {
    if (is_array($_POST['posted'])) {
        foreach ($_POST['posted'] as $p => $posted) {
            echo "\n$p = $posted";
        }
    }
    else {
        echo $_POST['posted'];
    }
}
?></pre>
 
<pre>$_POST => <?php print_r($_POST); ?></pre>
Edit: This post was recovered from search engine cache.