tags:

views:

54

answers:

3

Hello all,

I design a form that contains dynamic contents.

For example, The form has a textfield for Paper_Name and a textfield for Paper_Link. The user can add up to ten paper names and paper links.

The id for each paper name and paper link is as follows:

papername01, paperlink01, ..., papername10, paperlink10.

Those ids are dynamically created by JavaScript. In other words, if the user doesn't click Add Paper button and submit the form, the only field that will be received by the server is paperlink01, and papername01.

Now, the question what is a better practice to design the server PHP script to handle those submitted form content.

1> Use aJax form plugin? 2> Use regular form submission and iterate through all variables and see whether any of them is set? 3> use JSON data format?

Yes, the question is related to both server and client. I would like to know the best practice and change my client accordingly. Please help and thank you

The form design looks like the following: < div id="divDummy001"> < div id="paperTitle001Fld"> < label for="papertitle001">Title of Paper 001< /label > < div> < input type="text" size="16" value="" id="papertitle001" name="papertitle001" /> < /div > < /div >

 < div id="paperURL001Fld" >
  < label for="paperurl001">Paper Link 001< /label >
  < div>
   < input type="text" size="96" value="" id="paperurl001" name="paperurl001" />
   < /div >
 < /div >
< /div >

When the user clicks Add button, a new div will be created with the only difference is that the ID will be increased by 1.

+2  A: 

You could make the JS name the elements with []. See http://www.spsu.edu/cs/faculty/bbrown/papers/php2.html for an explanation of how it's normally done:

<form method="post">
I speak the following languages:
<input type="checkbox" name="lang[ ]" value="en">English<br />
<input type="checkbox" name="lang[ ]" value="fr">Français<br />
<input type="checkbox" name="lang[ ]" value="es">Español<br />
</form>

In the receiving code:

<?php
$langs = $_POST['lang'];
// Note that $snacks will be an array.
foreach ($langs as $lang) {
  echo "$lang<br />";
}
?>

You could also name text inputs that way, using <input type="text" name="papername[]"> several times. Then you can process the names as an array.

Beware, though: ID's must be unique, names don't have to. You should get HTML like

<input type="text" id="papername01" name="papername[]">
<input type="text" id="papername02" name="papername[]">
[etc]
MvanGeest
+2  A: 

Use arrays. Create textfields with names like papername[1], papername[2] then they'll appear as arrays in PHP script.

Mchl
Hello Mchl,If I use papername[1] for my first paper name, do I have problem to use the foreach function? since it starts with 0??thank you
q0987
No you don't. foreach starts with first element in array. Doesn't care what key it has.
Mchl
+3  A: 

Why don't you post an array?

Do a vanilla form submission, then on the server side, you can iterate through the POSTed array like any other PHP array.

All you gotta do is define your HTML elements' name attribute with a [] appended:

<input type="text" name="papername[]" value=""/>

Your PHP should look something like this:

<?php
    if(isset($_POST['papername'])) {
        foreach($_POST['papername'] as $papername) {
           //do something with $papername...
        }
    }
?>
Jacob Relkin
Hello Jacob,Thank you for the comments.If I understood correctly, by using papername[], I will lose the order submitted by the user. I need to keep the difference between different papers.
q0987
@q0987, you can place keys inside the array, like `papername[1]`, then later `papername[2]`, etc. This can allow you to preserve the original keys, thus the original ordering.
Charles