【发布时间】:2017-09-24 08:59:00
【问题描述】:
我正在尝试实现一个简单的 javascript 程序,在该程序中我正在对 php 脚本进行 ajax 调用。我确保页面不会自行刷新。所以,现在如果我在 php 中使用 echo 函数,那么它就不起作用了。
$("#check").on('click',function(){
//alert("hello");
var user_name=document.getElementById("user_name").value;
var pwd=document.getElementById("pwd").value;
$.ajax({
url:'checkUser.php',
type:'POST',
data:{
user_name:user_name,
pwd:pwd
},
success:function(){
alert("hello"); //This section works fine so the php file is getting called
}
});
return false;
})
checkUser.php
<?php require 'database.php';
if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
//Database is getting connected properly
$user_name=$_POST['user_name'];
$password=$_POST['pwd'];
echo("<script>console.log('PHP:');</script>"); //not getting displayed on console
$sql="Select * from login where User_name='$user_name' and Password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
echo $count; //not getting displayed on console
?>
如果我想使用回显功能,那我该怎么做呢?谢谢!
【问题讨论】:
-
您的代码容易受到SQL injection attacks 的攻击。您应该使用mysqli 或PDO 准备好的语句和this post 中描述的绑定参数。
-
停止使用
mysql_*函数。它们自 v5.5(2013 年 6 月)起已被弃用,自 v7.0(2015 年 12 月)起已被删除。而是将mysqli_* 或PDO 函数与prepared statements 和bound parameters 一起使用。 -
从不存储纯文本密码。您应该改用
password_hash()和password_verify()。如果您使用的是 5.5 之前的 PHP 版本,请不要使用 MD5 或 SHA1 来散列密码。相反,您可以使用this compatibility pack。 -
Woot,必须在一篇文章中使用我所有的简介!
-
<script>console.log('PHP:');</script>作为字符串发送回浏览器。$count;也返回到同一个字符串中,你没有对返回值做任何事情。
标签: javascript php jquery mysql ajax