为了账号安全,请及时绑定邮箱和手机立即绑定

从阿贾克斯到爪哇脚本的返回值

从阿贾克斯到爪哇脚本的返回值

PHP
慕妹3242003 2022-09-25 20:39:13
我正在尝试检查数据库中是否已存在名称。但是我的Ajax没有返回值,老实说,甚至不确定Ajax是否正在运行。这是我在php页面上的代码。<input class="form-control" id="name" name="name" onblur="checkNameStatus()" /><script>        function checkNameStatus(){    //alert('Start of funtion');    var name=$("#name").val();    //alert("Name Input value is: "+name);    $.ajax({        type:'post',            url:'<?php echo base_url() ?>assets/ajax/checkName.php?name='+name,            success:function(msg){            alert(msg);                 }     });     //alert("End of function");    }</script>使用警报,我可以判断该函数确实正在运行并接收名称输入的值。然后是我的阿贾克斯<?php$hostname   = 'localhost';$username   = 'root';$password   = '123';$dbname     = 'test_db';function clean($string) {   return preg_replace('/[^A-Za-z0-9\- .!@#$%&]/', '', $string);}$name= clean($_GET['name']);$msg = "";$conn=mysqli_connect($hostname,$username,$password,$dbname);if (mysqli_connect_errno()){    echo ("Failed to connect to MySQL: " . mysqli_connect_error());}$sql = "SELECT * FROM pattern where name = '$name';";$check=mysqli_query($conn, $sql);$count=mysqli_num_rows($check);if($count!=0)   {     $msg = "A Schedule Pattern with the Name of $name already exists. If you continue to use this Name it will overwrite $name's current Data.";     return $msg;   } else {    $msg = "Count check Failed";     echo $msg;   }mysqli_close($con); ?>如何从我的ajax接收$msg变量并将其返回到脚本,然后随后在警报中显示它?
查看完整描述

1 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

我认为你必须改变这一点


$check=mysql_query($sql);

$count=mysql_num_rows($check);

对此


$check=mysqli_query($conn, $sql);

$count=mysqli_num_rows($check);

您正在同时使用mysql_和mysqli_这是错误的,这可能是它未能检查计数的原因,我还建议您在找不到名称的情况下发送错误消息。


编辑,完整代码应为:(菲律宾比索):


<?php


$hostname   = 'localhost';

$username   = 'root';

$password   = '123';

$dbname     = 'test_db';



function clean($string) {

   return preg_replace('/[^A-Za-z0-9\- .!@#$%&]/', '', $string);

}


if(isset($_GET['name'])){

$name= clean($_GET['name']);

$msg = "";


$conn=mysqli_connect($hostname,$username,$password,$dbname);


if (mysqli_connect_errno()){

    echo ("Failed to connect to MySQL: " . mysqli_connect_error());

}


$sql = "SELECT * FROM pattern where `name` = '$name';";


$check=mysqli_query($conn, $sql);

$count=mysqli_num_rows($check);


if($count!=0)

   {

     $msg = "A Schedule Pattern with the Name of $name already exists. If you continue to use this Name it will overwrite $name's current Data.";

     echo $msg;

   } else {

    $msg = "Count check Failed"; 

    echo $msg;

   }    

mysqli_close($conn);   

}  

?>


查看完整回答
反对 回复 2022-09-25
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号