Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,911
    Thanks
    15
    Thanked 227 Times in 227 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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] = ""

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •