Reputation: 2140
I have a form that I created with rows of input fields that are created dynamically.
foreach ($data as $k => $v){
print "<tr>Table # $k</tr>";
print "<tr><td>First Name</td><td>Last Name</td></tr>"
for($i = 1; $i <= $data[$k]['tickets']; $i++){
print "<tr><td><input name='fname[]' type='text' class='mid_text_data' value=".$prefill[$k]['first_name']." maxlength='15' /></td><td><input name='lname[]' type='text' class='mid_text_data' value=".$prefill[$k]['last_name']." maxlength='15' /></tr>";
}
}
I've created the input fields based on the number of 'tickets' in my $data array, but I would like to prefill the values of fname with $prefill[$k]['first_name'].
My $prefill and $data variables look like:
$prefill = {
[145]=> array(9) {
["first_name"]=> string(5) "John"
["last_name"]=> string(6) "Doe"
["company"]=> string(10) "Big Business"
["email"]=> string(21) "[email protected]"
["photo_op"]=> int(0)
["gala"]=> int(1)
["chairman"]=> int(0)
["prefix"]=> NULL
["table_number"]=>int(60)
}
[146]=> array(9) {
["first_name"]=> string(5) "Jane"
["last_name"]=> string(6) "Doe"
["company"]=> string(10) "Big Business"
["email"]=> string(21) "[email protected]"
["photo_op"]=> int(0)
["gala"]=> int(1)
["chairman"]=> int(0)
["prefix"]=> NULL
["table_number"]=>int(60)
}
[147]=> array(9) {
["first_name"]=> string(5) "Stan"
["last_name"]=> string(6) "Derp"
["company"]=> string(10) "Big Business"
["email"]=> string(21) "[email protected]"
["photo_op"]=> int(0)
["gala"]=> int(1)
["chairman"]=> int(0)
["prefix"]=> NULL
["table_number"]=>int(83)
}
}
$data = {
[60]=> array(5) {
["tickets"]=> int(10)
["gala"]=> int(8)
["chair"]=> int(2)
["ind_id"]=> float(805879)
["photo"]=> int(0)
}
[83]=> array(5) {
["tickets"]=> int(5)
["gala"]=> int(5)
["chair"]=> NULL
["ind_id"]=> float(805879)
["photo"]=> int(1)
}
}
Currently, the form repeats 'John' to each one of my 'fname' fields.
Any ideas how to prefill the fields properly?
Thank you.
Upvotes: 2
Views: 862
Reputation: 360652
You're using $i
as the inner loop, but are using $k
in your array references. Try this:
for($i = 1; $i <= $data[$k]['tickets']; $i++) {
echo <<<EOL
<tr>
<td>
<input name="fname[]" type="text" class="mid_text_data" value="{$prefill[$i]['first_name']} maxlength='15' /></td>
<td>
<input name="lname[]" type="text" class="mid_text_data" value="{$prefill[$k]['last_name']}" maxlength="15" />
</tr>
EOL;
}
Note the use of a HEREDOC - far easier to read the html like this, instead of having it all smooshed into a single line of text.
However, to be strictly correct, you should pass actually be using htmlspecialchars()
to escape any HTML metacharacters in your prefill array - otherwise an extra "
or '
or >
could totally hose your form.
Upvotes: 0
Reputation: 11
The relationship between $prefill and $data seems to be like a "has-one". Couldn't you merge these two arrays for a simpler manipulation ?
For example, if you only use tickets from $data, why not putting it into $prefill directly (for instance, directly in your SQL query).
$prefill = {
[1]=> array(9) {
["first_name"]=> string(5) "John"
["last_name"]=> string(6) "Doe"
["company"]=> string(10) "Big Business"
["email"]=> string(21) "[email protected]"
["photo_op"]=> int(0)
["gala"]=> int(1)
["chairman"]=> int(0)
["prefix"]=> NULL
["table_number"]=>int(60),
["tickets"]=> int(10)
}
If you need all the $data's data, just nest $data parts in the corresponding $prefill parts :
$prefill = {
[1]=> array(9) {
["first_name"]=> string(5) "John"
["last_name"]=> string(6) "Doe"
["company"]=> string(10) "Big Business"
["email"]=> string(21) "[email protected]"
["photo_op"]=> int(0)
["gala"]=> int(1)
["chairman"]=> int(0)
["prefix"]=> NULL
["table_number"]=>int(60),
["data"]=> array(5) {
["tickets"]=> int(10)
["gala"]=> int(8)
["chair"]=> int(2)
["ind_id"]=> float(805879)
["photo"]=> int(0)
}
}
Upvotes: 1