SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    read multiple variables with same name

    If you have a form with some fields like textareas etc:

    Code:
    <p><label for="paragraph">Paragraph: </label><textarea name="paragraph" id="paragraph"></textarea></p>
    ... and you have a javascript that lets you add inputfields and textareas (with the same name and id's) when you click on a link to make that happen,

    Is there a possibility in php to read that form out with those new javascript created formfields, like:

    Code:
    $paragraph = $_POST['paragraph'] ;
    So if you create more than one paragraph with the same id's and names, how can you do that?
    Is it possible with something like that?

    Code:
    $amountPar = count('$_POST['paragraph']');
    for(i=0; i<$amountPar; i++){
    $paragraph[i] = $_POST['paragraph'];
    }

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lets use array names in your textarea name tag:
    Code:
    <textarea name="paragraph[]"
    And in processing program:
    PHP Code:
    <?php
    if ( isset( $_POST["paragraph"] ) AND count$_POST["paragraph"] > ) ) {
        foreach( 
    $_POST["paragraph"] AS $paragraph )
        echo 
    $paragraph "<br />";


    ?>

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    If you end the HTML form name with [] then PHP will allow you to access them using array notation.

    Idendifiers must be unique though, so you will need to ensure that those don't repeat.

    Code html4strict:
    <p>
        <label for="paragraph1">Paragraph: </label>
        <textarea name="paragraph[]" id="paragraph1"></textarea>
    </p>

    Code php:
    $paragraph = $_POST['paragraph'];
    // because form was "paragraph[]", $paragraph is now an array
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post

    Idendifiers must be unique though, so you will need to ensure that those don't repeat.
    Hmm, not possible in my case. My php script won't know beforehand what the names of my identifiers will be. Well, only just one name for that variable. So I'll try to make that with an array aswell.
    I'll try it this evening. Thanks!

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one name, but that could be an array variable name. using the [] 's


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •