【问题标题】:php or javascript Grab next array on buttonphp 或 javascript 抓取按钮上的下一个数组
【发布时间】:2012-03-07 21:41:13
【问题描述】:

提取 simplexml 并将它们解析为 php 变量。我有一个表格,里面有数组。我想要一个表单之外的按钮,它将“基本上”转到相同的表单但具有下一个数组编号。即:

<? 
   if( $xml = simplexml_load_file('my.xml') ) {
       foreach( $xml as $SAVED_EXPORT) {
           $mfg = $SAVED_EXPORT->productmanufacturer;
        }
   }
?>

<form id="myform" method="post" action="coderdb.php">
  <input type="text" value="<? echo $mfg[0] ?>" name="MFG" />
  <input type="submit" />
</form>

我想要一个按钮,上面写着 NEXT,点击它会拉出下一个数组,即。 $mfg[1]。我相信该页面必须重新加载,这很好。我在某个地方读到过,我可能不得不使用$key,但从未使用过,而且我不确定我在这里需要什么。

【问题讨论】:

    标签: php javascript arrays post next


    【解决方案1】:

    你需要一些 javascript 使用 JQuery。

    <script type="text/javascript">
        $(function(){
            var array = [];
    <? 
        if( $xml = simplexml_load_file('my.xml') ) {
            $i=0;
            foreach( $xml as $SAVED_EXPORT) {
                $mfg = $SAVED_EXPORT->productmanufacturer;
            }
        }
        foreach($mgf as $key=$value) {
            // if $key in not numeric then add iterator for this foreach   
            echo "array[$key]=$value";
        }
    ?>
            iterator = 0;
            $('#nextExport').click(function(){
                var theInput = $('#setExport');
                // theInput.attr('val',array[iterator]);      
                theInput.val(array[iterator]);
                if(iterator==array.length) { 
                      iterator = -1;
                }
                iterator++; 
            });
        });
    </script>
    
    <form id="myform" method="post" action="coderdb.php">
         <input type="text" id="setExport" value="<? echo $mfg[0] ?> name="MFG" />
         <input type="button" id="nextExport" value="Next Export" />
         <input type="submit" />
    </form>
    

    我希望这会有所帮助。如果出现问题,请检查语法,逻辑是正确的。

    【讨论】:

      【解决方案2】:

      您需要一种方法来跟踪您所在的数组索引,因为目前您只需将其设置为静态数字0。因此,如果您添加一个隐藏表单,您可以让它发布您当前的值,然后使用 PHP 您可以在显示新表单之前增加它。此外,您的表单应该指向它所在的 PHP 文档。

      <? 
         if( $xml = simplexml_load_file('my.xml') ) {
             foreach( $xml as $SAVED_EXPORT) {
                 $mfg = $SAVED_EXPORT->productmanufacturer;
              }
         }
         if(isset($_POST["index"])) $index = $_POST["index"] + 1;
         else $index = 0;
      ?>
      
      <form id="myform" method="post" action="coderdb.php">
        <input type="hidden" value="<? echo $index ?>" name="index" />
        <input type="text" value="<? echo $mfg[$index] ?>" name="MFG" />
        <input type="submit" />
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多