I have a survey form with almost 40 text boxes in HTML form.
A user has to fill in all the boxes and submit.

How do I take the survey text box answers in PHP to insert the answers in DB ?

One way is to declare 40 text variables in a loop of 40 (ridiculous option)
<input type=text size='20' maxlength='25' name='survey1'>
<input type=text size='20' maxlength='25' name='survey2'>
.
.
.
<input type=text size='20' maxlength='25' name='survey40'>
How will i relate the QuestionID to the answer then.

I tried this way too:

echo "<input type=hidden name='survey[id]' value=$qid>" ;
echo "Your answer <input type=text size='35' maxlength='40' name='survey[ans]'>" ;

The above will not create an array for me in PHP after submit.

What other options are there for me ?

thanks in advance.
tx.

    If you could expand slightly, do you want to know how to deal with the form post or how to display the form?

      Simon,

      sorry for the confusion creatred. I composed the question in hurry.

      it is to deal the form in PHP that i am about for.

      I have already created the form and that is fine.
      What i need is, how to handle 40 textbox questions with their relevant ids after submition, array or 40 variables, or anything, which is recommended and proper way.

      i want to insert the suvery answers in DB.

      Thanks for responding promptly.

        It really depends on where you want your answers. If you have just one rows of results thats easy;

        $query = "INSERT into answers values 
        ('$answer1','$answer2','$answer3','answer4', ..etc * 40)";
         mysql_db_query($dbname,$query);
        

        However I get the inclination you need to store the results in different tables related to different id's, is this the case?

        eg answer 1 needs to go into this table, answer 2 needs to go into this table?

        Further clarification would be good.

          No, it is only to go in one table
          with Survey Question Id and its Answer.

          I have the Question ID on the Form (assume as hidden variable or array, whichever)
          The answers are filled by the user, there are round about 40 questions, all text type.

          I thought of 40 variables, but that is rediculous, there should be an easy array method to handle this.
          like after submittion,

          array survey("id","ans"). after form submission.

          I then will read the array values, from top to last, and insert it into the table one by one.

          How can i achive this in a simpler task.

          thanks.

            There really is no problem using 40 variables,
            within a query, I run a commercial site, where you
            can design and distribute a survey of up to 90
            questions, I control insert database queries using a
            dynamic engine I designed using for loops.

            Either way though your still going to be doing the
            processing of the 40 variables, which are essentially
            such when you recieve them in the HTTP header.
            Remember the _GET super variable is of type array
            anyway.

            A for loop could be used thusly;

            //You must first create the record before you have dynamic access
            $query = "INSERT into tblname VALUES ("","","" * 40)";
            $try=mysql_db_query(DBNAME,$query);
            $id = mysql_insert_id();
            
            for ($i = 0; $i <= 40; $i++){
            $answer = "answer".$i; //Form element name
            $ans = $HTTP_POST_VARS[$answer];//Extract from http array
            /*
            I have assumed your column names are the 
            same as your form variables eg, answer1 etc etc.
            */
            $query = "UPDATE tblname SET $ans = $ans WHERE id = $id";
            $try=mysql_db_query(DBNAME,$query);
            }
            

            This however is a rather long winded way of doing things. Because we have to run 41 queries rather than just 1!!

            I cannot think of how else you mean to do this,
            but I suppose this would be alright if you needed
            control over the variables individually.

            Hope this has helped.

              I would most likely pass the survey id as a hidden variable.
              echo "<input type=hidden name='survey_id' value=$survid>" ;

              When you recieve it in the resulting script, I would make sure it's valid.

              Also, store your responses in "Answers[]" as such:
              <input type=text size='20' maxlength='25' name='Answers[]'>

              Addendum:

              And then, on the other side, I would use a foreach statement to loop through the variables.

              I cannot tell you the right thing to do, I can only tell you what I would do in your situation.

              Good luck.

                I did it like this, array method.
                I was looking for this type of method,where i can relate the question id to its answer later.

                echo "<input type=hidden name='survey_id[]' value=$qid>" ;
                echo "Your answer <input type=text size='35' maxlength='40' name='survey_ans[]'>" ;

                After submit.

                for ($i=0 ;$i<count($survey_id) ; $i++) {
                $qid=0 ;
                $qans='';
                $qid = $survey_id[$i] ;
                $qans = $survey_ans[$i] ;

                   $theSQL = '' ;
                   $theSQL = "Insert into surveytble " ;                                  
                   $theSQL .= " Values " ;
                   $theSQL .= " ($qid,\"$qans\")" ;

                }

                Thank you, Simon and Michael, for the clues and help.

                tx.

                  Write a Reply...