2015-09-07 80 views
0

我有一個將數據輸入到mysql數據庫的php文件。現在我想在成功插入數據時顯示一條JavaScript消息。但是,我無法執行JavaScript代碼。無法在PHP文件中執行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("Y-m-d 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>

+1

回聲「'; –

回答

0

script

echo '<script> run(); </scrit>'; 
         ^^^^^^ 

檢查拼寫這將是

echo '<script> run(); </script>'; 
0

腳本結束標記拼寫錯誤。更新如下:

echo '<script> run(); </script>'; 
0

您有拼寫錯誤</scrit>它必須是</script>

你可以試試這個:

echo '<script> alert("Data Inserted Successfully"); </script>'