如何回显警报?

我想在数据插入之前在 php 中使用 java 脚本警报。我能够将数据插入数据库,但 echo 无法完成它的工作。这可能只是一个简单的错误,但我无法发现它:(


/// Java script

/// send data for insertion

$.get("tute7_add.php",{unit_code:unit_code,unit_name:unit_name,lecturer:lecturer,semester:semester}).done(function(data){

                    alert('You have added a unit successfully!');

                });

tute7_add.php


<?php

            // I Can't send alert!!

            echo "<script type='text/javascript'>

                         alert('Don\'t have a record');

                    </script>";


            $code= $_GET['unit_code'];

            $name= $_GET['unit_name'];

            $lecturer= $_GET['lecturer'];

            $semester= $_GET['semester'];


            $query = "INSERT INTO units (`unit_code`,`unit_name`,`lecturer`,`semester`) VALUES ('$code','$name','$lecturer','$semester');";

            $mysqli->query($query);

    ?>


慕容708150
浏览 122回答 1
1回答

慕的地10843

您正在使用 Ajax 请求 URL。该data变量将包含一大块 HTML,其中包括脚本元素。您没有使用 的值做任何事情data。你只是忽略它。通常在处理 Ajax 时,您将以 JSON 之类的格式返回原始数据,而不是使用 HTML。然后您可以使用客户端代码处理它。例如header("Content-Type: application/json");if (1) { # Use a real condition here&nbsp; &nbsp; echo json_encode([ "error" => "Don't have a record" ]);&nbsp; &nbsp; exit;} else {&nbsp; &nbsp; # do database stuff and then…&nbsp; &nbsp; echo json_encode([ "success" => "Something something" ]);&nbsp; &nbsp; exit;}随着:$.get("tute7_add.php", {&nbsp; unit_code,&nbsp; unit_name,&nbsp; lecturer,&nbsp; semester,}).done(function (data) {&nbsp; if (done.success) {&nbsp; &nbsp; alert("You have added a unit successfully!");&nbsp; } else {&nbsp; &nbsp; alert(done.error);&nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP