I am building an online survey for which the results will be appended to a delimited file. Each page's selections in the survey will be concatenated to the previous pages results until the survey is done... and then written to the delimited file.

What is the most efficient way to keep concatenating the results? I post the string in a hidden input field each page and then pull it down on the next page to concatenate the next field results to. At the end, I would like a CSV file with quotes around the string fields. I tried doing that from the beginning but the quotes mess it all up! (I tried addslashes & stripslashes... that may be the answer, just need some guidance.

I could use the tab delimiter... but wanted some expert advice!

$outputstring = "\t".$surveyid."\t".$firstname."\t".$lastname."\t".($_POST['outputstring']);

Any help would be appreciated!

Thanks,
Doug

    You can add the quotes in first, but when putting the value into the hidden field use htmlspecialchars on it first.

    As far as alternatives, you could store an array of values in a session variable instead. That's what I'd do anyway.

      Originally posted by mtmosier
      You can add the quotes in first, but when putting the value into the hidden field use htmlspecialchars on it first.

      As far as alternatives, you could store an array of values in a session variable instead. That's what I'd do anyway.

      Your idea for an array sounds much easier. Especially since I could take the completed array and write it out to a string with the "," between fields. Could you provide an example of passing an array in a session variable?

      And I appreciate the note on the htmlspecialchars... that will work if I don't use the array! Thanks!!!

      Doug

        At the top of every page add

        session_start();

        Then you can store an existing array in the session like this

        $_SESSION['answers'] = $answerarray;

        You can use the array directly from the session array.

        echo $_SESSION['answers'][1];

        As usual more information and examples are available in the manual.

          Originally posted by mtmosier
          At the top of every page add

          session_start();

          Then you can store an existing array in the session like this

          $_SESSION['answers'] = $answerarray;

          You can use the array directly from the session array.

          echo $_SESSION['answers'][1];

          As usual more information and examples are available in the manual. [/B]

          Interesting... I'm used to vb where I would have to tell it that it's an array. In PHP, I don't have to declare that the variable $answerarray is an array?

          Sorry I'm being so dense... I already have a sesson started to capture the previous pages data. Like I submit a Survey ID on an html form on the first page and capture it on the second page with $surveyid = addslashes(ucwords(trim($_POST['surveyid'])));

          In this case, I have a series of questions:

          <form action="survey.php" method="post" name="infoForm" target="_top" id="infoForm">
          <table border="0" cellspacing="1" style="border-collapse: collapse" bordercolor="#111111" width="785">
              <tr>
                <td width="50%">&nbsp;</td>
                <td width="10%" align="center">Strongly Agree</td>
                <td width="10%" align="center">Disagree</td>
                <td width="10%" align="center">Neutral</td>
                <td width="10%" align="center">Agree</td>
                <td width="10%" align="center">Strongly Agree</td>
              </tr>
              <tr>
                <td width="50%" bgcolor="#CAE5F3">1. The Customer Service Representative was friendly</td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="1" name="p2q1"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="2" name="p2q1"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="3" name="p2q1"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="4" name="p2q1"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="5" name="p2q1"></td>
              </tr>
              <tr>
                <td width="50%">2. The Customer Service Representative was informative</td>
                <td width="10%" align="center"><input type="radio" value="1" name="p2q2"></td>
                <td width="10%" align="center"><input type="radio" value="2" name="p2q2"></td>
                <td width="10%" align="center"><input type="radio" value="3" name="p2q2"></td>
                <td width="10%" align="center"><input type="radio" value="4" name="p2q2"></td>
                <td width="10%" align="center"><input type="radio" value="5" name="p2q2"></td>
              </tr>
              <tr>
                <td width="50%" bgcolor="#CAE5F3">3. Overall I am satisfied by the customer service I 
                received.</td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="1" name="p2q3"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="2" name="p2q3"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="3" name="p2q3"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="4" name="p2q3"></td>
                <td width="10%" align="center" bgcolor="#CAE5F3"><input type="radio" value="5" name="p2q3"></td>
              </tr>
            </table>
          <a href="#" onclick="document.infoForm.submit();return false"><img border="0" src="images/next.gif" width="175" height="75"></a></form>
          

          How do I submit these results to a session array so I can pull them out in the next page. And then how can I subsequently add each page's resulting array while I loop through all the pages and questions.

          Thanks, for the help! You have me pulling out my PHP book! I think I've done some pretty good programming thus far. My page submits to itself, captures data entry errors and highlights them, and proceeds through the series of questions by using includes with the set of questions on each page. I've done 95% of this on my own (with 3 books in front of me) but this last issue is one that I want to program the best way possible.

          Doug

            Originally posted by coders4hire
            Interesting... I'm used to vb where I would have to tell it that it's an array. In PHP, I don't have to declare that the variable $answerarray is an array?

            Well, yes and no. What I said before was that you can add an existing array to the session, and as such was assuming that $answerarray already was initialized as an array.

            You can take any variable, existing or not, and simply start assigning elements to it as an array. Php will allow this, and if the variable is not already an array it will become an array. This, of course, is not recommended. You would normally initialize an array before use like this

            $answerarray = array();

            How do I submit these results to a session array so I can pull them out in the next page. And then how can I subsequently add each page's resulting array while I loop through all the pages and questions.

            Couple of ways to go about it. The most straightforward is to simply add each response to the array manually. Say you were using an associative array, using the question name as the key. You might do something like this.

            $_SESSION['answers']['p2q1'] = $_POST['p2q1'];
            $_SESSION['answers']['p2q2'] = $_POST['p2q2'];
            $_SESSION['answers']['p2q3'] = $_POST['p2q3'];

            If you had no elements in your form aside from questions, you could use array_merge to add them all at once.

            $_SESSION['answers'] = array_merge($_SESSION['answers'], $_POST);

            As long as you don't reassign $_SESSION['answers'] it should retain the information from previous pages. These examples are ways to add to what's there.

              Write a Reply...