dpmrakfbx820320638 2018-11-08 08:56
浏览 49

php中的多步形式

i am having problem with this multi step form i want to create with php. Within each form, you can store the current step — so that the script knows what stage the user has reached — as well as the data already entered by the user in other steps. when the user submits the current step the script runs the function to take the user to another stage. the inputs with "name" firstName, lastNmae and comments works perfectly fine but the select box and the radio button those not retain the value when the user goes back to the previous step... what do i do...

<!DOCTYPE html>
<html>
<head>
    <title>membership form</title>
</head>
<body>

      <?php  

      if (isset($_POST["step"]) AND $_POST["step"] >= 1 AND $_POST["step"] <= 3 ) { // note that this will only return true when the form containing the input with name "step" has been submitted..dont get confused that the input has its attr of "value" already set eg "value = 1"; that is why if you load the page the "displayStep1();" runs.

      call_user_func("processStep" .(int)$_POST["step"]); 

      }else{
        displayStep1();
      }

      function setInputValue($fieldName){
            if (isset($_POST[$fieldName])) {
                echo $_POST[$fieldName];
            }
        }

     function setChecked($fieldName , $fieldValue){
        if (isset($_POST[$fieldName]) AND $_POST[$fieldName] == $fieldValue) {
            echo 'checked = "checked" ';
        }
     }

     function setSelected($fieldName , $fieldValue){

        if (isset($_POST[$fieldName]) AND $_POST[$fieldName] == $fieldValue) {
            echo 'select = "selected"';
        }
     }


      function displayStep1(){?>

        <form action="registration_multistep.php" method="post">
            <h1>Member Signup: Step 1</h1>
            <div  style="width: 30em;">

                <!--to keep track of the steps-->
                <input type="hidden" name="step" value="1"> <!-- note that even if u set the value of a form element in the attr "value", php script will not return true unless the form has been submitted-->

             <!--notice the input for gender is only 1 here and the value is what the setInputValue() function echoed and the only argument passed is 'gender' for $fieldValue--> 
             <label></label><input type="hidden" name="gender" value=" <?php setInputValue('gender'); ?>" ><br><br>

                <label for = "favourite"></label><input type="hidden" name="favourite" id="favourite" value = "<?php setInputValue('favourite');?>" > <br><br>

                <label for = "comments"></label><input type="hidden" name="comments" id="comments" value="<?php setInputValue('comments'); ?>"><br><br>
                <label for = "password"></label><input type="hidden" name="password" id="password"><br><br>
                <label for = "passwordRetype"></label><input type="hidden" name="passwordRetype" id="passwordRetype"><br><br>

                <label for = "firstName">first name</label><input type="text" name="firstName" id="firstName" value="<?php setInputValue('firstName'); ?>"><br><br>
                <label for = "lastName">last name</label><input type="text" name="lastName" id="lastName" value="<?php setInputValue('lastName'); ?>"><br><br>
                <input type="submit" name="submitButton" value="next &gt;">
            </div>
        </form>

     <?php } ?>

     <?php
        function processStep1(){
            displayStep2();
        }

        function displayStep2(){?>

            <form action="registration_multistep.php" method="post">
            <h1>Member Signup: Step 1</h1>
            <div  style="width: 30em;">
                <input type="hidden" name="step" value="2"> <!-- note that even if u set the value of a form element in the attr "value", php script will not return true unless the form has been submitted-->
                your gender<br>

             <label for = "male">male</label><input type="radio" name="gender" id="male" value="male" <?php setChecked('gender' , 'male'); ?> >  <br><br>
             <label for = "female">female</label><input type="radio" name="gender" id="female" value="female"  <?php setChecked('gender' , 'female'); ?> > <br><br>

                <select name="favourite">

                    <option value="default" <?php setSelected('favourite' , 'default');?> >selected</option>
                    <option value="rice" <?php setSelected('favourite' , 'rice'); ?> >rice</option>
                    <option value="beans" <?php setSelected('favourite' , 'beans'); ?> >beans</option>

                </select>
                <label for = "comments"></label><input type="hidden" name="comments" id="comments"  value="<?php setInputValue('comments'); ?>"><br><br>

                <label for = "firstName"></label><input type="hidden" name="firstName" id="firstName"  value="<?php setInputValue('firstName'); ?>"><br><br>
                <label for = "lastName"></label><input type="hidden" name="lastName" id="lastName"  value="<?php setInputValue('lastName'); ?>"><br><br>

                <input type="submit" name="submitButton" id ="back" value="&lt; back">
                <input type="submit" name="submitButton" id ="next" value="next &gt;">
            </div>
        </form>


        <?php }?>

        <?php
        function processStep2(){

            if (isset($_POST["submitButton"]) AND $_POST["submitButton"] == "< back") {
                displayStep1();
            }else{
                displayStep3();
            }
        }

        function displayStep3(){?>

            <form action="registration_multistep.php" method="post">
            <h1>Member Signup: Step 1</h1>
            <div  style="width: 30em;">
                <input type="hidden" name="step" value="3"> <!-- note that even if u set the value of a form element in the attr "value", php script will not return true unless the form has been submitted-->


                 <!--notice the input for gender is only 1 here and the value is what the setInputValue() function echoed and the only argument passed is 'gender' for $fieldValue--> 
             <label></label><input type="hidden" name="gender" value=" <?php setInputValue('gender'); ?>" ><br><br>

                <label for = "favourite"></label><input type="hidden" name="favourite" id="favourite" value="<?php setInputValue('favourite'); ?>"><br><br>
                your comment<br>
                <label for = "comments"></label><input type="text" name="comments" id="comments"  value="<?php setInputValue('comments'); ?>"><br><br>

                <label for = "firstName"></label><input type="hidden" name="firstName" id="firstName"  value="<?php setInputValue('firstName'); ?>"><br><br>
                <label for = "lastName"></label><input type="hidden" name="lastName" id="lastName"  value="<?php setInputValue('lastName'); ?>"><br><br>

                <input type="submit" name="submitButton" id ="back" value="&lt; back">
                <input type="submit" name="submitButton" id ="next" value="next &gt;">
            </div>
        </form>


        <?php }?>

        <?php


        function processStep3(){

            if (isset($_POST["submitButton"]) AND $_POST["submitButton"] == "< back" ) {
                displayStep2();
            }else{
                displayThanks();
            }
        }

        function displayThanks(){
            echo "successful";
        }

        ?>









</body>
</html>
  • 写回答

1条回答 默认 最新

  • douji9734 2018-11-08 09:52
    关注

    It is not :

    echo 'select = "selected"';
    

    It should be :

    echo 'selected = "selected"';
    

    Also to make use of HTML5 autocomplete features you can use autocomplete="on" on each form:

    <form action="registration_multistep.php" method="post" autocomplete="on">
    
    
    <form action="registration_multistep.php" method="post"  autocomplete="on">
    
    <form action="registration_multistep.php" method="post" autocomplete="on">
    

    It will help on select boxes. But I guess it won't work on radio buttons though. You can change that to 'select box' if you don't want to use php sessions or javascript between steps.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度