Help With PHP Form Creating XML

[php]

Form to XML <?php if(isset($_REQUEST['submit'])) { // "Create" the document. $xml = new DOMDocument( "1.0" ); // Create some elements. $xml_doc = $xml->createElement( "document" ); $xml_data = $xml->createElement( "data"); foreach ($_REQUEST as $key => $value) { //loop through checkbox submissions // if (isset($_REQUEST['categoryItem'])) { // $optionArray = $_REQUEST['categoryItem']; // for ($i=0; $i<count($optionArray); $i++) { // echo $optionArray[$i]."
"; // } // } $xml_data->appendChild( $xml->createElement( $key, $value ) ); } // Map the $_REQUEST info from the form to variables // $xml_a = $xml->createElement( "Biography", $_REQUEST['bio'] ); // $xml_b = $xml->createElement( "DisplayName", $_REQUEST['displayName'] ); // $xml_c = $xml->createElement( "ContextualParagraph", $_REQUEST['contextualParagraph'] ); // $xml_d = $xml->createElement( "AdditionalPermissions", $_REQUEST['additionalPermissions'] ); // $xml_e = $xml->createElement( "CategoryItem", $_REQUEST['catList'] ); // $xml_f = $xml->createElement( "ImgURL", $_REQUEST['imgUrl'] ); // $xml_g = $xml->createElement( "ImgPermission", $_REQUEST['imgPermission'] ); // $xml_h = $xml->createElement( "NameOfAsset", $_REQUEST['assetName'] ); // $xml_i = $xml->createElement( "DateOfAsset", $_REQUEST['assetDate'] ); // $xml_j = $xml->createElement( "LocationOfAsset", $_REQUEST['assetLocation'] ); // $xml_k = $xml->createElement( "KeyNames", $_REQUEST['keyNames'] ); // $xml_l = $xml->createElement( "TypeOfAsset", $_REQUEST['assetType'] ); // $xml_m = $xml->createElement( "ContentAuthor", $_REQUEST['contentAuthor'] ); // $xml_n = $xml->createElement( "Choreographer", $_REQUEST['choreographer'] ); // $xml_o = $xml->createElement( "CommisssionedBy", $_REQUEST['comissionedBy'] ); // $xml_p = $xml->createElement( "Composer", $_REQUEST['composer'] ); // $xml_q = $xml->createElement( "Curator", $_REQUEST['curator'] ); // $xml_r = $xml->createElement( "Director", $_REQUEST['directors'] ); // $xml_s = $xml->createElement( "Festival", $_REQUEST['festival'] ); // $xml_t = $xml->createElement( "Film-maker", $_REQUEST['filmMaker'] ); // $xml_u = $xml->createElement( "Gallery", $_REQUEST['gallery'] ); // $xml_v = $xml->createElement( "NamedContent", $_REQUEST['namedContent'] ); // $xml_w = $xml->createElement( "OriginalBroadcaster", $_REQUEST['originalBroadcaster'] ); // $xml_x = $xml->createElement( "Photographer", $_REQUEST['photographer'] ); // $xml_y = $xml->createElement( "Playwright", $_REQUEST['playwright'] ); // $xml_z = $xml->createElement( "Producer", $_REQUEST['producer'] ); // $xml_aa = $xml->createElement( "ProductionCompany", $_REQUEST['productionCompany'] ); // $xml_ab = $xml->createElement( "Scenographer", $_REQUEST['scenographer'] ); // $xml_ac = $xml->createElement( "SchoolName", $_REQUEST['schoolName'] ); // $xml_ad = $xml->createElement( "WorkshopLeader", $_REQUEST['workshopLeader'] ); // $xml_ae = $xml->createElement( "OtherInfo", $_REQUEST['otherInfo'] ); // $xml_af = $xml->createElement( "Subject", $_REQUEST['subject'] ); // $xml_ag = $xml->createElement( "ConceptualFocus", $_REQUEST['conceptualFocus'] ); // $xml_ah = $xml->createElement( "HistoricalContext", $_REQUEST['historicalContext'] ); // Append the whole bunch. // $xml_data->appendChild( $xml_a ); // $xml_data->appendChild( $xml_b ); // $xml_data->appendChild( $xml_c ); // $xml_data->appendChild( $xml_d ); // $xml_data->appendChild( $xml_e ); // $xml_data->appendChild( $xml_f ); // $xml_data->appendChild( $xml_g ); // $xml_data->appendChild( $xml_h ); // $xml_data->appendChild( $xml_i ); // $xml_data->appendChild( $xml_j ); // $xml_data->appendChild( $xml_k ); // $xml_data->appendChild( $xml_l ); // $xml_data->appendChild( $xml_m ); // $xml_data->appendChild( $xml_n ); // $xml_data->appendChild( $xml_o ); // $xml_data->appendChild( $xml_p ); // $xml_data->appendChild( $xml_q ); // $xml_data->appendChild( $xml_r ); // $xml_data->appendChild( $xml_s ); // $xml_data->appendChild( $xml_t ); // $xml_data->appendChild( $xml_u ); // $xml_data->appendChild( $xml_v ); // $xml_data->appendChild( $xml_w ); // $xml_data->appendChild( $xml_x ); // $xml_data->appendChild( $xml_y ); // $xml_data->appendChild( $xml_z ); // $xml_data->appendChild( $xml_aa ); // $xml_data->appendChild( $xml_ab ); // $xml_data->appendChild( $xml_ac ); // $xml_data->appendChild( $xml_ad ); // $xml_data->appendChild( $xml_ae ); // $xml_data->appendChild( $xml_af ); // $xml_data->appendChild( $xml_ag ); // $xml_data->appendChild( $xml_ah ); // $xml_doc->appendChild( $xml_data ); $xml->appendChild( $xml_data ); $xml->save($_REQUEST['fileName'] . ".xml") or die('Error, unable to create XML file'); header( "Location: thankYou.php" ); } ?>
t Contributor Questionnaire
1. Please provide a short biographical note for yourself. Please begin your bio with your affiliation (if applicable) and end with another key piece of information. For example, Joe Bloggs is Professor of Theatre Studies at the University of Oxford, and directed the RSC&#39s 2013 production of Hamlet (max 200 characters):
2. What is your preferred display name for each of the assets you have given us (max 100 characters)?
3. Please provide a short contextual paragraph for each content item. This might relate it to the field, your other work, your career and/or the circumstances in which the work was created (max 300 words).
4. What – if any – additional permissions will we need to clear for, for example, music or visual content featured in a content item which is the property of a third party? Please include all essential credit info which must be included alongside the item.
5. Which of the following categories would you prefer each content item to be filed under?

Acting and Actor Training

Acting
Performer Training
Psychophysical Acting
Rehearsal
Text Analysis

Form and Genre

Laboratory Theatre
Naturalism and Realism

Theater-Making and Processes

Acting
Directing
Ensemble
Laboratory Theatre
Rehearsal
Text Analysis
Theatre History

6a. Please provide an image we can use to illustrate the pages containing your AV material. Ideally you should own, or be able to easily obtain, the rights to use the images. Please copy the images below (low-res please) along with copyright info, and email hi-res versions to #.com
6b. Would you be willing for us to use any or all of these images for promotional purposes? Yes No
7. Please fill out the following tables, which will dictate the manner in which your commentaries are tagged for keyword searching.

Essential info - please fill in all of the following:

Name of asset
Date of asset
Location of asset
Names of key figures/companies/groups involved
Type of asset

Optional info - please fill in all of the following that are relevant:

Author(s) of named content
Choreographer
Commissioned by
Composer
Curator
Director(s) (of both video and production, if different)
Festival (it&#39s a part of, it premiered at etc.)
Film-maker
Gallery
Named content featured (i.e. literary, music etc.)
Original broadcaster/channel
Photographer
Playwright(s) (of any texts used, cited, or explored)
Producer
Production Company
Scenographer
University/School (venue, affiliation of participants etc.)
Workshop Leader
Any other factual info

Thematic descriptors – please only provide two tags for each of the following categories:

Subject (what the work is ‘about’)
Conceptual focus – aesthetic or methodological (i.e. postmodern, postdramatic, realist)
Historical context, tradition or era (i.e. postwar, Broadway, off-Broadway)

Input file name for XML file that will be generated by this form (note: You do not need to append a file type after your file name. As an example, just input "MyFileName", not "MyFileName.xml"):
[/php]

Hi all. So I initially created this PHP file which will create an XML file from info that was submitted to the form. But (as you can see in the commented out sections) I did it via hard-coding values. I’m trying to convert it to dynamically loop through the data via foreach, but am having trouble in general, and specifically in the part where I need to loop through the array of checkbox items.

Any help or thoughts appreciated. Thank you.

You need to use a data structure (array or database table) that defines the expected form fields. You would then loop through this defining data structure to control what the code does. In general, you shouldn’t blindly loop through the submitted form data, since a hacker/bot script can submit anything they want.

Using an array -
[php]// define the expected form fields
$fields = [];
// the array key/index is the form field name, the array elements define information about the data, such as the label/xml element name
$fields[‘bio’] = [‘label’=>‘Biography’];
// add other field definitions here…

// for checkbox fields, that have a list of possible choices, you would add a sub-array listing the choices. you would loop over this defining list and detect if that checkbox is checked or not in the submitted form data.
// the checkbox form field array name’s index should be the value. the actual checkbox value should use the default on value. this will mean that you can just detect if the form field name[index] is set or not in the submitted data.

$fields[‘training’] = [‘label’=>‘Acting and Actor Training’,‘sub_array’=>[‘Acting’,‘Performer Training’,‘other choices here…’]];
[/php]

You would then loop through the $fields array and get the form field name ($key) and array of information using a foreach loop. You would get the actual data out of the $_POST array (don’t use $_REQUEST if you expect the data via a post method form) using the key - $data = $_POST[$key].

If there is a defined sub_array of choices in the $fields array entry, you would use that to loop over the sub-array of submitted data. You should define each group of your categoryItem choices using a different root field name.

The ‘label’ entry in the defining array will give you the xml element name. You would just dynamically create each xml element and append it to the main xml document.

phdr, first let me just say THANK YOU for taking the time to help out a newb. I think I “sorta” understand what you were saying, but not 100%. Here is what I did, based on your comment. Am I kind of on the right track?

[php]

Form to XML <?php if(isset($_REQUEST['submit'])) { // "Create" the document. $xml = new DOMDocument( "1.0" ); // Create some elements. $xml_doc = $xml->createElement( "document" ); $xml_data = $xml->createElement( "data"); foreach ($_REQUEST as $key => $value) { //loop through checkbox submissions // if (isset($_REQUEST['categoryItem'])) { // $optionArray = $_REQUEST['categoryItem']; // for ($i=0; $i<count($optionArray); $i++) { // echo $optionArray[$i]."
"; // } // } // BEGIN paste of code from phphelp // define the expected form fields $fields = []; // the array key/index is the form field name, the array elements define information about the data, such as the label/xml element name $fields['bio'] = ['label'=>'Biography']; $fields['displayName'] = ['label' => 'DisplayName']; $fields['contextualParagraph'] = ['label' => 'ContextualParagraph']; $fields['additionalPermissions'] = ['label' => 'AdditionalPermissions']; if(ISSET($fields['training'])) { $fields['training'] = ['label'=>'Acting and Actor Training','sub_array'=>['Acting', 'Performer Training', 'Psychophysical Acting', 'Rehearsal', 'Text Analysis'], 'sub_array2'=>['Laboratory Theatre', 'Naturalism and Realism'],'subarray3'=>['acting', 'Directing', 'Ensemble', 'Laboratory Theatre', 'Rehearsal', 'Text Analysis', 'Theatre History']]; } $fields['imgUrl'] = ['label' => 'ImgURL']; $fields['imgPermission'] = ['label' => 'ImgPermission']; $fields['assetName'] = ['label' => 'NameOfAsset']; $fields['assetDate'] = ['label' => 'DateOfAsset']; $fields['assetLocation'] = ['label' => 'LocationOfAsset']; $fields['keyNames'] = ['label' => 'KeyNames']; $fields['assetType'] = ['label' => 'TypeOfAsset']; $fields['contentAuthor'] = ['label' => 'ContentAuthor']; $fields['choreographer'] = ['label' => 'Choreographer']; $fields['comissionedBy'] = ['label' => 'CommisssionedBy']; $fields['composer'] = ['label' => 'Composer']; $fields['curator'] = ['label' => 'Curator']; $fields['directors'] = ['label' => 'Director']; $fields['festival'] = ['label' => 'Festival']; $fields['filmMaker'] = ['label' => 'Film-maker']; $fields['gallery'] = ['label' => 'Gallery']; $fields['namedContent'] = ['label' => 'NamedContent']; $fields['originalBroadcaster'] = ['label' => 'OriginalBroadcaster']; $fields['photographer'] = ['label' => 'Photographer']; $fields['playwright'] = ['label' => 'Playwright']; $fields['producer'] = ['label' => 'Producer']; $fields['productionCompany'] = ['label' => 'ProductionCompany']; $fields['scenographer'] = ['label' => 'Scenographer']; $fields['schoolName'] = ['label' => 'SchoolName']; $fields['workshopLeader'] = ['label' => 'WorkshopLeader']; $fields['otherInfo'] = ['label' => 'OtherInfo']; $fields['subject'] = ['label' => 'Subject']; $fields['conceptualFocus'] = ['label' => 'ConceptualFocus']; $fields['historicalContext'] = ['label' => 'HistoricalContext']; // add other field definitions here... // for checkbox fields, that have a list of possible choices, you would add a sub-array listing the choices. you would loop over this defining list and detect if that checkbox is checked or not in the submitted form data. // the checkbox form field array name's index should be the value. the actual checkbox value should use the default on value. this will mean that you can just detect if the form field name[index] is set or not in the submitted data. //END paste of code from phphelp $xml_data->appendChild( $xml->createElement( $key, $value ) ); } // Map the $_REQUEST info from the form to variables // $xml_a = $xml->createElement( "Biography", $_REQUEST['bio'] ); // $xml_b = $xml->createElement( "DisplayName", $_REQUEST['displayName'] ); // $xml_c = $xml->createElement( "ContextualParagraph", $_REQUEST['contextualParagraph'] ); // $xml_d = $xml->createElement( "AdditionalPermissions", $_REQUEST['additionalPermissions'] ); // $xml_e = $xml->createElement( "CategoryItem", $_REQUEST['catList'] ); // $xml_f = $xml->createElement( "ImgURL", $_REQUEST['imgUrl'] ); // $xml_g = $xml->createElement( "ImgPermission", $_REQUEST['imgPermission'] ); // $xml_h = $xml->createElement( "NameOfAsset", $_REQUEST['assetName'] ); // $xml_i = $xml->createElement( "DateOfAsset", $_REQUEST['assetDate'] ); // $xml_j = $xml->createElement( "LocationOfAsset", $_REQUEST['assetLocation'] ); // $xml_k = $xml->createElement( "KeyNames", $_REQUEST['keyNames'] ); // $xml_l = $xml->createElement( "TypeOfAsset", $_REQUEST['assetType'] ); // $xml_m = $xml->createElement( "ContentAuthor", $_REQUEST['contentAuthor'] ); // $xml_n = $xml->createElement( "Choreographer", $_REQUEST['choreographer'] ); // $xml_o = $xml->createElement( "CommisssionedBy", $_REQUEST['comissionedBy'] ); // $xml_p = $xml->createElement( "Composer", $_REQUEST['composer'] ); // $xml_q = $xml->createElement( "Curator", $_REQUEST['curator'] ); // $xml_r = $xml->createElement( "Director", $_REQUEST['directors'] ); // $xml_s = $xml->createElement( "Festival", $_REQUEST['festival'] ); // $xml_t = $xml->createElement( "Film-maker", $_REQUEST['filmMaker'] ); // $xml_u = $xml->createElement( "Gallery", $_REQUEST['gallery'] ); // $xml_v = $xml->createElement( "NamedContent", $_REQUEST['namedContent'] ); // $xml_w = $xml->createElement( "OriginalBroadcaster", $_REQUEST['originalBroadcaster'] ); // $xml_x = $xml->createElement( "Photographer", $_REQUEST['photographer'] ); // $xml_y = $xml->createElement( "Playwright", $_REQUEST['playwright'] ); // $xml_z = $xml->createElement( "Producer", $_REQUEST['producer'] ); // $xml_aa = $xml->createElement( "ProductionCompany", $_REQUEST['productionCompany'] ); // $xml_ab = $xml->createElement( "Scenographer", $_REQUEST['scenographer'] ); // $xml_ac = $xml->createElement( "SchoolName", $_REQUEST['schoolName'] ); // $xml_ad = $xml->createElement( "WorkshopLeader", $_REQUEST['workshopLeader'] ); // $xml_ae = $xml->createElement( "OtherInfo", $_REQUEST['otherInfo'] ); // $xml_af = $xml->createElement( "Subject", $_REQUEST['subject'] ); // $xml_ag = $xml->createElement( "ConceptualFocus", $_REQUEST['conceptualFocus'] ); // $xml_ah = $xml->createElement( "HistoricalContext", $_REQUEST['historicalContext'] ); // Append the whole bunch. // $xml_data->appendChild( $xml_a ); // $xml_data->appendChild( $xml_b ); // $xml_data->appendChild( $xml_c ); // $xml_data->appendChild( $xml_d ); // $xml_data->appendChild( $xml_e ); // $xml_data->appendChild( $xml_f ); // $xml_data->appendChild( $xml_g ); // $xml_data->appendChild( $xml_h ); // $xml_data->appendChild( $xml_i ); // $xml_data->appendChild( $xml_j ); // $xml_data->appendChild( $xml_k ); // $xml_data->appendChild( $xml_l ); // $xml_data->appendChild( $xml_m ); // $xml_data->appendChild( $xml_n ); // $xml_data->appendChild( $xml_o ); // $xml_data->appendChild( $xml_p ); // $xml_data->appendChild( $xml_q ); // $xml_data->appendChild( $xml_r ); // $xml_data->appendChild( $xml_s ); // $xml_data->appendChild( $xml_t ); // $xml_data->appendChild( $xml_u ); // $xml_data->appendChild( $xml_v ); // $xml_data->appendChild( $xml_w ); // $xml_data->appendChild( $xml_x ); // $xml_data->appendChild( $xml_y ); // $xml_data->appendChild( $xml_z ); // $xml_data->appendChild( $xml_aa ); // $xml_data->appendChild( $xml_ab ); // $xml_data->appendChild( $xml_ac ); // $xml_data->appendChild( $xml_ad ); // $xml_data->appendChild( $xml_ae ); // $xml_data->appendChild( $xml_af ); // $xml_data->appendChild( $xml_ag ); // $xml_data->appendChild( $xml_ah ); // $xml_doc->appendChild( $xml_data ); $xml->appendChild( $xml_data ); $xml->save($_REQUEST['fileName'] . ".xml") or die('Error, unable to create XML file'); header( "Location: thankYou.php" ); } ?>
Taylor & Francis Contributor Questionnaire
1. Please provide a short biographical note for yourself. Please begin your bio with your affiliation (if applicable) and end with another key piece of information. For example, Joe Bloggs is Professor of Theatre Studies at the University of Oxford, and directed the RSC&#39s 2013 production of Hamlet (max 200 characters):
2. What is your preferred display name for each of the assets you have given us (max 100 characters)?
3. Please provide a short contextual paragraph for each content item. This might relate it to the field, your other work, your career and/or the circumstances in which the work was created (max 300 words).
4. What – if any – additional permissions will we need to clear for, for example, music or visual content featured in a content item which is the property of a third party? Please include all essential credit info which must be included alongside the item.
5. Which of the following categories would you prefer each content item to be filed under?

Acting and Actor Training

Acting
Performer Training
Psychophysical Acting
Rehearsal
Text Analysis

Form and Genre

Laboratory Theatre
Naturalism and Realism

Theater-Making and Processes

Acting
Directing
Ensemble
Laboratory Theatre
Rehearsal
Text Analysis
Theatre History

6a. Please provide an image we can use to illustrate the pages containing your AV material. Ideally you should own, or be able to easily obtain, the rights to use the images. Please copy the images below (low-res please) along with copyright info, and email hi-res versions to [email protected]
6b. Would you be willing for us to use any or all of these images for promotional purposes? Yes No
7. Please fill out the following tables, which will dictate the manner in which your commentaries are tagged for keyword searching.

Essential info - please fill in all of the following:

Name of asset
Date of asset
Location of asset
Names of key figures/companies/groups involved
Type of asset

Optional info - please fill in all of the following that are relevant:

Author(s) of named content
Choreographer
Commissioned by
Composer
Curator
Director(s) (of both video and production, if different)
Festival (it&#39s a part of, it premiered at etc.)
Film-maker
Gallery
Named content featured (i.e. literary, music etc.)
Original broadcaster/channel
Photographer
Playwright(s) (of any texts used, cited, or explored)
Producer
Production Company
Scenographer
University/School (venue, affiliation of participants etc.)
Workshop Leader
Any other factual info

Thematic descriptors – please only provide two tags for each of the following categories:

Subject (what the work is ‘about’)
Conceptual focus – aesthetic or methodological (i.e. postmodern, postdramatic, realist)
Historical context, tradition or era (i.e. postwar, Broadway, off-Broadway)

Input file name for XML file that will be generated by this form (note: You do not need to append a file type after your file name. As an example, just input "MyFileName", not "MyFileName.xml"):
[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service