Jump to content


Photo

Saving Survey Data in XML


  • Please log in to reply
3 replies to this topic

#1 honeyroasted

honeyroasted
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 October 2006 - 02:25 AM

I have a survey I wrote for class and write now I'm running a script that just sends me text in an e-mail of the survey replies.  I need to be able to parse the survey responses into xml so that I can make a quick table (mostly because i know what to do with xml once i have it).

So the php I'm running is from a friend of mine, meaning I really have no experience using the language  ???.  I guess I'll say what my html is like?

Here's an example question:

<p>
<span class="q"><b>(2) </b>If you answered <b>YES</b>, is this full-time job permanent or temporary?</span><br><br/>
<input type="radio" name="FTtype" value="PermanentFT">Permanent</input><br/>
<input type="radio" name="FTtype" value="TemporaryFT">Temporary</input><br/>
<input type="radio" name="FTtype" value="DNA">Does Not Apply</input><br/>
</p>

As you can see, the question's answers have the same "name" which can be used as the XML element, and the values can be used as the value of the xml element.  Ideally, I'd like to get xml from this question looking like this:

<FTtype>PermanentFT</FTtype>

Can anyone out there help me please? :)

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 October 2006 - 07:18 AM

Something like this

<?php
$xml = '';
foreach ($_POST as $field => $value) {
    if ($field != 'submit_button_name') {
    
        $xml .= "<$field>$value</$field>\n";
    }
}

//view xml
echo '<pre>', $xml, '</pre>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 honeyroasted

honeyroasted
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 October 2006 - 01:00 PM

Hi Barand,

Thank you for the code - it's working!

The only problem is for questions in which there are multiple answers.  The script is only grabbing 1 answer, the last one.  So for a question like this, how do I get the code?

<p>
<span class="q"><b>(20) </b>During your work day, how do people contact you? (check all that apply)</span><br><br/>

<input type="checkbox" name="contactmethod" value="cell">Cell Phone<br>
<input type="checkbox" name="contactmethod" value="pager">Pager<br>
<input type="checkbox" name="contactmethod" value="telephone">Regular Telephone<br>
<input type="checkbox" name="contactmethod" value="email">Email<br>
<input type="checkbox" name="contactmethod" value="IM">Instant Messenger<br>
<input type="checkbox" name="contactmethod" value="Fax">Fax<br>
<input type="checkbox" name="contactmethod" value="inperson">In Person
</p>


It doesn't matter if I get it like:

<contactmethod>cell</contactmethod>
<contactmethod>page</contactmethod>

As in, I don't need the answers embedded in their own hierarchy.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 October 2006 - 07:48 PM

First, you need to name your checkboxes as "contactmethod[]" or only the last will be posted. With the "[]" at the end they (selected ones) are all posted as an array of values

example
<?php
$xml = '';
foreach ($_POST as $field => $value) {
    if ($field != 'action') {
        if (is_array($value)) {
            foreach ($value as $v) {
                $xml .= "<$field>$v</$field>\n";
            }
        }
        else {
            $xml .= "<$field>$value</$field>\n";
        }
    }
}

//view xml
echo '<pre>', htmlentities($xml), '</pre>';
?>
<hr>
<FORM method='post'>
Field 1 <input type="text" name="field1" size="5"><br />
Field 2 <input type="text" name="field2" size="5"><br />
Radio <input type="radio" name="rad1" value="Y"> Yes
<input type="radio" name="rad1" value="N"> No <br />
CBoxes <input type="checkbox" name="cbox[]" value="a">A
<input type="checkbox" name="cbox[]" value="b">B
<input type="checkbox" name="cbox[]" value="c">C
<input type="checkbox" name="cbox[]" value="d">D
<input type="checkbox" name="cbox[]" value="e">E<br />
<input type="submit" name="action" value="Submit"> 
</FORM>   

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users