动态创建的脚本不起作用

时间:2018-01-04 16:52:00

标签: javascript php jquery echo

我正在尝试动态创建脚本,但它无法正常工作。

我试过了:

echo '<script type="text/javascript">alert("hello!");</script>';
echo '<script>alert("hello!");</script>';
echo '<script type="text/javascript">$( document ).ready(function() {alert("hello!");});</script>';

请帮助

根据请求,这里是完整代码,其中脚本是动态创建的: 事实上,有一个php页面有一个javascript文件,按下发送类型的按钮调用ajax函数并调用此php页面,这个将动态创建带有正确图片的bootstrap轮播(给出正确的类别)

<?php

//the login database info goes here too

    $_type= $_POST['Type'];
$_category= $_POST['Category'];



if($_type == "adult")
{
    $title = "FOR THE ADULTS";

}
else
    $title = "FOR THE KIDS";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT image,imagealt FROM gallery where type='".$_type."' AND category='".$_category."' ";

$result = $conn->query($sql);
$result1 =  $conn->query($sql);


?>


<tr>
    <td class="GalleryImageInfo">
        <div id="parent_back" >
          <h1>GALLERY</h1>
          <p class="ForAdults"> <?php echo $title ?>  </p>
          <p class="ButtonGalleryPreview" style="text-align:center"> <?php echo $_category ?> </p>


<?php

$countit = 0;

if ($result->num_rows > 0) {
    // output data of each row

?>

<div id="slider_caption" > <div>

<?php
    while($row = $result->fetch_assoc()) {
        $image_description = $row["imgdesc"];
     echo "
                        <div  class=\"carousel-caption caption-".$countit."\">
                            <h3>Description</h3>
                            <p>".$image_description."</p></div>";

                        $countit++;
    }

echo "</div></div>";


echo "<p  class=\"GalleryImageBack\" style=\"font-family:BandaBold; color:#6c555e; font-size:15px;\"><i class=\"fa fa-arrow-circle-left\" style=\"margin-right:10px; font-size:15px;\" ></i>BACK</p>
</div>
</td>


<td style=\"width:100%\">
<div class=\"container\" style=\"width:100% ; padding:0px !important;\">

  <div id=\"myCarousel2\" class=\"carousel slide\" data-ride=\"carousel\">
    <!-- Wrapper for slides -->
    <div class=\"carousel-inner\">";


$firstitem = true;


    while($row = $result1->fetch_assoc()) {
        //echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

        $image_url = $row["image"];
        $image_alt = $row["imagealt"];

        if($firstitem)
       { echo"<div class=\"item active\">
        <img src=\" $image_url \"
        alt=\".$image_alt.\" style=\"width:100%;\">
      </div>";
      $firstitem= false;
        }

        else
       echo"<div class=\"item\">
        <img src=\" $image_url \"
        alt=\".$image_alt.\" style=\"width:100%;\">
      </div>";

    }


    echo " </div>

    <!-- Left and right controls -->
    <a class=\"left carousel-control\" href=\"#myCarousel2\" data-slide=\"prev\">
      <span class=\"glyphicon glyphicon-chevron-left\"></span>
      <span class=\"sr-only\">Previous</span>
    </a>
    <a class=\"right carousel-control\" href=\"#myCarousel2\" data-slide=\"next\">
      <span class=\"glyphicon glyphicon-chevron-right\"></span>
      <span class=\"sr-only\">Next</span>
    </a>
  </div>
</div>
</td>

</tr>";



echo "<script type=\"text/javascript\">$(document).ready(function() {
    alert(\"hello!\");
});</script>";

} 
 else {
    echo "0 results";
}
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

检查是否在此页面中添加了jquery脚本。