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 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样