【问题标题】:str_replace for dynamic formstr_replace 用于动态形式
【发布时间】:2016-09-13 01:05:45
【问题描述】:

我正在使用这个动态表单

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Input Data</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script language="javascript">
    function tambahHobi() {
            var idf = document.getElementById("idf").value;
            var stre;
            stre="<p id='srow" + idf + "'><input type='text' size='40' name='rincian_hobi[]' placeholder='Masukkan Hobi' /> <a href='#' style=\"color:#3399FD;\" onclick='hapusElemen(\"#srow" + idf + "\"); return false;'>Hapus</a></p>";
            $("#divHobi").append(stre); 
            idf = (idf-1) + 2;
            document.getElementById("idf").value = idf;
        }
    function hapusElemen(idf) {
            $(idf).remove();
        }
</script>
</head>
<body>
<div id="container">
<h2>Input Data</h2>
<form method="post" action="doc.php">
    <input id="idf" value="1" type="hidden" />
    <p>  Nomor Kotak : <input name="nama" type="text" id="nama" size="40"> </p>
    <p> Kode :<select name="kode">
    <option></option>
    <option value="BMN">BMN</option>
    <option value="KP">KP</option>
    <option value="A">A</option>
    <option value="KU">KU</option>
    <p>  No : <input name="nokode" type="text" id="nama" size="40"> </p>
    <p>  TAHUN : <input name="tahun" type="text" id="nama" size="40"> </p>

    <button type="button"  onclick="tambahHobi(); return false;">Tambah Rincian</button>
    <div id="divHobi"></div>
    <button type="submit">Simpan</button>
</form>
</div>
</body>
</html>

我不使用数据库,所以我只是将表单生成到 ms.Word 文档中
这是 doc.php

<?php

$nama=$_POST['nama'];
        $kode=$_POST['kode'];
        $nokode=$_POST['nokode'];
        $tahun=$_POST['tahun'];
       if(isset($_POST["rincian_hobi"]))
                    {
                        $hoby=$_POST["rincian_hobi"];
                        reset($hoby);
                        while (list($key, $value) = each($hoby)) 
                            {
                                $rincian_hoby   =$_POST["rincian_hobi"][$key];
                            }
                    }

    $document = file_get_contents("doc.rtf");

$document = str_replace("%%NAMA%%", $nama, $document);
$document = str_replace("%%KODE%%", $kode, $document);
$document = str_replace("%%NOKODE%%", $nokode, $document);
$document = str_replace("%%TAHUN%%", $tahun, $document);
$document = str_replace("%%HOBI%%",  $rincian_hoby, $document);


header("Content-type: application/msword");
header("Content-disposition: inline; filename=doc.rtf");
header("Content-length: " . strlen($document));
echo $document;

?>

但是 %%HOBI%% 上的 str_replace 效果不佳
它只是替换表单中的最后一个输入,而不是全部
谁能告诉我怎么了
thx

【问题讨论】:

  • 您没有显示 doc.rtf 中的内容,这或许可以澄清情况。
  • rtf 文件很简单:` %%NAMA%% %%KODE%% %%NOKODE%% %%TAHUN%% %%HOBI%% ` 如果我输入动态形式,str_replace %%HOBI%% 只是预览最后一条记录,不是全部

标签: php arrays forms dynamic


【解决方案1】:

这可能是个错误...

       if(isset($_POST["rincian_hobi"]))
                {
                    $hoby=$_POST["rincian_hobi"];
                    reset($hoby);
                    while (list($key, $value) = each($hoby)) 
                        {
                            $rincian_hoby   =$_POST["rincian_hobi"][$key];
                        }
                }

在上面的代码中,你是 $rincian_hoby =$_POST["rincian_hobi"][$key];这是错误的。应该是,

           if(isset($_POST["rincian_hobi"]))
                {
                    $hoby=$_POST["rincian_hobi"];
                    reset($hoby);
                    while (list($key, $value) = each($hoby)) 
                        {
                            $rincian_hoby .= $_POST["rincian_hobi"][$key] . ",";
                        }
                    $rincian_hoby = substr($rincian_hoby,0,-1);
                }

【讨论】:

  • 你能再帮忙吗,如何将 $rincian_hoby 的结果垂直放置很多
  • 使用
    这样的标签代替 .= $_POST["rincian_hobi"][$key] 。 "
    ";然后 $rincian_hoby = substr($rincian_hoby,0,-5);
  • 谢谢你的回答,但这不起作用,-PS:输出是带有 ekstension .rtf 的文档 -
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
相关资源
最近更新 更多