[英]Unable to Execute Javascript Code In A PHP File
我有一個將數據輸入到mysql數據庫中的php文件。 現在,我想在成功插入數據后顯示javascript消息,但是我無法執行javascript代碼。
下面是我的代碼。
<html> <head> <title></title> <script type="text/javascript"> function run(){ alert("Data Inserted Successfully"); } </script> </head> <body> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "datacentre"; $first=$_POST['firstname'];//this values comes from html file after submitting $last=$_POST['lastname']; $dept= $_POST['department']; $unit= $_POST['unit']; $request=$_POST['request']; $purpose=$_POST['purposebuttons']; $accessedby = $_POST['personbuttons']; $description=$_POST['description']; $accessdate = $_POST['date-time']; /* Get Current Date and Time for the bookking_time field */ $booking_time=new DateTime(); $booking_time = $booking_time -> format("Ymd H:i:s"); // Create connection $con = new mysqli($servername, $username, $password, $dbname , 3306); if ($con->connect_error) { die("Connection failed: " . $con->connect_error); } //mysql_select_db("$database", $con); $sql= "INSERT INTO data_centre_users (first_name,last_name,department, unit, request, purpose , accessed_by, description,booking_time,access_time) VALUES ('$first','$last','$dept', '$unit','$request','$purpose', '$accessedby' ,'$description', NOW() , '$accessdate')"; if ($con->query($sql) === TRUE) { /* Calling the javascript code */ echo '<script> run(); </scrit>'; } else { echo "Error: " . $sql . "<br>" . $con->error; } $con->close(); ?> </body> </html>
腳本結束標記拼寫錯誤。 如下更新:
echo '<script> run(); </script>';
檢查script
拼寫
echo '<script> run(); </scrit>';
^^^^^^
這將是
echo '<script> run(); </script>';
您拼錯了</scrit>
它一定是</script>
。
您可以嘗試以下方法:
echo '<script> alert("Data Inserted Successfully"); </script>'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.