...

View Full Version : php array from posted form data



ejscottb
07-19-2012, 04:39 PM
####### SORRY, MY TITLE SHOULD BE: PHP ARRAY FROM DATA - I'm not wanting to use POSTED FORM DATA, I'm using dynamic data called from a database. I just included the form elements here so users could see how the script processes posted data. #######

So I've got this form using a class function. Only I don't want to use the form. I'm just going to call data from a couple of database tables and process it to input into another database, so no posted values. I know I only need 'myfield_2', which will have a dynamic value, and all the others ('myfield_1' and 'myfield_3') will be equal to nothing. I have to use the class unaltered.

How can I express $postFields["myfields"] = array(), much like I've done for $lists? My head hurts much.

The form fields would look like this:


<input type="text" name="myfield_1" value="" />
<input type="text" name="myfield_2" value="B" />
<input type="text" name="myfield_3" value="" />

Form processor:


$contactFields = array();
$contactFields["email_address"] = "bob@here.com";
$contactFields["first_name"] = "Bob";
$contactFields["last_name"] = "Smith";

$contactFields["lists"] = array("http://pathtolist.com/contactlistID");

$contactFields["myfields"] = array();
foreach($_POST as $key=>$val) {
if (strncmp($key, 'my_field_', strlen('my_field_')) === 0) {
$contactFields["myfields"][substr($key, strlen('my_field_'), strlen($key)-1)] = $val;
}
}

DrDOS
07-19-2012, 05:01 PM
$field1 = $_POST['myfield_1'];
$field2 = $_POST['myfield_2'];
$field3 = $_POST['myfield_3'];
$my_fields=array($field1,$field2,$field2);

I think that should do it.

ejscottb
07-19-2012, 05:19 PM
There are no posted values - I won't be using the form. I just included it here as it applied to the form processor. I will only need myfield_2 and its value $var, but I assume I have to express the other 2 fields in the array statement (???) Something like below (neither of which work):



$contactFields["myfields"] = array(myfield_1=> "", myfield_2=> "$var", myfield_3=> "");

// OR MAYBE

$contactFields["myfields"][1] = "";
$contactFields["myfields"][2] = $var;
$contactFields["myfields"][3] = "";

Fou-Lu
07-19-2012, 06:19 PM
If you want to treat these all as an array, then you do as such. Change this:


<input type="text" name="myfield_1" value="" />
<input type="text" name="myfield_2" value="B" />
<input type="text" name="myfield_3" value="" />


to this:


<input type="text" name="myfield[1]" value="" />
<input type="text" name="myfield[2]" value="B" />
<input type="text" name="myfield[3]" value="" />

And access in PHP now becomes:


$_POST['myfield'][1]

For example. You can use whatever names or values you want for the index.

ejscottb
07-20-2012, 01:13 PM
I got it - in case anybody ever needs this, the solution is:



$contactFields["myfields"] = array(1=> "", 2=> $var, 3=> "");


Note no quotes surrounding the $var in array definition.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum