www.webdeveloper.com
Results 1 to 4 of 4

Thread: Loop Button and Action

Hybrid View

  1. #1
    Join Date
    Jul 2007
    Posts
    76

    Loop Button and Action

    Hi All,

    Apologies, I dont know whether if this is more a Javascript or PHP query, but I am a bit stuck with getting a form button and action to work separately on each line of my PHP loop.

    Currently the first button works, but if you decide to click a button on another line the first field is populated and not on the line of the button pressed.

    You can see the page in question here: http://www.jadeboylan.com/game/post.php

    The code here:

    <?php

    $funds = $_POST['prcbox'];
    $ageval = $_POST['age'];

    echo "You have $funds to spend!";

    ?>

    <input type="hidden" id="age" name="age" value="$ageval">


    <?php

    fgetcsv_PHP();

    function fgetcsv_PHP()
    {
    /*
    * See if we can open a file named fgetcsv.csv in
    * read mode, if we can then assign pointer to this
    * file to a variable named $handle
    * 'r' - Open for reading only; place the file
    * pointer at the beginning of the file.
    */
    if (($handle = fopen("./assets/actors.csv", "r")) !== FALSE)
    {
    /*
    * fgetcsv( resource $handle int $length string $delimiter )
    *
    * resource $handle
    * A valid file pointer to a file successfully opened by fopen(),
    * popen(), or fsockopen().
    *
    * int $length
    * Must be greater than the longest line (in characters) to be
    * found in the CSV file (allowing for trailing line-end characters).
    * It became optional in PHP 5. Omitting this parameter (or setting
    * it to 0 in PHP 5.0.4 and later) the maximum line length is not
    * limited, which is slightly slower.
    *
    * string $delimiter
    * Set the field delimiter (one character only).
    *
    * RETURN VALUES
    *
    * Returns an indexed array containing the fields read.
    *
    * Note: A blank line in a CSV file will be returned as an array
    * comprising a single null field, and will not be treated
    * as an error.
    *
    * Note: If PHP is not properly recognizing the line endings when
    * reading files either on or created by a Macintosh computer,
    * enabling the auto_detect_line_endings run-time configuration
    * option may help resolve the problem.
    *
    * fgetcsv() returns NULL if an invalid handle is supplied or FALSE
    * on other errors, and when the end of file has been reached.
    */


    $length = 1000;
    $delimiter = ",";

    /*
    * Print the opening table tag to begin buiding HTML table
    * and the first row of the table; with column names
    */
    echo "<table>\n";
    echo "<td>&nbsp;</td><td width='90px'><b>Name</b></td><td><b>Surname</b></td>";

    /*
    * Loop through the array of values returned by fgetcsv until there are
    * no more lines (indicated by FALSE)
    */
    while ( ( $data = fgetcsv( $handle, $length, $delimiter ) ) !== FALSE )
    {
    // Count number of array elements in $data
    $num = count($data);
    // Print opening table row HTML tag
    echo "<tr>\n";
    echo "<td width='90px'><button onclick='myApproach();'>Approach</button></td>\n";

    /*
    * Loop through the $data array and output each element
    * wrapped by opening and closing table data HTML tags
    */
    for ($c=0; $c < $num; $c++)
    {
    echo "<td>".$data[$c]."</td>\n";
    }

    echo "<td width='90px'><textarea id='agebox' style='width:70px;' readonly='readonly' onchange='submitChange()'></textarea></td>\n";

    echo "<td width='90px'><input type='submit' id='submit' style='display: none;' /></td>\n";





    // Print closing table row HTML tag
    echo "</tr>\n";
    }

    // Print close table HTML tag
    echo "</table>";

    // Close the file pointed to by $handle
    fclose($handle);
    }
    }

    ?>

    <script>
    function myApproach() {
    var age,voteable;
    age=document.getElementById("age").value;
    voteable=(age<18)?"Too young":"Old enough";
    document.getElementById("agebox").value=voteable;
    }

    function submitChange()
    {
    var inputOne = document.getElementById("agebox");
    var inputSubmit = document.getElementById("submit");
    if(inputOne.value == "Old enough")
    {
    inputSubmit.style.display = "block";
    }
    else
    {
    inputSubmit.style.display = "none";
    }
    }

    </script>

    Any help would be greatly appreciated!

  2. #2
    Join Date
    Mar 2009
    Posts
    452
    as you are using same id for input field, so when you try to access input field using document.getElementById("age") the very first matched element is returned.

    what you need to do is to assign different id to the input filed on each line. for example: age1, age2, age3 etc...

  3. #3
    Join Date
    Jul 2007
    Posts
    76
    sorry im still struggling with this i understand I have to have an array of ids on the id='agebox' input field

    but i dont understand why you say to have an array on the input with the id='age'??

    that is just a single field holding a hidden value which the function im trying to loop gains its data from.

  4. #4
    Join Date
    Mar 2009
    Posts
    452


    actually when you try to getElementById('age') it returns the first element in the document that has id="age"

    when you loop it through PHP id is same for all the rows (view source), so it just picks up the first element not 2nd, 3rd...

Thread Information

Users Browsing this Thread

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

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