0

I'm creating a dynamic text field for the answer of a question, which the user selects from the answer field display. When the user enters the answer and submits the form, the php code stores the user's answer in php variable named $ans1. When the form reloads after submitting, the answer doesn't show. I want to show that answer in the field note that I'm creating dynamically. The code is given below:

<?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
  • 1
    You have PHP syntax glitches (you need to learn basic PHP string syntax), and nowhere in your html do you have an element with a name="oFormObject" Commented Sep 5, 2014 at 19:54

1 Answer 1

2

You have a problem in your javascript:

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

Change it to this

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

But you can also do something from php inside the form like:

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

Instead of using javascript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.