www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] form, javascript array to php

Hybrid View

  1. #1
    Join Date
    Jun 2010
    Posts
    14

    resolved [RESOLVED] form, javascript array to php

    Hello,

    I have to make a form, where a part of a form is repeated n-times e.g.:
    previous education: text field
    school: text field
    degree: text field
    add to form (link)

    and as a user clicks on link, the entry is added to javascript array[x][y].

    At the end of the form, user clicks button and
    all stuff is POST sent to other php page to be processed
    and stored in mysql database.

    Now, this is how I see it, and I have been wrong before,
    so I would be grateful for tips on how to do this repeating part...

    Basically, in form user puts hers/his input,
    some part of the form has to be repeated n-times
    (as much as user needs), and than POST sent to another php page.

    Thank you

  2. #2
    Join Date
    Apr 2010
    Location
    Cocoa Beach, FL
    Posts
    41

    One option for form element n-times

    There is a quick way you can process n-times form elements as part of Web Application Development.

    However it requires that form fields have data entered into them, which you can enforce using Javascript.

    In HTML if you have two or more form elements with the same name, it builds a list of data from the form elements. Here is a short example.

    <form method="post">
    <input type="text" name="firstname" value="" />
    <input type="text" name="firstname" value="" />
    <input type="submit" value="submit" />
    </form>

    If you put 'Name One' in the first element, and 'Name Two' in the second element, the value of the form element after submit will be "Name One, Name Two". When you have multiple form elements with the same name, the data from those form elements is combined into a list during the form submit.

    This can be very useful for forms which have a dynamic number of elements. You can use loops to work through the data of the elements, where you use the comma , as the delimiter of the data in the list.

    Another option is to add a hidden form element where the value is the number N for the N number of elements. And each element has a different name. When you submit the form your server side code will get the hidden element and its value and that way you can know the number of N elements after the submit. For example the following:


    <form method="post">
    <input type="text" name="firstname1" value="" />
    <input type="text" name="firstname2" value="" />
    <input type="hidden" name="Count" value="2">
    <input type="submit" value="submit" />
    </form>

    After the form submit you know the number of elements from the value of the COUNT form element. You can then use that accordingly to process your form elements with loops and other methods, for the server side platform you are using, such as ASP.NET, ColdFusion, or PHP.

    Michael G. Workman
    michael.g.workman@gmail.com

  3. #3
    Join Date
    Jun 2010
    Posts
    14
    Hello Michael_G and thank you for your answer.

    I tried both yours suggestions.
    I must admit, first one I did not get to work,
    but second one, with hidden fields, that did the trick.

    So I went with hidden fields.

    Your input is much appreciated,
    since I thought I hit the wall...

    Thank you again so much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles