我有一个问题想弄清楚如何从 html 制作动态 pdf。我有一个表单,用户可以在需要时添加更多字段,但我不知道如何将它添加到 html 中以实现动态。预期结果:
目前一切都是硬编码的,而不是我想要的动态。
格式.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<style type="text/css">
div{
padding:8px;
}
</style>
</head>
<body>
</head>
<body>
<form method="post" action="html_pdf.php">
<div id="p_scents">
<p class="scent">
<label for="p_scnts1">Input Value</label>
<input type="text" size="20" name="p_scnt1"/>
<label for="p_more1">Input more</label>
<input type="text" size="20" name="p_more1"/>
<a href="#" class="addScnt">Add</a>
<a href="#" class="remScnt">Remove</a>
</p>
</div>
<input type='submit' value='submit' name="submit" id='submit'>
</form>
</form>
</body>
<script type="text/javascript">
$(function() {
var scntDiv = $('#p_scents');
$('.addScnt').bind('click', function() {
var line=$('p:eq(0)',scntDiv).clone(true).insertAfter($(this).closest('p.scent'));
$(':input:eq(0)',line).val('').focus();
$i = 1;
$('p',scntDiv).each(function(i){
$(':input',this).each(function(){
this.id=this.name = this.name.match(/^[a-z_]*/)[0]+(i+1);
});
});
return false;
});
$('.remScnt').bind('click', function() {
$(this).parents('p').remove();
return false;
});
});
html_pdf.php
<?php
if(isset($_POST['submit'])) {
$darbss = $_POST['p_scnt1'];
$stundass = $_POST['p_more1'];
$darbss2 = $_POST['p_scnt2'];
$stundass2 = $_POST['p_more2'];
$darbss3 = $_POST['p_scnt3'];
$stundass3 = $_POST['p_more3'];
}
?>
///////////////////////////
<body>
<div class="container">
<table class="table">
// I WANT THIS TO REPEAT DYNAMYCALY
<thead>
<tr>
<th><?php echo $darbss ?></th>
<th><?php echo $stundass ?></th>
</tr>
</thead>
<thead>
<tr>
<th><?php echo $darbss2 ?></th>
<th><?php echo $stundass2 ?></th>
</tr>
</thead>
<thead>
<tr>
<th><?php echo $darbss3 ?></th>
<th><?php echo $stundass3 ?></th>
</tr>
</thead>
</table>
最佳答案
因为它们是动态输入,所以你需要让它们的名字也动态化
所以你需要用[]来命名它们,这样你就可以循环访问它们
例如,如果您有名字和姓氏,则可以是
<input type="text" name="myinputs[0][first_name]">
<input type="text" name="myinputs[0][last_name]">
将其用作一组输入,因此当您添加另一组时,您只需增加索引
然后你可以通过 $_POST['myinputs'] 从 php 访问它们 你可以循环它
foreach($_POST['myinputs'] as $input) {
// first name will be $input['first_name']
// last name will be $input['last_name']
}
关于javascript - 动态 html/php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50544380/