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

时间:2014-09-05 19:46:04

标签: javascript php

我为问题的答案创建动态文本字段,用户从答案字段显示中选择。当用户输入答案并提交表单时,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("ans1_txt-input");
        input.appendChild(inpdiv);

        var inp = document.createElement("INPUT");
        inp.setAttribute("type","text");
        inp.setAttribute("id","ans1");
        inp.setAttribute("class","txt-box");
        inp.setAttribute("name","ans1");
        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="ans1_txt-input"></div> 
      <?php

                if($qans1 == 1){
                    echo '<script type="text/javascript"> add_txt_field() </script>';

                    ***// here's the problem***                  


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

                }


                ?>

      <button id="create" name="create" type="submit">Create</button>

   </form>
  </body
</html>

1 个答案:

答案 0 :(得分:1)

您的javascript中存在问题:

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

将其更改为此

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

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

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

而不是使用javascript

相关问题