Jump to content

Saving Survey Data in XML


honeyroasted

Recommended Posts

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? :)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
[code]
<?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>    [/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.