我运行了一段PHP代码,它收集用户从基于web的表单(CF7插件)输入的数据,然后将其导出为XML文件格式。
在此表单中,用户可以填写多种类型的课程。例如:工程学、市场营销学、医学……
我正在寻找一个句子来添加一个功能到我的代码中,只有“创建”属性的数据(字段块)时,需要时,即当用户填写超过1课程的信息,而不是有它之前设置和留空。
这是因为我不能预测用户可以填写多少课程,,,并且我不能仅仅预先创建几个字段“等待”用户的输入,这些字段可能不会出现。我不能把它留空。需要根据需要创建。
我的实际代码段(2个字段块):
$xmlSigam = $domDocument->createElement('SigaFiles');
$xmlEntity->appendChild($xmlSigam);
$xmlSigam->setAttribute("Text", "SQM");
$xml_dados = $domDocument->appendChild($domDocument->createElement('Dados'));
$xmlSigam->appendChild($xml_dados);
$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($posted_data['code']));
$attribute->setAttribute('domainname', 'Code');
$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($posted_data['course']));
$attribute->setAttribute('domainname', 'Course');
$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($posted_data['description']));
$attribute->setAttribute('domainname', 'Description');
$xml_dadosmb = $domDocument->appendChild($domDocument->createElement('Dados'));
$xmlSigam->appendChild($xml_dadosmb);
$attributemb = $xml_dadosmb->appendChild($domDocument->createElement('attribute'));
$attributemb->appendChild($domDocument->createTextNode($posted_data['codemb']));
$attributemb->setAttribute('domainname', 'Code');
$attributemb = $xml_dadosmb->appendChild($domDocument->createElement('attribute'));
$attributemb->appendChild($domDocument->createTextNode($posted_data['coursemb']));
$attributemb->setAttribute('domainname', 'Course');
$attributemb = $xml_dadosmb->appendChild($domDocument->createElement('attribute'));
$attributemb->appendChild($domDocument->createTextNode($posted_data['descriptionmb']));
$attributemb->setAttribute('domainname', 'Description');
当前可扩展标记语言文件格式输出:(以防用户只填写1块字段)
<SigaFiles Text="SQM">
<Data>
<attribute domainname="Code">00001</attribute>
<attribute domainname="Course">ENGINEERING</attribute>
<attribute domainname="Description">COMPUTER ENGINEERING</attribute>
</Data>
<Data>
<attribute domainname="Code"></attribute>
<attribute domainname="Course"></attribute>
<attribute domainname="Description"></attribute>
</Data>
</SigaFiles>
发布于 2018-09-17 01:48:44
您应该在表单中创建多选字段
<select name="courses[]" id="countries" multiple="multiple">
<option value="0001">Engineering</option>
<option value="0002">History</option>
<option value="0003">Computer Science</option>
<option value="0004">Philosphy</option>
</select>
在你的帖子php代码块中:
$xmlSigam = $domDocument->createElement('SigaFiles');
$xmlEntity->appendChild($xmlSigam);
$xmlSigam->setAttribute("Text", "SQM");
$xml_dados = $domDocument->appendChild($domDocument->createElement('Dados'));
$xmlSigam->appendChild($xml_dados);
$course_array = $_POST['courses'];
foreach($courses as $courseCode => $course)
{
$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($courseCode));
$attribute->setAttribute('domainname', 'Code');
$attribute = $xml_dados->appendChild($domDocument->createElement('attribute'));
$attribute->appendChild($domDocument->createTextNode($course));
$attribute->setAttribute('domainname', 'Course');
}
https://stackoverflow.com/questions/52318998
复制相似问题