str_replace用于动态表单


str_replace for dynamic form

我使用这个动态表单

?>
<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

这可能是个错误。。。

       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_hoobi"][$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);
                }