posting form data into array


function addShipper(){
$shipper = array(
‘Contact’ => array(
‘PersonName’ => <?php echo $_POST["name"]; ?>, <------------------------------- I need help with this part
‘CompanyName’ => ‘Hairy Niver’,
‘PhoneNumber’ => ‘603-820-666655’

Im trying to post data from an HTML form but its not working. Can someone show me how to fix this?


maybe this post should be in the beginner’s forum instead. You have some very bad concepts applied here.

echo is used to output data to a user. like document.write of javascript or print of asp, c++ etc. you cannot echo the data to a variable (which an array is essentially a group of variables assigned to one value.)

I never use arrays since I started programming in Windows 98 days. I have never used arrays except so I cannot tell you exactly how to do this in php but you must begin by removing the echo statement.

BUT you should sanitize POST data before you assign it to the array. at least try the following:

trim($_POST[‘name’]) then stripslashes($_POST[‘name’]) then htmlspecialchars($_POST[‘name’])
and assign it to a variable $nameHolder = $_POST[‘name’]

then perhaps add this variable to the array:
‘PersonName’ => $nameHolder

again, I never use arrays so maybe this is not how it is done. In theory, you just need to know the array variable and the value position: arraygroupname[0] to access update its contents.

maybe someone with array experience can help you. Just remember that echo is output to the screen/user not to assign data to a variable (= sign assigns a var in php.)

I hope that you are able to repair the code and get it working :slight_smile:


[php]function addShipper(){
$shipper = array(
‘Contact’ => array(
‘PersonName’ => $_POST[“name”],
‘CompanyName’ => ‘Hairy Niver’,
‘PhoneNumber’ => ‘603-820-666655’[/php]

Should work fine.