简体   繁体   中英

Unable to Execute Javascript Code In A PHP File

I have a php file that inputs data into mysql database. Now I want to display a javascript message upon the successful insertion of the data.However I am not able to execute the javascript code.

Below is my code.

 <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> 

The script end tag is misspelled. Update it as follows:

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

Check spelling of script

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

It would be

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

You have misspelled </scrit> it must be </script> .

you can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM