在PHP变量的Javascript值存储中设置输入字段值


Set input field value in Javascript value store in PHP variable

我正在为问题的答案创建一个动态文本字段,用户可以从答案字段显示中选择该字段。当用户输入答案并提交表单时,php代码将用户的答案存储在名为$ans1的php变量中。提交后重新加载表单时,不会显示答案。我想在我动态创建的字段注释中显示这个答案。代码如下:

<?php
$ques1Err = $ans1Err = '' ;//variable to display error like field required
$ques1 = $ans1 = '' ;// variable to store  value what user enter or select
$qans1 = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST"){
   if(empty($_POST['q1'])){
        $ques1Err = 'Security Question Required' ;
   }
   else{
        $ques1 = $_POST['q1'];
        $qans1 = 1;
   }
   if(empty($_POST['ans1'])){
        $ans1Err = 'Answer Required' ;
   }
   else{
        $ans1 = check_input($_POST['ans1']);
        if(!preg_match("/^[a-zA-Z ]*$/",$ans1)){
            $ans1Err = "Only letters and white space allowed";
        }
   }
}
?>
<html>
<head>
  <script>
      // dynamically add input field for the user to enter the answer of question
     function add_txt_field(){
        var inpdiv = document.createElement("DIV");
        inpdiv.setAttribute("class","input-txt");
        var input = document.getElementById("answers1_txt-input");
        input.appendChild(inpdiv);
        var inp = document.createElement("INPUT");
        inp.setAttribute("type","text");
        inp.setAttribute("id","answers1");
        inp.setAttribute("class","txt-box");
        inp.setAttribute("name","answers1");
        inp.setAttribute("value","");
        inpdiv.appendChild(inp);

    }
  </script>
</head>
 <body>
   <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
        Security Question<select class="txt-box" onchange="add_txt_field()" id="q1" name="q1">
        <option value="<?php echo $ques1 ?>"><?php echo $ques1 ?></option>
        <option value="What was your childhood nickname?">What was your childhood nickname?</option>
        <option value="What is your grandmother's first name?">What is your grandmother's first name?</option>
        <option value="What school did you attend for sixth grade?">What school did you attend for sixth grade?</option>
      </select><span class="error"> <?php echo $ques1Err;?></span></div>
      <div id="answers1_txt-input"></div> 
      <?php
                if($qans1 == 1){
                    echo '<script type="text/javascript"> add_txt_field() </script>';
                    ***// here's the problem***                  

                    echo '<script> oFormObject.elements["answers1"].value = "$ans1" </script>' ;
                }

                ?>
      <button id="create" name="create" type="submit">Create</button>
   </form>
  </body
</html>

您的javascript:出现问题

echo '<script> oFormObject.elements["answers1"].value = "$ans1" </script>' ;

将其更改为此

echo '<script> document.getElementById("answers1").value = "'.$ans1.'"; </script>' ;

但是您也可以在表单中从php中执行一些操作,例如:

<?php
   if (isset($ans1)) { 
      echo '<input type="text" name="answers1" id="answers1" value="'.$ans1.'"/>';
   }
?>

而不是使用javascript