Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-19-2012, 04:39 PM   PM User | #1
ejscottb
New to the CF scene

 
Join Date: Jul 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ejscottb is an unknown quantity at this point
php array from posted form data

####### 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:
Code:
        <input type="text" name="myfield_1" value="" />             
        <input type="text" name="myfield_2" value="B" />            
        <input type="text" name="myfield_3" value="" />
Form processor:
PHP Code:
$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($keystrlen('my_field_'), strlen($key)-1)] = $val;
            }
        } 

Last edited by ejscottb; 07-19-2012 at 06:42 PM..
ejscottb is offline   Reply With Quote
Old 07-19-2012, 05:01 PM   PM User | #2
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,155
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
PHP Code:
$field1 $_POST['myfield_1'];
$field2 $_POST['myfield_2'];
$field3 $_POST['myfield_3'];
$my_fields=array($field1,$field2,$field2); 
I think that should do it.
DrDOS is offline   Reply With Quote
Old 07-19-2012, 05:19 PM   PM User | #3
ejscottb
New to the CF scene

 
Join Date: Jul 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ejscottb is an unknown quantity at this point
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):

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

// OR MAYBE

$contactFields["myfields"][1] = "";
$contactFields["myfields"][2] = $var;
$contactFields["myfields"][3] = ""
ejscottb is offline   Reply With Quote
Old 07-19-2012, 06:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
If you want to treat these all as an array, then you do as such. Change this:
Code:
        <input type="text" name="myfield_1" value="" />             
        <input type="text" name="myfield_2" value="B" />            
        <input type="text" name="myfield_3" value="" />
to this:
Code:
        <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:
PHP Code:
$_POST['myfield'][1
For example. You can use whatever names or values you want for the index.
Fou-Lu is offline   Reply With Quote
Old 07-20-2012, 01:13 PM   PM User | #5
ejscottb
New to the CF scene

 
Join Date: Jul 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ejscottb is an unknown quantity at this point
I got it - in case anybody ever needs this, the solution is:

PHP Code:
$contactFields["myfields"] = array(1=> ""2=> $var3=> ""); 
Note no quotes surrounding the $var in array definition.
ejscottb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:00 AM.


Advertisement
Log in to turn off these ads.