PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Sending multiple values from a form having same field names.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sending multiple values from a form having same field names.

Réponse
 
LinkBack Outils de la discussion
Vieux 14/03/2008, 09h38   #1
Suamya Srivastava
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sending multiple values from a form having same field names.


Hi,

How can I send multiple values from a form to be stored in a database, as
name of the fields is the same?

For example:

<?php
foreach ($field_data as $field) {

$field_name=$field["field_name"];
$field_id=$field["field_id"];
$datatype=$field["datatype_name"];

?>
<input type="hidden" name="field_id" value="<?php echo $field_id;?>" />
<tr>
<td><strong><?php echo $field_name;?><strong></td>
<?php
if ($datatype=="text" || $datatype=="integer") {
echo "<td><input type=\"text\" name=\"field_data\"></td>";
}
elseif ($datatype=="textarea") {
echo "<td><textarea rows=\"10\" cols=\"100\"
name=\"field_data\"></textarea><br></td>";
}
echo "</tr>";
}
?>

This creates a form with field names and text box or textarea box next to
each field name depending on the datatype. After the user enters the
values in the text or textarea and clicks submit, the values should get
stored in a database. But what is happening is that only the value entered
in the last field of the form is getting entered into the database.
This code is embedded in an application which is having an inbuilt
structure of taking the values from a form in a hash. Since key is the
same (i.e. field_id) everytime, the value gets overwritten and only the
last value gets stored in db. But I am not able to work out a solution for
this.
I hope I am able to make my problem clear enough.

Thanks,
Suamya.




-----------------------------------------------------------------------------
DISCLAIMER:-
"The information in this e-mail is confidential, and is intended
solely for the addressee or addressees. If you are not the intended recipient,
please delete the mail and kindly notify the sender of misdelivery. Any
unauthorised use or disclosure of the contents of the mail is not permitted
and may be unlawful."
-----------------------------------------------------------------------------

"Scanned By MailScanner"

  Réponse avec citation
Vieux 14/03/2008, 11h02   #2
Zoltán Németh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Sending multiple values from a form having same fieldnames.

2008. 03. 14, péntek keltezéssel 14.08-kor Suamya Srivastava ezt Ãrta:
> Hi,
>
> How can I send multiple values from a form to be stored in a database, as
> name of the fields is the same?
>
> For example:
>
> <?php
> foreach ($field_data as $field) {
>
> $field_name=$field["field_name"];
> $field_id=$field["field_id"];
> $datatype=$field["datatype_name"];
>
> ?>
> <input type="hidden" name="field_id" value="<?php echo $field_id;?>" />
> <tr>
> <td><strong><?php echo $field_name;?><strong></td>
> <?php
> if ($datatype=="text" || $datatype=="integer") {
> echo "<td><input type=\"text\" name=\"field_data\"></td>";


make field_data an array indexed by field_id

<input type="text" name="field_data[<?php echo $field_id; ?>]"

greets,
Zoltán Németh

> }
> elseif ($datatype=="textarea") {
> echo "<td><textarea rows=\"10\" cols=\"100\"
> name=\"field_data\"></textarea><br></td>";
> }
> echo "</tr>";
> }
> ?>
>
> This creates a form with field names and text box or textarea box next to
> each field name depending on the datatype. After the user enters the
> values in the text or textarea and clicks submit, the values should get
> stored in a database. But what is happening is that only the value entered
> in the last field of the form is getting entered into the database.
> This code is embedded in an application which is having an inbuilt
> structure of taking the values from a form in a hash. Since key is the
> same (i.e. field_id) everytime, the value gets overwritten and only the
> last value gets stored in db. But I am not able to work out a solution for
> this.
> I hope I am able to make my problem clear enough.
>
> Thanks,
> Suamya.
>
>
>
>
> -----------------------------------------------------------------------------
> DISCLAIMER:-
> "The information in this e-mail is confidential, and is intended
> solely for the addressee or addressees. If you are not the intended recipient,
> please delete the mail and kindly notify the sender of misdelivery. Any
> unauthorised use or disclosure of the contents of the mail is not permitted
> and may be unlawful."
> -----------------------------------------------------------------------------
>
> "Scanned By MailScanner"
>
>


  Réponse avec citation
Vieux 15/03/2008, 01h12   #3
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Sending multiple values from a form having same field names.

Eric Butera wrote:
> On Fri, Mar 14, 2008 at 6:02 AM, Zoltán Németh <znemeth@alterationx.hu> wrote:
>> 2008. 03. 14, péntek keltezéssel 14.08-kor Suamya Srivastava ezt Ãrta:
>>
>>> Hi,
>> >
>> > How can I send multiple values from a form to be stored in a database, as
>> > name of the fields is the same?
>> >
>> > For example:
>> >
>> > <?php
>> > foreach ($field_data as $field) {
>> >
>> > $field_name=$field["field_name"];
>> > $field_id=$field["field_id"];
>> > $datatype=$field["datatype_name"];
>> >
>> > ?>
>> > <input type="hidden" name="field_id" value="<?php echo $field_id;?>" />
>> > <tr>
>> > <td><strong><?php echo $field_name;?><strong></td>
>> > <?php
>> > if ($datatype=="text" || $datatype=="integer") {
>> > echo "<td><input type=\"text\" name=\"field_data\"></td>";

>>
>> make field_data an array indexed by field_id
>>
>> <input type="text" name="field_data[<?php echo $field_id; ?>]"
>>
>> greets,
>> Zoltán Németh
>>
>>
>>
>> > }
>> > elseif ($datatype=="textarea") {
>> > echo "<td><textarea rows=\"10\" cols=\"100\"
>> > name=\"field_data\"></textarea><br></td>";
>> > }
>> > echo "</tr>";
>> > }
>> > ?>
>> >
>> > This creates a form with field names and text box or textarea box next to
>> > each field name depending on the datatype. After the user enters the
>> > values in the text or textarea and clicks submit, the values should get
>> > stored in a database. But what is happening is that only the value entered
>> > in the last field of the form is getting entered into the database.
>> > This code is embedded in an application which is having an inbuilt
>> > structure of taking the values from a form in a hash. Since key is the
>> > same (i.e. field_id) everytime, the value gets overwritten and only the
>> > last value gets stored in db. But I am not able to work out a solution for
>> > this.
>> > I hope I am able to make my problem clear enough.
>> >
>> > Thanks,
>> > Suamya.
>> >
>> >
>> >
>> >
>> > -----------------------------------------------------------------------------
>> > DISCLAIMER:-
>> > "The information in this e-mail is confidential, and is intended
>> > solely for the addressee or addressees. If you are not the intended recipient,
>> > please delete the mail and kindly notify the sender of misdelivery. Any
>> > unauthorised use or disclosure of the contents of the mail is not permitted
>> > and may be unlawful."
>> > -----------------------------------------------------------------------------
>> >
>> > "Scanned By MailScanner"
>> >
>> >

>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

>
> Since Zoltán give you the answer I might give you another fish. Which
> one is more readable:
>
> <input type="hidden" name="field_id" value="<?php echo $field_id;?>" />
> <tr>
> <td><strong><?php echo $field_name;?><strong></td>
> <?php
> if ($datatype=="text" || $datatype=="integer") {
> echo "<td><input type=\"text\" name=\"field_data\"></td>";
> }
> elseif ($datatype=="textarea") {
> echo "<td><textarea rows=\"10\" cols=\"100\"
> name=\"field_data\"></textarea><br></td>";
> }
> echo "</tr>";
> }
> ?>
>
>
> <input type="hidden" name="field_id" value="<?php echo
> htmlspecialchars($field_id); ?>" />
> <tr>
> <td><strong><?php echo htmlspecialchars($field_name); ?><strong></td>
>
> <?php if ($datatype=="text" || $datatype=="integer"): ?>
> <td><input type="text" name="field_data"></td>
> <?php elseif ($datatype=="textarea"): ?>
> <td>
> <textarea rows="10" cols="100" name="field_data"></textarea>
> <br>
> </td>
> <?php endif; ?>
>
> </tr>


This is more readable

<?php
while ( $row = mysql_fetch_row($result_set) ) {

# Extract all data fields from result set
list($datatype,$field_id,$field_name,etc...) = $row;

# Initialize or clear variable
$field = '';

# Check to see if it requires a text field
if ( $datatype == "text" || $datatype == "integer" ) {

# Create Text field
$field = '<input type="text" name="field_data[{$field_id}]" />';

# Check to see if it requires a text area
} elseif ( $datatype == "textarea" ) {

# Create Text Area
$field = '<textarea rows="10" cols="100" name="field_data[{$field_id}]">'.
'</textarea>';

}
# Display it all
echo <<<ROW
<tr>
<td><strong>{$field_name}</strong></td>
<td>{$field}</td>
</tr>
ROW;
}
?>


--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Réponse avec citation
Vieux 20/03/2008, 06h41   #4
Suamya Srivastava
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Sending multiple values from a form having same fieldnames.

Hi,

Thanks all for the .

I suffixed all the field names in the form with field id, like
name=field_name_fieldid and fetched it as an associative array.

Thanks,
Suamya

> Eric Butera wrote:
>> On Fri, Mar 14, 2008 at 6:02 AM, Zoltán Németh
>> <znemeth@alterationx.hu> wrote:
>>> 2008. 03. 14, péntek keltezéssel 14.08-kor Suamya Srivastava ezt
>>> Ãrta:
>>>
>>>> Hi,
>>> >
>>> > How can I send multiple values from a form to be stored in a
>>> database, as
>>> > name of the fields is the same?
>>> >
>>> > For example:
>>> >
>>> > <?php
>>> > foreach ($field_data as $field) {
>>> >
>>> > $field_name=$field["field_name"];
>>> > $field_id=$field["field_id"];
>>> > $datatype=$field["datatype_name"];
>>> >
>>> > ?>
>>> > <input type="hidden" name="field_id" value="<?php echo $field_id;?>"
>>> />
>>> > <tr>
>>> > <td><strong><?php echo $field_name;?><strong></td>
>>> > <?php
>>> > if ($datatype=="text" || $datatype=="integer") {
>>> > echo "<td><input type=\"text\"
>>> name=\"field_data\"></td>";
>>>
>>> make field_data an array indexed by field_id
>>>
>>> <input type="text" name="field_data[<?php echo $field_id; ?>]"
>>>
>>> greets,
>>> Zoltán Németh
>>>
>>>
>>>
>>> > }
>>> > elseif ($datatype=="textarea") {
>>> > echo "<td><textarea rows=\"10\" cols=\"100\"
>>> > name=\"field_data\"></textarea><br></td>";
>>> > }
>>> > echo "</tr>";
>>> > }
>>> > ?>
>>> >
>>> > This creates a form with field names and text box or textarea box
>>> next to
>>> > each field name depending on the datatype. After the user enters the
>>> > values in the text or textarea and clicks submit, the values should
>>> get
>>> > stored in a database. But what is happening is that only the value
>>> entered
>>> > in the last field of the form is getting entered into the database.
>>> > This code is embedded in an application which is having an inbuilt
>>> > structure of taking the values from a form in a hash. Since key is
>>> the
>>> > same (i.e. field_id) everytime, the value gets overwritten and only
>>> the
>>> > last value gets stored in db. But I am not able to work out a
>>> solution for
>>> > this.
>>> > I hope I am able to make my problem clear enough.
>>> >
>>> > Thanks,
>>> > Suamya.
>>> >
>>> >
>>> >
>>> >
>>> > -----------------------------------------------------------------------------
>>> > DISCLAIMER:-
>>> > "The information in this e-mail is confidential, and is
>>> intended
>>> > solely for the addressee or addressees. If you are not the intended
>>> recipient,
>>> > please delete the mail and kindly notify the sender of misdelivery.
>>> Any
>>> > unauthorised use or disclosure of the contents of the mail is not
>>> permitted
>>> > and may be unlawful."
>>> > -----------------------------------------------------------------------------
>>> >
>>> > "Scanned By MailScanner"
>>> >
>>> >
>>>
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>

