php – submit form in iframe using javascript-ThrowExceptions

Exception or error:

I not well known of iframes. I want to submit a form which is in a iframe(f2) from the another iframe(f3) using java script. Don’t know how to implement and the form has two types of submit buttons used by the condition.

content in iframe(f2):

<?php
include 'database.php';
$query = 'select start from time where id =1';
$result = mysqli_query($con,$query);
$time = mysqli_fetch_assoc($result);
$timer = $time['start'];
//echo $timer;
echo "<br>";
?>

<html>
   <head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
      <title>JavaScript Dates</title>
   </head>
   <body>
   <p id="progressBar" align="right"></p>
   <!--<form id="myForm" action="">
  <input type="button" onclick="myFunction()" value="Submit form">
</form>-->

<script>
function myFunction() {
  //document.getElementById("myForm").submit();
  //var form = window.opener.document.getElementById("theFormID");
  //window.opener.document.getElementById("form1").submit();
  var ifrm = parent.document.getElementById('f3');
  var win = ifrm.contentWindow;
  var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
  doc.getElementsByName('myfm').submit();

}
</script>
      <script>
         function myTime(time) {
            var hr = ~~(time / 3600);
            var min = ~~((time % 3600) / 60);
            var sec = time % 60;
            var sec_min = "";
            if (hr > 0) {
               sec_min += "" + hr + ":" + (min < 10 ? "0" : "");
            }
            sec_min += "" + min + ":" + (sec < 10 ? "0" : "");
            sec_min += "" + sec;
            return sec_min+ " min";
         }
         //document.write(myTime(<?php echo $timer ?>));



        var timeleft = <?php echo $timer ?>;
        var downloadTimer = setInterval(function(){
        if(timeleft <= 0){
            clearInterval(downloadTimer);
            myFunction();
            }
        //document.getElementById("progressBar").innerHTML =  timeleft;
        document.getElementById("progressBar").innerHTML = "<h3>Time Remaining:<br>"+(myTime(timeleft))+"&nbsp&nbsp&nbsp&nbsp&nbsp</h3>";
        timeleft -= 1;
        }, 1000);
      </script>




    <script>
    function fetchdata(){
        console.log(timeleft,"timeleft");
        $.ajax({
        url: 'fetch.php',
        type: 'POST',
        data: {count : timeleft},
        success: function(data){
   // Perform operation on return value
        console.log(data,"from ajax");
        },
        complete:function(data){
            setTimeout(fetchdata,10000);
            }
        });
    }

    $(document).ready(function(){
        setTimeout(fetchdata,10000);
        //console.log(thisId,"thisId");
    });
    </script>
   </body>
   </html>

I want to access form from myfunction() in above content and has to pass a string Get result with submit variable to iframe(f3).

contennt in iframe(f3):

<?php
session_start();
error_reporting(1);
include("database.php");
extract($_POST);
extract($_GET);
extract($_SESSION);
$day = date("d/m/Y");

if(isset($subid) && isset($type))
{
$_SESSION[sid]=$subid;
$_SESSION[type]=$type;
header("location:quiz.php");
}
if(!isset($_SESSION[sid]) || !isset($_SESSION[type]))
{
    //header("location: index.php");
    echo '<a id="link" target="_parent" href="index.php"></a>

<script type="text/javascript">
    document.getElementById("link").click();
</script>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Online Quiz</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="quiz.css" rel="stylesheet" type="text/css">

<style type="text/css">
    #nextQ{
        border: 2px solid black;
        background-color: white;
        padding: 1px 5px;
        font-size: 16px;
        cursor: pointer;
        border-color: #4CAF50;
        color: green;
        align: center;
        margin: 0 auto;
        position:relative;
        display: inline-block;
    }
    #getQ{
        border: 2px solid black;
        background-color: #4CAF50;
        padding: 3px 8px;
        font-size: 16px;
        cursor: pointer;
        border-color: #4CAF50;
        color: white;
        align: center;
        margin: 0 auto;
        position:relative;
        display: inline-block;
    }
</style>
</head>

<body>
<?php
//include("header.php");

$query="select * from mst_question";

$rs=mysqli_query($con,"select * from mst_question where type='$type'",$cn) or die(mysqli_error());
echo '';
?>
<?php
if(!isset($_SESSION[qn]))
{
    $_SESSION[qn]=0;
    mysqli_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysqli_error());
    $_SESSION[trueans]=0;

}
else
{
        if($submit=='Next Question' && isset($ans))
        {
                mysqli_data_seek($rs,$_SESSION[qn]);
                $row= mysqli_fetch_row($rs);
                mysqli_query($con,"insert into mst_useranswer(sess_id, type, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', '$type','$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysqli_error());
                if($ans==$row[7])
                {
                            $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                $_SESSION[qn]=$_SESSION[qn]+1;
        }
        else if($submit=='Get Result' && isset($ans))
        {
                mysqli_data_seek($rs,$_SESSION[qn]);
                $row= mysqli_fetch_row($rs);
                mysqli_query($con,"insert into mst_useranswer(sess_id, type, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', '$type','$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysqli_error());
                if($ans==$row[7])
                {
                            $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                echo "<h1 class=head1> Result</h1>";
                $_SESSION[qn]=$_SESSION[qn]+1;
                echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
                echo "<tr class=tans><td>Correct Answer<td>".$_SESSION[trueans];
                $w=$_SESSION[qn]-$_SESSION[trueans];
                echo "<tr class=fans><td>Wrong Answer<td> ". $w;
                echo "</table>";
                $sql = mysqli_query($con,"insert into mst_result(login,type,test_date,score)
                                                     values('$login','$type','$day','$_SESSION[trueans]')");
                                                     if (!$sql) {
                                                        echo "".mysqli_error($con);
                                                     }

                echo "<h1 align=center><a href=review.php> Review Question</a> </h1>";
                unset($_SESSION[qn]);
                unset($_SESSION[sid]);
                unset($_SESSION[type]);
                unset($_SESSION[trueans]);
                exit;
        }
}
$rs=mysqli_query($con,"select * from mst_question where type='$type'",$cn) or die(mysqli_error());
if($_SESSION[qn]>mysqli_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error  Occured</h1>";
session_destroy();
//echo "Please <a href=index.php> Start Again</a>";
echo 'Please <a id="link" target="_parent" href="index.php">Start Again!!!</a>';
exit;
}
mysqli_data_seek($rs,$_SESSION[qn]);
$row= mysqli_fetch_row($rs);
echo "<form name='myfm' id='myfm' method=post action=quiz.php>";
echo "<table width=100%> <tr> <td width=30>&nbsp;<td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]";

if($_SESSION[qn]<mysqli_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit id='nextQ' value='Next Question'></form>";
else
echo "<tr><td><input type=submit name=submit id='getQ' value='Get Result'></form>";
echo "</table></table>";
?>
</body>
</html>

Anyone can help me plz!!!……….

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *