【发布时间】:2016-08-19 09:26:32
【问题描述】:
当用户填写表单时,我有一些按钮可以动态添加新问题。但是,我无法将这些按钮的值发布到后续的 PHP 页面(所有其他信息都可以发布)。
在下面的示例中,我无法通过 $_POST['add_email']; 获取“process-form.php”中“add_email”的值;
提前感谢您的帮助。在简化的形式中,它看起来像这样:
HTML 表单
<form id="form" class="form" action="process-form.php" method="POST">
<input id="name" name="name" type="text" placeholder="Name">
<!-- Yes/No buttons asking if the user wants to enter their email-->
<div id="form_email">
<h4>Do you want to enter your email?</h4>
<input type="button" class="btn" id="add_email" name="add_email" value="Yes" onclick="showEmailQ(this.value)"></input>
<input type="button" class="btn" id="add_email" name="add_email" value="No" onclick="showEmailQ(this.value)"></input>
</div>
<input type="submit" class="btn" value="Submit »">
</form>
Javascript
<script>
//Function to process whether user wants to enter email and then display value
function showEmailQ(value){
var table_row = document.getElementById("form_email");
if(value == "Yes"){
table_row.innerHTML = '<input id="email" name="email" type="text" placeholder="Email">';
}
else{
table_row.innerHTML = '<p>You have chosen not to enter your email</p>';
}
}
</script>
PHP
//process-form.php
session_start();
$enteredEmail = $_POST['add_email'];
echo $enteredEmail; // Nothing prints to screen
【问题讨论】:
-
showEmailQ 在替换 form_email div 的 innerHTML 时删除按钮,为什么要发布?
标签: javascript php html forms post