SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can I use php arrays in form fields?

    I am having problems trying to use values from an array defined within a function. The function is called when the form page is loaded - the arrays are then populated. That part works fine.

    However when the form is submitted, I wish the content of the arrays to be used in another function on the success page - I just don't know how to access them!

    I have tried:
    calling the variables directly (e.g. print "$var[2]"
    using $_POST (e.g. print "$_POST['var']"; and "$_POST['var[2]']"

    However the arrays are not form fields. Perhaps I need to create hidden form fields, then write the contents of the arrays to these?
    Any help gratefully received...
    Best regards, Brian

  2. #2
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    belfast
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey - yip, you got it in one ... you need a form that has a hidden field. On page 1 assign your array ... then on the page 2 which you post to just use you $_POST code as above ...

    check out

    http://www.webmasterworld.com/forum88/1204.htm

  3. #3
    SitePoint Zealot headsnet's Avatar
    Join Date
    Feb 2001
    Location
    London, UK
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alternatively you could store the arrays in the session and access/update them from wherever you wanted.

    Info on PHP sessions is here: http://www.php.net/session

  4. #4
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by omegadm
    I am having problems trying to use values from an array defined within a function. The function is called when the form page is loaded - the arrays are then populated. That part works fine.

    However when the form is submitted, I wish the content of the arrays to be used in another function on the success page - I just don't know how to access them!

    I have tried:
    calling the variables directly (e.g. print "$var[2]"
    using $_POST (e.g. print "$_POST['var']"; and "$_POST['var[2]']"

    However the arrays are not form fields. Perhaps I need to create hidden form fields, then write the contents of the arrays to these?
    Any help gratefully received...
    Best regards, Brian
    an array of an array is a multidimentional array. they are called like this:
    $_POST['var'][2]

    to create arrays with form data:
    <input name="var[2]">
    or
    <input name="var[]">

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by headsnet
    Alternatively you could store the arrays in the session and access/update them from wherever you wanted.

    Info on PHP sessions is here: http://www.php.net/session
    I like this idea, but am running into a bit of difficulty. This is what I have for my first call of the program:

    Code:
     
      session_start();
      // declare arrays
      $urls    = array();
      $titles   = array();
      $descriptions  = array();
    
      while loop.... 
    	 $_SESSION['urls[]'] = $bcurl;
    	 $_SESSION['titles[]'] = $bctitle;
    	 $_SESSION['descriptions[]'] = $description;
      end-while
    For the succes page:
    Code:
      $sql = "INSERT into $linkstable SET
    	url    = $_SESSION['urls[0]'],
    	title    = $_SESSION['titles[0]'],
    	description  = $_SESSION['descriptions[0]']";
    I am only trying the first[0] array value at the moment, but eventually this will also go in a while loop to set all the values. However, the error being reported when loading the script (first call) is:

    "Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/httpd/omegadm.co.uk/wsnlinks/import.php on line 172"

    This is the first line of the sql statement i.e.
    url = $_SESSION['urls[0]'],

    Can anyone put me on track please?

    Best regards, Brian

  6. #6
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    plz refer above to what i said about the syntax of 'arrays of arrays' (aka: multidimentional arrays)

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers for the reminder Darchangel
    I have changed the code but still the same error message:

    Code:
     
    while loop.... 
    	 $_SESSION['urls'][] = $bcurl;
    	 $_SESSION['titles'][] = $bctitle;
    	 $_SESSION['descriptions'][] = $description;
    end-while
    For the succes page:
    Code:
     $sql = "INSERT into $linkstable SET
    	url = $_SESSION['urls'][0],
    	title = $_SESSION['titles'][0],
    	description = $_SESSION['descriptions'][0]";
    Am I missing something obvious??

    Best regards, Brian

  8. #8
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     $sql = "INSERT into $linkstable SET
    	url = $_SESSION['urls'][0],
    	title = $_SESSION['titles'][0],
    	description = $_SESSION['descriptions'][0]";
    2 things:
    1) use single quotes around your values in your queries
    2) you are creating a string variable (ie: a one dimensional linear collection of arbitrary characters). arrays, especially multi-dimentional arrays, contain characters which php may see as literal characters or may see as part of the syntax of an array (like the square bracets) - so what you're doing here with arrays may work or may not. to ensure that it does, either include the arrays outside of the quotes or surround them with {}

    so your final product should be one of the following
    PHP Code:
    $sql "INSERT into $linkstable SET
        url = '
    {$_SESSION['urls'][0]}',
        title = '
    {$_SESSION['titles'][0]}',
        description = '
    {$_SESSION['descriptions'][0]}'"
    PHP Code:
    $sql "INSERT into $linkstable SET
        url = '" 
    $_SESSION['urls'][0] . "',
        title = '" 
    $_SESSION['titles'][0] . "',
        description = '" 
    $_SESSION['descriptions'][0] . "'"

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - got it. i used the {} syntax

    Thanks Darchangel

    best regards, brian


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
  •