php - Form validation with javascript with multiple input type name -
i want make quiz . first of make quiz insert. want make validation fill values in form , answer must in option ... try codeigniter framework . view
<?php include_once('header.php'); ?> <?php include_once('tnavbar.php'); ?> <script src="<?php echo base_url();?>/js/jquery-1.9.1.min.js"></script> <script> function validateform1() //this javascript function not working { var j=<? echo $number;?>; for(var i=1; i<= j ; i++) var qus1=document.forms["myform1"]["ques[i]"].value; var opt1=document.forms["myform1"]["opt1[i]"].value; var opt2=document.forms["myform1"]["opt2[i]"].value; var opt3=document.forms["myform1"]["opt3[i]"].value; var ans1=document.forms["myform1"]["ans1[i]"].value; if ((qus1==null || qus1=="")||(opt1==null || opt1=="")||(opt2==null || opt2=="")|| (opt3==null || opt3=="")||(ans1==null || ans1=="")) { alert("all field must fill"); return false; } } </script> <div class="maincontent_area"> <div class="container"> <div class="row"> <div class="span12"> <form name="myform1" action="<? echo base_url()."/index.php/tmcq_test/insertmcq";?>" onsubmit="return validateform1()" method="post"> <?php for($i=1; $i<= $number; $i++){ //here $number user input 2,3, or 4 etc number of mcq ?> question : <input type="text" name="ques[<?php echo $i;?>]"><br/> option 1 : <input type="text" name="opt1[<?php echo $i;?>]" ><br/> option 2 : <input type="text" name="opt2[<?php echo $i;?>]" ><br/> option 3 : <input type="text" name="opt3[<?php echo $i;?>]" ><br/> answer : <input type="text" name="ans[<?php echo $i;?>]" ><br/> <br/> <?php //echo $value->answer; } ?> <input type="hidden" name="quiz_name" value="<?php echo $name;?>"> <input type="hidden" name="sub_id" value="<?php echo $sub_id;?>"> <input type="hidden" name="quiz_number" value="<?php echo $number;?>"> <input type="hidden" name="time_number" value="<?php echo $time_number;?>"> <input type="submit" name="submit" id="submit" value="create" class="btn btn-info"> </form> <a href="<? echo base_url()."/index.php/tmcq_test";?>"> <button class="btn btn-info"> exit </button> </a> </div> </div> </div> how can add form validation in view javascript add js code not working how implement ? can
you should update variable assignment. in case js looking form element named "ques[i]", "opt1[i]", "ans1[i]" etc. need make letter "i" variable. try one:
var qus1=document.forms["myform1"]["ques[" + + "]"].value; var opt1=document.forms["myform1"]["opt1[" + + "]"].value; var opt2=document.forms["myform1"]["opt2[" + + "]"].value; var opt3=document.forms["myform1"]["opt3[" + + "]"].value; var ans1=document.forms["myform1"]["ans1[" + + "]"].value; this works me: http://jsfiddle.net/kxwbr/1/
Comments
Post a Comment