duan41497 2018-06-18 16:30
浏览 47
已采纳

更新答案的PHP表单

I am currently working in PHP I am new to development in PHP and I am having quite a time trying to get a form to submit then update a value each time it is submitted.

So here is the gist of it. I am submitting my form to self at which time it runs this:

<div id="quiz">
    <form method="post" id="test" action="">
        <input type="hidden" value="<?php $question->set_results() ?>">
        Answer A<input type="radio" name="answer" value="a">
        Answer B<input type="radio" name="answer" value="b">
        Answer C<input type="radio" name="answer" value="c">
        Answer D<input type="radio" name="answer" value="d">
        <input type="submit">
    </form>

class test_results{

    public function get_results(){
        $this->lastanswer = array();
        $this->personsanswer = $_POST['answer'];
        $this->allanswers = array_push($this->lastanswer, $this->personsanswer);
        implode($this->lastanswer);
        echo $this->allanswers;


    }
}

After this runs it seems to work but instead of updating the variable it just adds a number where I wanted it to update. All in all I just want to be able to submit a form, after submitting a form I have a hidden field in the form and I want it to update that hidden field with the ALL of the previous options chosen. I have gotten it as far as to update one option at a time but not multiple times.

My end goal is to have a questionnaire form where users fill out answers one question at a time and each time they submit the form the hidden field holds ALL of the previous answer letters in it.

  • 写回答

1条回答 默认 最新

  • dongpu1331 2018-06-18 16:52
    关注

    set_results() is not specified in the question.

    You will need to take into account $_POST["answer"] inside set_results. Also, you will need to post the hidden field as well, otherwise you will lose your information. So, first, change your form to post the hidden field as well:

    <div id="quiz">
        <form method="post" id="test" action="">
            <input name="answers" type="hidden" value="<?php $question->set_results() ?>">
            Answer A<input type="radio" name="answer" value="a">
            Answer B<input type="radio" name="answer" value="b">
            Answer C<input type="radio" name="answer" value="c">
            Answer D<input type="radio" name="answer" value="d">
            <input type="submit">
        </form>
    

    and then you will need to calculate something like

    $_POST["answers"].($_POST["answers"] ? "," : "").$_POST["answer"]
    

    somewhere, so you will get the correct result, returned to the hidden field as value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行