SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array and form generation

    I am trying to generate a form dynamically.

    The way it works is the user specifies the number on text inputs they want, hit go and it creates them. The next stage is to then label them, and the next stage displays the code for them/finishes.

    I came up with a really hit n miss solution to this, and I am sure there is a better way to do it, using an array of some sort. I figured some stuff out, but I was having issues labelling the fields in the second part of the process based on the form submission previously...

    AT the moment it is something like this (not actual code, just written now off the top of my head based on my hitnmiss solution):

    Stage 1: Get number of required fields
    Code:
    <form action=$PHP_SELF>
    <INPUT TYPE="text" NAME="numbertextinputs">
    <INPUT TYPE="submit" NAME="stage2">
    </FORM>
    Stage 2: Generate that many inputs fields to be labelled
    PHP Code:
    <FORM ACTION="$PHP_SELF">
    <?php
    if ($stage2) {

    for ($=
    1;$i<$numbertextinputs;$i++) {

    echo(
    "<INPUT TYPE=\"text\" NAME=\"input_$i\">");
    }
    ?>
    <INPUT TYPE="submit" NAME="stage3">
    </FORM>
    Stage 3: Label them creating some sort of code...

    PHP Code:
    if($stage3) {

    ...

    I know this is nasty (and doesn't work). So can someone give me a headsup and show me how to create an array for the stage 2 (based on the requested number of inputs from stage 1) and then create the HTML code with the fields named appropriately (stage 3), based on the results of Stage 2.

    Ta.
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  2. #2
    SitePoint Guru
    Join Date
    Dec 2000
    Location
    Karachi, Pakistan
    Posts
    913
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Weird ,

    Well, when you display the number of input boxes..

    PHP Code:
    <?php
    if ($stage2) {

    for (
    $i=1;$i<$numbertextinputs;$i++) {

    echo(
    "<INPUT TYPE=\"text\" NAME=\"input_$i\">");
    }
    ?>
    add onething right after echo'ing the text box and that is keep adding the values of 'i'

    Here is what I mean:

    PHP Code:
    <?php
    if ($stage2) {
    $mynumber 0;
    for (
    $i=1;$i<$numbertextinputs;$i++) {

    echo(
    "<INPUT TYPE=\"text\" NAME=\"input_$i\">");
    $mynumber $mynumber $i;
    }
    ?>
    And then when you want to display the value of the text input boxes, you can do this:

    PHP Code:
    <?php
    if ($stage2) {
    $mynumber2 $HTTP_POST_VARS['mynumber'];
    for (
    $i=1;$i<$mynumber2;$i++) {
    echo(
    "Input Box $i$HTTP_POST_VARS['input_$i']");
    }
    ?>

    I hope that was the thing you needed.. Well, if it ain't then sorry... because I know a very little of arrays..


    Salam,
    -Omair
    36Host.com - $36/year web hosting [affiliates earn 30%]
    * Affordable Small Business Web Hosting since 2003! *
    "500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
    mysql, pre-installed php scripts, 24/7 support & more...."



  3. #3
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    I hope this is what you wanted:
    The first code will display in step 2 another form where the user has to enter labels for the formfields.

    PHP Code:
    if (!isset($HTTP_POST_VARS['step']))
    {
        echo 
    '<form method="post" action='.$PHP_SELF.'>';
        echo 
    'How many input fields: <input type="text" name="total">';
        echo 
    '<input type="hidden" name="step" value="2">';
        echo 
    '<input type="submit" name="submit">';
        echo 
    '</form>';
    }

    elseif (
    $HTTP_POST_VARS['step'] == 2)
    {
        echo 
    '<form method="post" action='.$PHP_SELF.'>';

        for (
    $i 0$i < (int)$HTTP_POST_VARS['total']; $i++)
            echo 
    'Name for formfield'.($i+1).': <input type="text" name="desc[]"><br>';

        echo 
    '<input type="submit" name="submit">';
        echo 
    '<input type="hidden" name="step" value="3">';
        echo 
    '</form>';
    }

    elseif (
    $HTTP_POST_VARS['step'] == 3) {
        echo 
    "<pre>";

        for (
    $i 0$i count($HTTP_POST_VARS['desc']); $i++)
           echo 
    'Textfield named: '.$HTTP_POST_VARS['desc'][$i].': '.htmlspecialchars("<input type=\"text\" name=\"".$HTTP_POST_VARS['desc'][$i]."\">").'<br>';

        echo 
    "</pre>";

    If you want label like "label_1" to "label_n" maybe this helps:

    PHP Code:
    if (!isset($HTTP_POST_VARS['step']))
    {
        echo 
    '<form method="post" action='.$PHP_SELF.'>';
        echo 
    'How many input fields: <input type="text" name="total">';
        echo 
    '<input type="hidden" name="step" value="2">';
        echo 
    '<input type="submit" name="submit">';
        echo 
    '</form>';
    }

    elseif (
    $HTTP_POST_VARS['step'] == 2)
    {
        echo 
    "<pre>";

        for (
    $i 0$i < (int)$HTTP_POST_VARS['total']; $i++)
           echo 
    htmlspecialchars("<input type=\"text\" name=\"label_$i\">").'<br>';

        echo 
    "</pre>";

    Both functions will display the HTML code for the input fields. If you remove the htmlspecialchars(...) then the input fields will show up.

  4. #4
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks you two, sorry for being late replying but I have been having modem problems.

    Anyway, I will be trying your ideas probably tomorrow, so I will let you know!

    Thanks.
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  5. #5
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK... i tried... and the second works perfectly.

    But I am a little confused by a couple of things, if you could perhaps explain them for me:

    1)

    PHP Code:
    echo 'Name for formfield'.($i+1).': <input type="text" name="desc[]"><br>'
    What is the desc[]? Where does that come from?

    2) Why do you put .'s before $PHP_SELF?
    In fact you seem to put it before and after all your variables?

    3)

    PHP Code:
    '.$HTTP_POST_VARS['desc'][$i].' 
    How does this work (i.e. the [$i] after H_P_V['desc'] etc.

    Apart from that though, thanks a lot!
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    1.) + 3.) "desc[]" is just a name and is used to put all values from the input boxes into one array after the form has been submitted. This is IMO easier to access.

    In the step where the form has already been submitted:
    Now you can access each element of the array by a number.

    $HTTP_POST_VARS['desc'] is the array name and "[$i]" is used to access each element of the array.

    $HTTP_POST_VARS['desc'][2] would access the third element of the array $HTTP_POST_VARS['desc']

    the contents of $HTTP_POST_VARS['desc'] could look like this:

    PHP Code:
    Array
    (
        [
    0] => wert
        
    [1] => tret
        
    [2] => asd
        
    [3] => data1
        
    [4] => werttrew


    2.) I sometimes have a strange coding style.
    echo '$variable' will display $variable without processing it.
    Since I used echo '...' i had to escape it to properly display the value of the variables and not just the variable name.
    You could use echo "<form action='$PHP_SELF' method='POST'> as well.

    Hope this helps.

    --Chris

  7. #7
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Chris82,

    I have a problem! I am using your code but want to do something with it: I am using this:

    PHP Code:
    for ($i 0$i < (int)$HTTP_POST_VARS['total']; $i++){
           
           echo(
    "<TR><TD>?</TD><TD COLSPAN=\"2\">Name for INPUT field ". ($i+1) .": <input type=\"text\" name=\"desc[]\"><INPUT TYPE=\"radio\" CLASS=\"radio\" onFocus=\"desc[].value='Email';\">Email</TD></TR>");


    Notice the radio input -- basically on the onClick I want a string to be input into the input field.

    this current code gives me a syntax error. If I change the name of the field to something else (for example $i) it works fine, but the next stage of the operation fails, since it is no longer called desc[].

    What can I do? Thanks!
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand, you can't assign a value to a radio button like that can you?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  9. #9
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, this is the problem. It gives a syntax error. So what I would like to know is:

    How can I assign a value to the radio button, but still have the values passed through to stage three, of the x number of inputs? It is to do with $HTTP_POST_VARS which I am not very familiar with which is causing me the problems.

    From the code that Chris posted, the desc[] in the INPUT stuff, defines an array called desc, then the elements of which are called using $HTTP_POST_VARS['arrayname'][placeinarray], in this case ['desc'][$i] -- how do I modify it?
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  10. #10
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This now works:

    PHP Code:
    <?php } else {

       for (
    $i 0$i < (int)$HTTP_POST_VARS['total']; $i++){
           
           echo(
    "<TR><TD>?</TD><TD COLSPAN=\"2\">Name for INPUT field ". ($i+1) .": <input type=\"text\" name=\"desc".($i+1)."\"><INPUT TYPE=\"radio\" CLASS=\"radio\" onFocus=\"desc".($i+1).".value='Email'\">Email</TD></TR>");

    }
    echo(
    "<INPUT TYPE=\"hidden\" NAME=\"numFields\" VALUE=\"".$i."\">");




    include(
    "submit.inc");
    }
    }

    elseif (
    $HTTP_POST_VARS['step'] == 3) {

    $number=(int)$HTTP_POST_VARS['numFields'];

    echo (
    "<TR><TD>&nbsp;<TD COLSPAN=\"2\">$number fields created!</TD></TR>");
    echo(
    "<TR><TD>&nbsp;</TD><TD COLSPAN=\"2\">");

        for (
    $i 0$i < ($number); $i++) 
           echo 
    'Text field named: '.$HTTP_POST_VARS['desc'.($i+1)].'<BR>'

    echo(
    "</TD></TR>");
    include(
    "submit.inc");
    }
    ?>
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky


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
  •