当用户填写表单时,我有一些按钮会动态添加新问题。但是,我无法获得这些按钮的值来发布到后续的PHP页面(所有其他信息帖子都很好)。从由Javascript控制的HTML表单发布按钮值
在下面的例子中,我无法通过$ _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时,删除按钮,他们为什么要发布? – RobG