Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP变量的Javascript值存储中设置输入字段值_Javascript_Php - Fatal编程技术网

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

在PHP变量的Javascript值存储中设置输入字段值,javascript,php,Javascript,Php,我正在为问题的答案创建一个动态文本字段,用户可以从“答案”字段显示中选择该字段。当用户输入答案并提交表单时,php代码将用户的答案存储在名为$ans1的php变量中。提交后重新加载表单时,答案不会显示。我想在我动态创建的字段注释中显示答案。代码如下: <?php $ques1Err = $ans1Err = '' ;//variable to display error like field required $ques1 = $ans1 = '' ;// variable to stor

我正在为问题的答案创建一个动态文本字段,用户可以从“答案”字段显示中选择该字段。当用户输入答案并提交表单时,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>

//动态添加输入字段,供用户输入问题的答案
函数add_txt_field(){
var inpdiv=document.createElement(“DIV”);
setAttribute(“类”,“输入文本”);
var input=document.getElementById(“ans1_txt-input”);
input.appendChild(inpdiv);
var inp=document.createElement(“输入”);
输入设置属性(“类型”、“文本”);
输入设置属性(“id”、“ans1”);
输入.setAttribute(“类”,“文本框”);
输入setAttribute(“名称”、“ans1”);
inp.setAttribute(“值”,“值”);
inp-div.appendChild(inp);
}
创造

您的javascript有问题:

echo '<script> oFormObject.elements["ans1"].value = "$ans1" </script>' ;
对象元素[“ans1”]的
echo'。值=“$ans1””;
换成这个

echo '<script> document.getElementById("ans1").value = "'.$ans1.'"; </script>' ;
echo'document.getElementById(“ans1”).value=“”.$ans1.;”;
但您也可以在表单中从php执行以下操作:

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

您没有使用javascript,而是出现了PHP语法问题(您需要学习basic),html中没有一个元素带有
name=“of对象”