我使用这种动态形式
?>
<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,效果不佳 它只是替换表单中的最后一个输入,而不是全部 有人可以告诉我错误的是什么
答案 0 :(得分: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);
}