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 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    92
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Inserting Default Array Values back into a form Input Field

    Basically, if an error comes up (for whatever reason), I want the initially entered values to re-appear, so that the user doesn't have to refill in the fields. This works just fine for my NON-array inputs, but I'm having trouble with making the Array values re-appear. Here's what I have:

    In php:
    Code:
    $contacts = "";
    $contacts = $_POST['contacts'];
    In the html:
    Code:
    <tr>
               <td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td>
              	<td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td>
             	<td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td> 
              	<td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td>
    </tr>
    Instead of putting the contact names back in the fields, my output result in this:

    Array Array Array Array

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Juniper747 View Post
    Basically, if an error comes up (for whatever reason), I want the initially entered values to re-appear, so that the user doesn't have to refill in the fields. This works just fine for my NON-array inputs, but I'm having trouble with making the Array values re-appear. Here's what I have:

    In php:
    Code:
    $contacts = "";
    $contacts = $_POST['contacts'];
    In the html:
    Code:
    <tr>
               <td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td>
              	<td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td>
             	<td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td> 
              	<td><input name="contacts[]" type="text" id="contacts[]" value="<?php print "$contacts"; ?>" size="30" maxlength="50" /></td>
    </tr>
    Instead of putting the contact names back in the fields, my output result in this:

    Array Array Array Array
    remove " before and after $contacts and because is a array try to use indexes, 0-3 to ouput the content.
    Code:
    <tr>
               <td><input name="contacts[]" type="text" id="contacts[]"
     value="<?php print $contacts[0]; ?>" size="30" maxlength="50" /></td>
              	<td><input name="contacts[]" type="text" id="contacts[]"
     value="<?php print $contacts[1]; ?>" size="30" maxlength="50" /></td>
             	<td><input name="contacts[]" type="text" id="contacts[]"
     value="<?php print $contacts[2]; ?>" size="30" maxlength="50" /></td> 
              	<td><input name="contacts[]" type="text" id="contacts[]"
     value="<?php print $contacts[3]; ?>" size="30" maxlength="50" /></td>
    </tr>
    best regards

  • Users who have thanked oesxyl for this post:

    Juniper747 (05-04-2011)

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    92
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Worked perfectly, thanks!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Juniper747 View Post
    Worked perfectly, thanks!
    you are welcome, keep in mind that in php all arrays have an index and using print_r you can see how was builded.

    best regards


  •  

    Tags for this Thread

    Posting Permissions

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