【问题标题】:output form values to xml using php for loop使用php for循环将表单值输出到xml
【发布时间】:2016-04-28 21:58:07
【问题描述】:

所以我在 PHP 中设置 for 循环以将 HTML 表单的内容输出到 xml 文件时遇到了麻烦。它可以在没有 for 循环的情况下工作,但不能。有任何想法吗?扯掉我的头发!

提前致谢。

//PHP Form

<?php
if (isset($_POST['lsr-submit']))
{
}
$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);
$i=1;
for ($i=1; $i <50; $i++) 
{ 
   $name = $_POST['Name_'.$i];
   $time = $_POST['Time_'.$i];
   $duration = $_POST['Duration_'.$i];
   $name = htmlentities($name, ENT_COMPAT, 'UTF-8', false);
   $time = htmlentities($time, ENT_COMPAT, 'UTF-8', false);
   $duration = htmlentities($duration, ENT_COMPAT, 'UTF-8', false);
   $xml->Slot = "";
   $xml->Slot->addChild('Name', $name);
   $xml->Slot->addChild('Time', $time);
   $xml->Slot->addChild('Duration', $duration);
}
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('../scSHARE/editor.xml');
print "<meta http-equiv=\"refresh\"     content=\"0;URL=return.php\">";
?>

//HTML Form

<?php
$xml=simplexml_load_file("../scSHARE/editor.xml");
$xml_array = (array) $xml;
$chunks=array_chunk($xml_array["Slot"], 7);
$i=1;
$l=1;
foreach($chunks as $bank) {
  ?> 
  <form name="form1" method="post" action="form.php">
     <div class="menu-toggle bank_<?php echo $i; ?>"  style="display:none;">
       <?php
       foreach($bank as $slot) { ?>
         <div class="row">
           <div class="small-12 column">
             <div class="small-6 column">
                Slot <?php echo $l?>
               <input type="text" name="Name_<?php echo $l;?>" value="<?php echo $slot->Name;?>">
             </div>
             <div class="small-6 column">
               <div class="row">
                 <div class="small-6 column">
                  Time
                   <input type="text" name="Time_<?php echo $l;?>" value="<?php echo $slot->Time;?>">
                 </div>
                 <div class="small-6 column">
                   Duration
                   <input type="text" name="Duration_<?php echo $l;?>" value="<?php echo $slot->Duration;?>">
                 </div>
               </div>
             </div>
           </div>
         </div>
       <?php $l++;} ?>
      <p style="text-align: center;">
        <input type="submit" name="submit" id="submit" class="hollow success button" value="Update">
      </p>
     </div>
  </form>

<?php $i++;
}
?>

【问题讨论】:

  • 首先,你的if (isset($_POST['lsr-submit']))没有任何作用。你需要把它应用到你所有的代码上,而不是什么都没有。

标签: php html xml


【解决方案1】:

你使用这个语法:

for ($i=1; $i <50; $i++) 
{ 
    (...)
    $xml->Slot = "";
    $xml->Slot->addChild('Name', $name);
}

通过这种方式,在每次迭代中,您都会覆盖之前的 &lt;Slot&gt; 节点。您必须将子节点添加到根节点:

for ($i=1; $i <3; $i++) 
{ 
    (...)
    $slot = $xml->addChild( "Slot" );
    $slot->addChild( 'Name', $name );
    $slot->addChild( 'Time', $time );
    $slot->addChild( 'Duration', $duration );
}

旁注:

考虑用isset($_POST['lsr-submit'])包装整个代码

if (isset($_POST['lsr-submit']))
{
    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    (...)
    print "<meta http-equiv=\"refresh\"     content=\"0;URL=return.php\">";
}

【讨论】:

  • 啊,这很有道理,它现在可以正常工作了。感谢您及时的回复。非常感谢!
猜你喜欢
  • 2017-03-12
  • 1970-01-01
  • 2019-12-26
  • 2012-12-07
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 2020-07-23
相关资源
最近更新 更多