Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2011, 07:25 AM   PM User | #1
Juniper747
New Coder

 
Join Date: Apr 2011
Posts: 92
Thanks: 26
Thanked 0 Times in 0 Posts
Juniper747 is an unknown quantity at this point
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
Juniper747 is offline   Reply With Quote
Old 05-04-2011, 08:03 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
Juniper747 (05-04-2011)
Old 05-04-2011, 04:08 PM   PM User | #3
Juniper747
New Coder

 
Join Date: Apr 2011
Posts: 92
Thanks: 26
Thanked 0 Times in 0 Posts
Juniper747 is an unknown quantity at this point
Worked perfectly, thanks!
Juniper747 is offline   Reply With Quote
Old 05-04-2011, 06:52 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Tags
array, input

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:14 AM.


Advertisement
Log in to turn off these ads.