get data from a dynamic form

Discussion in 'PHP' started by dourvas, Jan 18, 2009.

  1. #1
    hallo,

    i ve created a dynamic form. A user can select The number of fields he d like to fill in (test creation)

    echo '<form method="post" action="createxml.php">';
    for ($i =1; $i <=$number; $i+=1){ // THE $NUMBER CONTAINS VALUE -ok-

    echo '<fieldset width ="20%">';
    echo '<legend class = "login">WRITE A QUESTION</legend>';
    echo ' <blockquote align="center">';
    echo 'QUESTION '.$i.' (DESCRIPTION):<br>';
    echo '<textarea name="q".$i rows="3" cols="50"></textarea><br><br>';
    echo 'ANSWEAR 1:<textarea name="q".$i."a1" rows="1" cols="50"></textarea><br>';
    echo 'ANSWEAR 2:<textarea name="q".$i."a2" rows="1" cols="50"></textarea><br>';
    echo 'ANSWEAR 3:<textarea name="q".$i."a3" rows="1" cols="50"></textarea><br>';
    echo 'ANSWEAR 4:<textarea name="q".$i.a4" rows="1" cols="50"></textarea><br>';

    echo '<input type="submit">';
    echo '</form>';

    Now in createxml.php i want to get the values in order to write them inside xml file. i have tried a lot but i can not do it. the last code i used which caused unfinishing loop with empty values was this

    echo "$number";//ok
    for ($i =1; $i <=$number; $i+=1){
    $q.$i = $_POST['q'.$i];
    echo "$q.$i";// empty
    echo "<br>";

    $a.$i= $_POST["q".$i."a1"];
    $a.$i= $_POST["q".$i."a2"];
    $a.$i= $_POST["q".$i."a3"];
    $a.$i= $_POST["q".$i."a4"];

    echo "answear=".$_POST["q".$i."a4"];// empty
    echo "<br>";

    }

    can u help me??????
     
    dourvas, Jan 18, 2009 IP
  2. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is due to $i is not converted to its actual value because it's enclose in single quote.
    Try rewrite your form to

    
    echo '<form method="post" action="createxml.php">';
    for ($i =1; $i <=$number; $i+=1){ // THE $NUMBER CONTAINS VALUE -ok-
        echo '<fieldset width ="20%">';
        echo '<legend class = "login">WRITE A QUESTION</legend>';
        echo ' <blockquote align="center">';
        echo 'QUESTION '.$i.' (DESCRIPTION):<br>';
        echo '<textarea name="q' . $i . '" rows="3" cols="50"></textarea><br><br>';
        echo 'ANSWEAR 1:<textarea name="q' . $i . 'a1" rows="1" cols="50"></textarea><br>';
        echo 'ANSWEAR 2:<textarea name="q' . $i . '"a2" rows="1" cols="50"></textarea><br>';
        echo 'ANSWEAR 3:<textarea name="q' . $i . '"a3" rows="1" cols="50"></textarea><br>';
        echo 'ANSWEAR 4:<textarea name="q' . $i . '"a4" rows="1" cols="50"></textarea><br>';
    
        echo '<input type="submit">';
        echo '</form>';
    }
    
    PHP:
     
    artiskool, Jan 18, 2009 IP
  3. dourvas

    dourvas Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks for the quick answear.

    i saw what u ment and i chnged the code to yours.

    i am having problems to post this values. here is my code

    the form
    
    
    echo '<form method="post" action="createxml.php">';
    for ($i =1; $i <=$number; $i+=1){
    
    echo '<fieldset width ="20%">';
    echo   '<legend class = "login"></legend>';
    echo '   <blockquote align="center">';
    echo 'Questions '.$i.' (description):<br>';
    echo '<textarea name="q' . $i . '" rows="3" cols="50"></textarea><br><br>';
    echo 'answear 1:<textarea name="q' . $i . 'a1" rows="1" cols="50"></textarea><br>';
    echo 'answear 2:<textarea name="q' . $i . 'a2" rows="1" cols="50"></textarea><br>';
    echo 'answear 3:<textarea name="q' . $i . 'a3" rows="1" cols="50"></textarea><br>';
    echo 'answear 4:<textarea name="q' . $i . 'a4" rows="1" cols="50"></textarea><br>';
    echo ' </blockquote>';
    echo '</fieldset>';
    }
    echo '<input type="submit">';
    echo '</form>';
    
    the createxml.php
    
    for ($i =1; $i <=$number; $i+=1){
            $temp="q' . $i . '";
           $q[$i] = $_POST[$temp];
           echo  $q[$i];// empty
           echo "<br>";
                 
           $qa1[$i]= $_POST["q' . $i . 'a1"];
           $qa2[$i]= $_POST["q' . $i . 'a2"];
           $qa3[$i]= $_POST["q' . $i . 'a3"];
           $qa4[$i]= $_POST["q' . $i . 'a4"];
           
           echo "answear=".$qa4[$i]; //empty
            echo "<br>";
    
        }
    
    Code (markup):
    whats wrong???
     
    dourvas, Jan 18, 2009 IP
  4. dourvas

    dourvas Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Anyone.. please help me i am seriusly stuck. an idea would help
     
    dourvas, Jan 18, 2009 IP
  5. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What you are trying to access in this code?

    
    $qa1[$i]= $_POST["q{$i}a1"];
    $qa2[$i]= $_POST["q{$i}a2"];
    $qa3[$i]= $_POST["q{$i}a3"];
    $qa4[$i]= $_POST["q{$i}a4"];
    
    PHP:
     
    artiskool, Jan 18, 2009 IP
  6. bigrollerdave

    bigrollerdave Well-Known Member

    Messages:
    2,112
    Likes Received:
    52
    Best Answers:
    0
    Trophy Points:
    140
    #6
    I tired to fix your code, but I can't figure out what you're trying to do here

    
    	$a.$i= $_POST["q".$i."a1"];
    	$a.$i= $_POST["q".$i."a2"];
    	$a.$i= $_POST["q".$i."a3"];
    	$a.$i= $_POST["q".$i."a4"];
    
    PHP:
    Here is the first page though

    
    <?
    echo '<form method="post" action="createxml.php">';
    for ($i =1; $i <=$number; $i++){ // THE $NUMBER CONTAINS VALUE -ok-
    	echo '<fieldset width ="20%">';
    	echo '<legend class = "login">WRITE A QUESTION</legend>';
    	echo ' <blockquote align="center">';
    	echo 'QUESTION '.$i.' (DESCRIPTION):<br>';
    	echo '<textarea name="q".$i rows="3" cols="50"></textarea><br><br>';
    	echo 'ANSWEAR 1:<textarea name="q'.$i.'a1" rows="1" cols="50"></textarea><br>';
    	echo 'ANSWEAR 2:<textarea name="q'.$i.'a2" rows="1" cols="50"></textarea><br>';
    	echo 'ANSWEAR 3:<textarea name="q'.$i.'a3" rows="1" cols="50"></textarea><br>';
    	echo 'ANSWEAR 4:<textarea name="q'.$i.'a4" rows="1" cols="50"></textarea><br>';
    }
    echo '<input type="submit">';
    echo '</form>';
    ?>
    
    PHP:
    Also when you echo a variable you don't need to put it on quotes just echo it like this
    
    //Wrong
    echo "$variable";
    //Right
    echo $variable;
    PHP:
     
    bigrollerdave, Jan 18, 2009 IP
  7. dourvas

    dourvas Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    let me be more specific.

    I am trying to built a test creation script. In a form the user is asked how many questions he is trying to set (variable $number) and with a for loop i create those questions.
    .......................
    
     echo '<form method="post" action="createxml.php">';
    for ($i =1; $i <=$number; $i++){ // THE $NUMBER CONTAINS VALUE -ok-
        echo '<fieldset width ="20%">';
        echo '<legend class = "login">WRITE A QUESTION</legend>';
        echo ' <blockquote align="center">';
        echo 'QUESTION '.$i.' (DESCRIPTION):<br>';
        echo '<textarea name="q".$i rows="3" cols="50"></textarea><br><br>';
        echo 'ANSWEAR 1:<textarea name="q'.$i.'a1" rows="1" cols="50"></textarea><br>';
        echo 'ANSWEAR 2:<textarea name="q'.$i.'a2" rows="1" cols="50"></textarea><br>';
        echo 'ANSWEAR 3:<textarea name="q'.$i.'a3" rows="1" cols="50"></textarea><br>';
        echo 'ANSWEAR 4:<textarea name="q'.$i.'a4" rows="1" cols="50"></textarea><br>';
    }
    echo '<input type="submit">';
    echo '</form>';
    
    Code (markup):
    in rumnig mode the first question will be named q1 (in order to post it) the first possible answear q1a1 the second q1a2, third q1a3, forth q1a4.
    the second question q2 .... and so on
    in the code
    
    for ($i =1; $i <=$number; $i+=1){
            $temp="q' . $i . '";
           $q[$i] = $_POST[$temp];
           echo  $q[$i];
           echo "<br>";
                 
           $qa1[$i]= $_POST["q' . $i . 'a1"];
           $qa2[$i]= $_POST["q' . $i . 'a2"];
           $qa3[$i]= $_POST["q' . $i . 'a3"];
           $qa4[$i]= $_POST["q' . $i . 'a4"];
           
           echo "answear=".$qa4[$i];
            echo "<br>";
    
        }
    
    Code (markup):
    i am trying to get those questions and answears in order to write it into xml file.
    e.g
    $qa1[$i]= $_POST["q' . $i . 'a1"];

    when the $i get 1 value it will post answear q1a1 into $qa1[1].
    that the whole idea. it does not work. how can i get those anwears in order to write them into xml file.

    My english are not quite well. i hope u understood me this time..
     
    dourvas, Jan 19, 2009 IP
  8. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    assuming $i = 1, PHP evaluates this $qa1[$i]= $_POST["q' . $i . 'a1"]; to $qa1[$i]= $_POST["q' . 1 . 'a1"]; because index is enclosed in double quote, to fix the issue, please see my previous post.
     
    artiskool, Jan 19, 2009 IP