包含的HTML文件javascript无法正常工作

时间:2018-08-22 16:33:48

标签: javascript php html

我的程序的场景是当我提交表单时,表单操作转到xyz.php。 在xyz.php中,我只是使用了 INCLUDE("abc.html")重定向到另一个HTML页面,现在abc.html包含一些HTML标签以及“ JAVASCRIPT” ,但是当我的表单提交后,页面重定向到了{{ 1}}。 abc.html包含HTML和Javascript代码。 HTML内容可见,但javascript无法正常工作,

我是PHP新手。谢谢

下面是“ student.php”文件

abc.html

这里是“ teachdata.html”

<html>
<head>
   <title>Marks Entering</title>
</head>  
<body>
   <?php 

     $con1= mysqli_connect('localhost','root','','student');
     $con2= mysqli_connect('localhost','root','','first_year');
     $con3= mysqli_connect('localhost','root','','computer_dep');



     if(isset($_POST['submit']))
     {  

        $query1 = "SELECT MAX(`ID`) as 'ID', `Last_Name`, `Middle_Name`, `first_Name`, `DOB`, `Department`, `Semester` FROM `student`";
        $run = mysqli_query($con1,$query1);




        if($run == TRUE)
        {
            $data = mysqli_fetch_assoc($run);
            $ID=$data['ID'];
            $name= $data['Last_Name'] . " " . $data['Middle_Name'] . " " .$data['first_Name'];
            $sem= $data['Semester'];


            switch($sem)
            {
                case 1:
                echo "sem1 not have mark";

                    break;

                case 2:

                    $m1=$_POST['m_i'];
                    $ph=$_POST['ph'];
                    $bee=$_POST['bee'];
                    $em=$_POST['em'];
                    $evs=$_POST['evs'];
                    $cm=$_POST['cm'];



                    $query2="INSERT INTO `sem1`(`Id`,`Name`,`M1`,`PHYSICS1`,`CHEM1`,`BEE`,`EM`,`EVS`) VALUES ($ID,'$name',$m1,$ph,$cm,$bee,$em,$evs)";

                    $run1 = mysqli_query($con2,$query2);

                    if($run1 == TRUE)
                    {
                        echo "<script type='text/javascript'>
                        alert('Submitted Successfully!!!!')" ;

                        include("teachdata.html");
                    }
                    else {
                            echo mysqli_error($con2);
                        }

                    break;      
            }




        }   

     } 

     ?>

     </body>
     </html>

下面是“ teachtab.js”

    <html>
<head>
    <title>Data Entry</title>
    <link rel="stylesheet" type="text/css" href="teachdata.css">
    <script src="teachtab.js"></script>
</head>

<body>
    <h1> Welcome teacher section.....</h1>

    <div class="solid">

            <div class="tab">
                 <hr>
                <p style="margin-left: 40px; text-width:5px ; text-shadow: 0 0 3px  #ffb3b3, 0 0 5px  #ffe6e6;"><b> Menu</b> </p>
                <hr style="color:white">
                <button id="defaultOpen" class="tablink" onclick="opentab(event,'home')">Home</button>
                <button class="tablink" onclick="opentab(event,'Info')">Add Information</button>
                <button class="tablink" onclick="opentab(event,'Marks')">Add Marks</button>
                <button class="tablink" onclick="opentab(event,'Update')">Update</button>

            </div>

            <div id="home" class="tabcontent">
                <hr>
                   <p style="text-shadow: 0 0 3px #FF0000; text-align:center;">Instruction's</p>
                 <hr>

            </div>

            <div id="Info" class="tabcontent">
                  <hr>
                   <p style="text-shadow: 0 0 3px #FF0000; text-align:center;">Information</p>
                  <hr> 

                  <form action="student.php" method="post">

                    <label>Last Name:</label> <input type="text" name="lname" class="space"> 
                    <label>Middle Name:</label>  <input type="text" name="mname" class="space"> 
                    <label>first Name:</label>  <input type="text" name="fname" class="space"></br></br>



                    <label > DOB:</label>
                    <input type="date" name="bday"> </br></br>

                    <label>Department:</label>  
                    <select name="department" class="space">
                        <option value="comp">Computer Engineering</option>
                        <option value="it">IT Engineering</option>
                        <option value="civil">Civil Engineering</option>
                        <option value="mech">Mechanical Engineering</option>
                        <option value="extc">Electronics Engineering</option>
                        <option value="auto">Automobile Engineeering </option>

                    </select></br></br>

                    SEM:
                    <select name="semno">
                        <option value="1">SEM I</option>
                        <option value="2">SEM II</option>
                        <option value="3">SEM III</option>
                        <option value="4">SEM IV</option>
                        <option value="5">SEM V</option>
                        <option value="6">SEM VI</option>
                        <option value="7">SEM VII</option>
                        <option value="8">SEM VIII</option>

                    </select>   


                    <button type="submit" name="submit">Submit</button> 





            </div>
            <div id="Marks" class="tabcontent">
                   <hr>
                   <p style="text-shadow: 0 0 3px #FF0000; text-align:center;">Marks</p>
                  <hr> 
            </div>

            <div id="Update"  class="tabcontent">
                    <hr>
                   <p style="text-shadow: 0 0 3px #FF0000; text-align:center;">Update</p>
                  <hr> 
            </div>





    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

好的,这是最简单的解决方案,但不好保留。

在主脚本中包含js:

<html>
<head>
   <title>Marks Entering</title>
   <link rel="stylesheet" type="text/css" href="teachdata.css">
   <script src="teachtab.js"></script>
</head>  
<!-- keep the rest -->

在teacherdata.html中,摆脱<html><body><head>的所有内容,

<h1> Welcome teacher section.....</h1>

<!-- to -->

</div>

但是,重定向到teachdata.html(或使其成为php脚本teachdata.php并在其中包含一条消息)会更好。

    if($run1 == true) {
        header("Location: teachdata.php?msg=success");
    } else {
    //....

在teacherdata.php中,您现在可以将消息内嵌显示为普通的html

   <?php
   if (isset($_GET['msg'])) {
       echo "<div>Message: " . $_GET['msg'] . "</div>";
   }
   ?>

,或者如果您确实希望这样做,则发出普通的旧JavaScript警报:

   <?php
   if (isset($_GET['msg'])) {
       echo "<script> alert('" . $_GET['msg'] . "'); </script>";
   }
   ?>

更多注释:

  • 请查看准备好的角色。您的代码不安全,开放 到sql注入(Google“ Bobby Tables”和“ Sql Injection”)。
  • 您确定您的应用需要3个不同的数据库吗?