+ Reply to Thread
Results 1 to 3 of 3

Thread: How to handle dynamic input fields in post method using Php

  1. #1
    oceanwap's Avatar
    oceanwap is offline x10Hosting Member oceanwap is an unknown quantity at this point
    Join Date
    Nov 2009
    Location
    India
    Posts
    24

    How to handle dynamic input fields in post method using Php

    I have created a script to allow user to input text inside a form using post method having user defined no of fields.
    Here is the code:-
    PHP Code:
    if(isset($_get['url_no']))
    {
    <
    form name="myform" action="'.$_SERVER['PHP_SELF'].'" method="post">
    $counter 1;
    $url_no 1;
    while (
    $url_no <= $_GET['url_no'])
    {
    echo 
    'title:<input type = "text" name="title'.$counter.'" ><br/>
    link:<input type = "text" name="url'
    .$counter.'" ><br/>
    '
    ;
    $url_no++;
    $counter++;
    }
    <
    input type="submit" name="submit">
    </
    form>

    Now how I can process this form and generate set of anchor tags with user supplied title and url.

  2. #2
    masshuu's Avatar
    masshuu is offline Head of the Geese masshuu has a spectacular aura about
    Join Date
    Oct 2007
    Location
    Las Colinas, Tx
    Posts
    2,262

    Re: How to handle dynamic input fields in post method using Php

    i would say, make a hidden input, give it $_GET['url_no']
    then when posted, check the hidden feild, and just use something like
    PHP Code:
    $Count $_POST['hiddenCount'];
    $Data "Your Links: <br />";
    for(
    $i 0$i $Count$i++)
    {
       
    $Data .= '<a href="' $_POST['url'.$i] . '>' .  $_POST['title'.$i] . '</a><br />';

    alternatively, you could do:
    PHP Code:
    $i 0;
    $Data "Your Links: <br />";
    while(isset( 
    $_POST['url'.$i]))
    {
       
    $Data .= '<a href="' $_POST['url'.$i] . '>' .  $_POST['title'.$i] . '</a><br />';
       
    $i++;

    which doesn't need to have a hidden value with the count.
    Last edited by masshuu; 06-12-2010 at 02:21 AM.
    Just leading the flock.
    Livewire
    Masshuu ------ carl6969
    descalzo ------------------- Smith6612
    Bryon--------------------------------- Corey
    If you find any post helpful or useful, duck
    \ / This for that post and rep it up.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: How to handle dynamic input fields in post method using Php

    Use array syntax in the form input names to get the input as arrays:
    PHP Code:
    <?php
    for ($i=0$i $_GET['url_no']; ++$i) {
      
    ?><label for="title_<?php echo $i ?>">Title:</label><input name="title[]" id="title_<?php echo $i ?>" />
      <label for="url_<?php echo $i ?>">Link:</label><input name="url[]" id="url_<?php echo $i ?>" />
      <?php
    }
    You can use count or foreach in your loop over input elements.
    PHP Code:
    for ($i=0$i count($_POST['title']); ++$i) {
       ... 
    $_POST['title'][$i] ...
       ... 
    $_POST['url'][$i] ...

    Last edited by misson; 06-12-2010 at 04:58 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. in IE input password is displayed shorter than input text
    By manoogim in forum Graphics & Webdesign
    Replies: 1
    Last Post: 11-05-2009, 05:23 PM
  2. Max FTP connection that x10hosting can handle?
    By YCadmin in forum Free Hosting
    Replies: 2
    Last Post: 12-25-2008, 12:40 AM
  3. How Many Visitors Can My server handle at once?
    By ashwinsinha in forum Free Hosting
    Replies: 1
    Last Post: 10-05-2008, 01:04 PM
  4. Ads in Inline Fields?
    By Zdroyd in forum Free Hosting
    Replies: 1
    Last Post: 03-10-2008, 01:46 AM
  5. Can x10 handle my site?
    By Tupac in forum Free Hosting
    Replies: 5
    Last Post: 06-02-2007, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers