【问题标题】:Posting button value from an HTML form controlled by Javascript从 Javascript 控制的 HTML 表单中发布按钮值
【发布时间】: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 &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

【问题讨论】:

  • showEmailQ 在替换 form_email div 的 innerHTML 时删除按钮,为什么要发布?

标签: javascript php html forms post


【解决方案1】:

你通过覆盖输入来破坏你的表单

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">';

【讨论】:

  • ahhhh 这很有意义....我只需要找到一种方法来保留这两个值。我会努力的,谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-07-08
  • 2013-03-31
  • 1970-01-01
  • 2013-11-10
  • 2012-02-14
  • 2014-12-10
  • 2014-04-28
  • 2013-02-16
相关资源
最近更新 更多