从由Javascript

时间:2016-08-19 01:06:31

标签: javascript php html forms post

当用户填写表单时,我有一些动态添加新问题的按钮。但是,我无法将这些按钮的值发布到后续的PHP页面(所有其他信息都很好)。

在下面的示例中,我无法获得" add_email"的价值。 in" process-form.php"通过$ _POST [' 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 &raquo;">
</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

1 个答案:

答案 0 :(得分:0)

您通过使用

覆盖输入来破坏表单
table_row.innerHTML = '<input id="email" name="email" type="text" placeholder="Email">';

因此可能更改输入名称以匹配

table_row.innerHTML = '<input id="email" name="add_email" type="text" placeholder="Email">';