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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating / Using Arrays with Forms

    Hey Guys,

    I've been searching all over the internet for a simple answer to this and can't seem to get anything working... Here is what I am looking to do:

    Use a form which will update a bunch of like elements:

    <form method="POST" action="samepage.html">

    <!-- My First Set From the Database -->
    <input name="first_input" value="1">
    <input name="second_input" value="1">
    <input name="third_input" value="1">

    <!-- My Second Set From the Database -->
    <input name="first_input" value="1">
    <input name="second_input" value="1">
    <input name="third_input" value="1">

    <input type="submit" value="Update" name="update_links" >

    I know the names cannot be the same for all of the names of the inputs, but I want some sort of array of inputs for this and then maybe I can iterate through them on the postback.

    I am detecting the submit, via:

    if ($_POST['update_links'])
    { //Code Goes Here }



    I would expect to be able to do this quite simply, but just can't figure it out I hope the example above was clear enough... There can be any number of groups of inputs, so I need to iterate thru all of them...


    Thanks a ton!!!!

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Maybe this is what you want to do..
    PHP Code:
    $inputs = array ( "first_input""second_input""third_input");

    foreach (
    $inputs as $key){
    echo 
    "<input name=".$key." value=\"1\">";

    That would create all the input fields from array.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,044
    Thanks
    2
    Thanked 316 Times in 308 Posts
    This will help you with creating form field names that are arrays - http://www.php.net/manual/en/faq.htm...aq.html.arrays
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've got the inputs down, I think... I have had this:


    echo '<input type="hidden" name="site_id['.$indexid.']" value="'.$msubs['sid'].'">';

    echo '<input type="hidden" name="cas_id['.$indexid.']" value="'.$gcc['cid'].'">';

    echo '<input type="hidden" name="exists['.$indexid.']" value="'.$linkinfo[0]['count'].'">';

    Now, I need to be able to get all the site_id[x], cas_id[x], exists[x] out of the post after the submit... That is really what I have been struggling with.

    I did try looking at that array page from php.net and just couldn't come up with code from it that would iterate through the above I tried, though!

    thanks again.

  • #5
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Been trying different methods all morning Nothing is working for me.

    I can't find any example on the web of what I am trying to accomplish even though I am sure it is being done over and over again.

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    I would try to help you, but i still don't understand what you are talking about. And i think i'm not the only one, couse people here would answer you i'm sure if they understood you better.

    Sometimes a good night sleep solves the problem

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,044
    Thanks
    2
    Thanked 316 Times in 308 Posts
    You can use foreach(...) to iterate through each array or even the whole $_POST array if you want - http://php.net/foreach
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I figured out what was wrong... 1 stupid mistake and 1 way I didn't know about

    Stupid Mistake...

    <textarea name="" name="MyVar"></textarea>

    I had 2 names identified and it took the 1st one

    To Retreive:

    for ($i = 1; $i <= $total; $i++) {
    $myitem = $_POST['MyVar'][$i];

    echo $i.' - '.$myitem.'<br />';
    }


  •  

    Posting Permissions

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