SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing the array key names

    I have a series of forms on my page which has content generated by my database. When I click on submit they look like this:

    PHP Code:
    array
      
    'name2' => string 'PHP' (length=3)
      
    'publish2' => string 'YES' (length=3)
      
    'submit2' => string 'submit' (length=6)
      
    'hidden2' => string '2' (length=1)
      
    'hidden002' => string 'PHP' (length=3
    or like this:

    PHP Code:
    array
      
    'name5' => string 'Web development' (length=15)
      
    'publish5' => string 'YES' (length=3)
      
    'submit5' => string 'submit' (length=6)
      
    'hidden5' => string '5' (length=1)
      
    'hidden005' => string 'Web development' (length=15
    What I want to be able to do is change the key names on submission:

    PHP Code:
    array
      
    'first' => string 'Web development' (length=15)
      
    'second' => string 'YES' (length=3)
      
    'third' => string 'submit' (length=6)
      
    'fourth' => string '5' (length=1)
      
    'fifth' => string 'Web development' (length=15
    Looking elsewhere on the web there are guides about how to change the key names if they are already known [http://www.hotscripts.com/forums/php...rray-key.html] but here my forms are created dynamically so I won't know the key names until they are submitted
    Let everyday be Christmas

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Why are you not able to simply change the name attribute in each form element?

    Code:
    <input type="text" name="first">

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    or just make your dynamic entries arrays....
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    but here my forms are created dynamically so I won't know the key names until they are submitted
    Oh, I missed that part.

    Well will you know for certain the order in which they are being submitted then?

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    My question would be why are you sending different names to the form? Name can be the same over and over again on the page -- only ID needs to be unique and have the numbers attached. Even "generated by the database" that makes little if any sense... or are you doing multiple identical fields inside a single form? If so, you may want to consider using array based form names.

    name="element[1][name]"
    name="element[1][publish]"

    Which would resolve to $_POST['element'][1]['name'] for example.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This can no doubt be improved on, but if you do know the order in which your form elements are going to be returned:

    PHP Code:
    // what is incoming - all you know is the order, not the key names
    $_POST = array(
    'name1' => 'Bob' ,
    'name2' => 'The' ,
    'name3' => 'Builder' ,
    );

    // the rewrite keys in the correct order
    $newkeys = array('first''middle''last');

    // put this function into a file and include it

    function newKeys($existing$newkeys){

    // a really simple check that the arrays are the same size
      
    if(count($existing)  !== count($newkeys)) 
         return 
    false// or pipe out a useful message, or chuck exception

    $data = array();  // set up a return array
    $i ;
    foreach( 
    $existing as $k=>$v){
    $data[$newkeys[$i]] = $v;  // build up the new array
    $i++;
    }
    return 
    $data// return it
    }


    var_dump(newKeys($_POST$newkeys) ); 
    gives:

    array
    'first' => string 'Bob' (length=3)
    'middle' => string 'The' (length=3)
    'last' => string 'Builder' (length=7)

    Although to be honest, you might as well have built your sql statement inside that loop as reassign the key names, but only you know the full extent of your challenge.

  7. #7
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    My question would be why are you sending different names to the form? Name can be the same over and over again on the page -- only ID needs to be unique and have the numbers attached. Even "generated by the database" that makes little if any sense... or are you doing multiple identical fields inside a single form? If so, you may want to consider using array based form names.

    name="element[1][name]"
    name="element[1][publish]"

    Which would resolve to $_POST['element'][1]['name'] for example.
    Lets take a step back.

    I want to display multiple forms on the same page. There are user details which I want the admin to be able to edit. But rather than have a list of links which the admin has to click on and then goes to a separate page I want all the forms on one page.

    Yes, it is possible to have the same form inputs on the same page but the problem arises when a form validation error is thrown and then the code for one input means the code for all inputs will be the same:

    PHP Code:
    value="isset($_POST['nameField'])? print ($_POST['nameField']): null;" 
    Which is why I was giving the name attribute values unique numbers... hence the array question... taking a step back... is there a better way of organising my form code to allow multiple forms on the same page?
    Let everyday be Christmas

  8. #8
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Set a value on your submit, then you know which submit was clicked.... or put a unique value in a hidden form element on each form, letting you know which form was sent. That way they can all have the same name, but you can still tell which one was sent.

  9. #9
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    Set a value on your submit, then you know which submit was clicked.... or put a unique value in a hidden form element on each form, letting you know which form was sent. That way they can all have the same name, but you can still tell which one was sent.
    Having the same name values for different forms is fine for submitting them. The problem comes after errors are produced. Any altered text placed in the input values by the code above will change on every form unless the input values are different. Having a unique submit name value won't alter that.
    Let everyday be Christmas

  10. #10
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Divisive Cotton View Post
    Having the same name values for different forms is fine for submitting them. The problem comes after errors are produced. Any altered text placed in the input values by the code above will change on every form unless the input values are different. Having a unique submit name value won't alter that.
    That doesn't make any sense, if you know what 'form' they are from, you only use them to populate THAT form.... because none of the other forms were submitted they shouldn't even HAVE values.

  11. #11
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    This can no doubt be improved on, but if you do know the order in which your form elements are going to be returned:

    PHP Code:
    // what is incoming - all you know is the order, not the key names
    $_POST = array(
    'name1' => 'Bob' ,
    'name2' => 'The' ,
    'name3' => 'Builder' ,
    );

    // the rewrite keys in the correct order
    $newkeys = array('first''middle''last');

    // put this function into a file and include it

    function newKeys($existing$newkeys){

    // a really simple check that the arrays are the same size
      
    if(count($existing)  !== count($newkeys)) 
         return 
    false// or pipe out a useful message, or chuck exception

    $data = array();  // set up a return array
    $i ;
    foreach( 
    $existing as $k=>$v){
    $data[$newkeys[$i]] = $v;  // build up the new array
    $i++;
    }
    return 
    $data// return it
    }


    var_dump(newKeys($_POST$newkeys) ); 
    gives:

    array
    'first' => string 'Bob' (length=3)
    'middle' => string 'The' (length=3)
    'last' => string 'Builder' (length=7)

    Although to be honest, you might as well have built your sql statement inside that loop as reassign the key names, but only you know the full extent of your challenge.
    thanks for that by the way it works well
    Let everyday be Christmas


Bookmarks

Posting Permissions

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