动态创建的脚本不起作用

问题描述 投票:1回答:1

我试图动态创建一个脚本,但它无法正常工作。

我试过了:

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();
?>
javascript php jquery echo
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.