>>
>> Since Zoltán give you the answer I might give you another fish. Which
>> one is more readable:
>>
>> <input type="hidden" name="field_id" value="<?php echo $field_id;?>" />
>> <tr>
>> <td><strong><?php echo $field_name;?><strong></td>
>> <?php
>> if ($datatype=="text" || $datatype=="integer") {
>> echo "<td><input type=\"text\" name=\"field_data\"></td>";
>> }
>> elseif ($datatype=="textarea") {
>> echo "<td><textarea rows=\"10\" cols=\"100\"
>> name=\"field_data\"></textarea><br></td>";
>> }
>> echo "</tr>";
>> }
>> ?>
>>
>>
>> <input type="hidden" name="field_id" value="<?php echo
>> htmlspecialchars($field_id); ?>" />
>> <tr>
>> <td><strong><?php echo htmlspecialchars($field_name); ?><strong></td>
>>
>> <?php if ($datatype=="text" || $datatype=="integer"): ?>
>> <td><input type="text" name="field_data"></td>
>> <?php elseif ($datatype=="textarea"): ?>
>> <td>
>> <textarea rows="10" cols="100" name="field_data"></textarea>
>> <br>
>> </td>
>> <?php endif; ?>
>>
>> </tr>

>
> This is more readable
>
> <?php
> while ( $row = mysql_fetch_row($result_set) ) {
>
> # Extract all data fields from result set
> list($datatype,$field_id,$field_name,etc...) = $row;
>
> # Initialize or clear variable
> $field = '';
>
> # Check to see if it requires a text field
> if ( $datatype == "text" || $datatype == "integer" ) {
>
> # Create Text field
> $field = '<input type="text" name="field_data[{$field_id}]" />';
>
> # Check to see if it requires a text area
> } elseif ( $datatype == "textarea" ) {
>
> # Create Text Area
> $field = '<textarea rows="10" cols="100"
> name="field_data[{$field_id}]">'.
> '</textarea>';
>
> }
> # Display it all
> echo <<<ROW
> <tr>
> <td><strong>{$field_name}</strong></td>
> <td>{$field}</td>
> </tr>
> ROW;
> }
> ?>
>
>
> --
> Jim Lucas
>
> "Some men are born to greatness, some achieve greatness,
> and some have greatness thrust upon them."
>
> Twelfth Night, Act II, Scene V
> by William Shakespeare
>
>
> -----------------------------------------------------------------------------
> DISCLAIMER:-
> "The information in this e-mail is confidential, and is intended
> solely for the addressee or addressees. If you are not the intended
> recipient,
> please delete the mail and kindly notify the sender of misdelivery. Any
> unauthorised use or disclosure of the contents of the mail is not
> permitted
> and may be unlawful."
> -----------------------------------------------------------------------------
>
> "Scanned By MailScanner"
>
>




-----------------------------------------------------------------------------
DISCLAIMER:-
"The information in this e-mail is confidential, and is intended
solely for the addressee or addressees. If you are not the intended recipient,
please delete the mail and kindly notify the sender of misdelivery. Any
unauthorised use or disclosure of the contents of the mail is not permitted
and may be unlawful."
-----------------------------------------------------------------------------

"Scanned By MailScanner"

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21239 seconds with 12 queries