doumubi6784 2018-01-25 16:20
浏览 83

使用动态输入将表单提交给PHP

I have a form which is empty in HTML file and I'm adding input elements to it using javascript but when I submit the form to PHP file (Controller.php) it doesn't send anything

HTML File

<form id="test" action="Controller.php" method="POST">
    <!-- QUESTIONS WILL APPEAR HERE-->
    <div id="questions"></div>
    <input type="submit" value="Finish">
</form>

JS File

var d1 = document.getElementById('questions');
        d1.insertAdjacentHTML('beforeend',
            "<div class='question'>"
            + "<h4 id='questionTitle'>Question Title goes here...</h4>"
            + '<ul>'
            + '<li id="ans1"><input type="radio" id="1st-option" name="selector">'
            + '<label for= "1st-option" >Answer 1</label>'
            + '<div class="check"></div>'
            + '</li>'


            + '<li id="ans1"><input type="radio" id="2nd-option" name="selector">'
            + '<label for= "2nd-option" >Answer 2</label>'
            + '<div class="check"></div>'
            + '</li>'


            + '<li id="ans1"><input type="radio" id="3rd-option" name="selector">'
            + '<label for= "3rd-option" >Answer 3</label>'
            + '<div class="check"></div>'
            + '</li>'


            + '<li id="ans1"><input type="radio" id="4th-option" name="selector">'
            + '<label for= "4th-option" >Answer 4</label>'
            + '<div class="check"></div>'
            + '</li>'
            + '</ul>');

PHP File

print_r($_POST);

The result of $_POST is an empty array Array ( ), I have tried to add elements to the form statically (in HTML) and it worked, but I can't do it from JS,IDK why this happens

  • 写回答

1条回答 默认 最新

  • donglv6747 2018-01-25 16:28
    关注

    Your inputs have no value attribute :

    Change :

    <input type="radio" id="1st-option" name="selector">

    to

    <input type="radio" id="1st-option" name="selector" value="your_value">

    For the "Answer 1" the value could be "answer1" for exemple, or "1st-option" like the id that you've set, or anything you want. But you must have something in it. Else it will be empty.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分