关于php输出值的问题
我做了个简单的html登陆表单,点击提交按钮,异步提交验证。php文件连接数据库验证用户名密码,对则echo一个字符串‘true’,错了则无输出值。返给html后,做判断,再做响应动作。可我测试时发现,不管输错输对,页面均是输对的动作。php部分是正确的,可以根据表单提交数据输出响应的值,我觉得bug在客户端部分,怎么改都不对,望各位大神帮帮忙。以下是客户端脚本:
<script> <br /> $('#btn').click(function () { <br /> $('#form2').ajaxSubmit({ <br /> success:function(data) { <br /> console.log(data+' '+typeof data); <br /> console.log(data=='true'); <br /> if (data) { <br /> alert('登陆成功!'); <br /> }else { <br /> $('#tip5').html('用户名或密码错误,请重新输入!') <br /> } <br /> }, <br /> <br /> }) <br /> }) <br /> </script>
回复讨论(解决方案)
输错时,控制台输出data是什么?
输错时,无输出。这是我php代码部分:
$name_login=$_POST['name_login'];
$psw_login=$_POST['psw_login'];
$url = "127.0.0.1";
//账号
$user = "root";
//密码
$password = "";
//连接
$con = mysql_connect($url,$user,$password);
if (!$con){
die('Could not connect: ' . mysql_error());
}
//设置编码机
mysql_query("set names utf8");
//连接数据库
mysql_select_db("yang");
$sql = "select * from plane where username='$name_login'";
$result=mysql_query($sql,$con);
$re=mysql_fetch_assoc($result);
if($re) {
if($re['psw']==$psw_login) {
echo "true";
}
}
?>
输错时,控制台输出data是什么?
输错时,无输出。这是我php代码部分:
$name_login=$_POST['name_login'];
$psw_login=$_POST['psw_login'];
$url = "127.0.0.1";
//账号
$user = "root";
//密码
$password = "";
//连接
$con = mysql_connect($url,$user,$password);
if (!$con){
die('Could not connect: ' . mysql_error());
}
//设置编码机
mysql_query("set names utf8");
//连接数据库
mysql_select_db("yang");
$sql = "select * from plane where username='$name_login'";
$result=mysql_query($sql,$con);
$re=mysql_fetch_assoc($result);
if($re) {
if($re['psw']==$psw_login) {
echo "true";
}
}
?>
我是说输错时
console.log(data+' '+typeof data);
console.log(data=='true');
这两句输出什么?
我是说输错时
console.log(data+' '+typeof data);
console.log(data=='true');
这两句输出什么?
test.html:38 string
test.html:39 false
看看你的表单
看看你的表单
谢大神帮我分析,以下是html内容
nbsp;html>
<script></script>
<script></script>
<script> <br /> $('#btn').click(function () { <br /> $.ajax({ <br /> url:'plane/login.php', <br /> type:'post', <br /> data:{ <br /> 'name_login':'fengjie', <br /> 'psw_login':'123123', <br /> <br /> }, <br /> success:function(data) { <br /> console.log(data+' '+typeof data); <br /> console.log(data=='true'); <br /> if (data) { <br /> alert('登陆成功!'); <br /> }else { <br /> $('#tip5').html('用户名或密码错误,请重新输入!') <br /> } <br /> }, <br /> <br /> }) <br /> }) <br /> </script>
看看你的表单
这个js脚本是我改成了直接使用jq的ajax()方法异步提交,想看看是不是ajaxsubmit插件出了问题。结果还是一样,客户端根本不识别php返回来的字符串。这样测试
$(function() { //最好这样包起来,以防 jq 加载太慢 $('#btn').click(function () { $.ajax({ url:'plane/login.php', type:'post', data:{ 'name_login':'fengjie', 'psw_login':'123123', }, success:function(data) { alert(data); //直接查看返回的数据,如果 php 有问题,一样能看到 } }) })})
这样测试
$(function() { //最好这样包起来,以防 jq 加载太慢 $('#btn').click(function () { $.ajax({ url:'plane/login.php', type:'post', data:{ 'name_login':'fengjie', 'psw_login':'123123', }, success:function(data) { alert(data); //直接查看返回的数据,如果 php 有问题,一样能看到 } }) })})
测试了,如果用户名密码写对了,弹窗提示‘true’,若果错的,弹窗为空。我现在的问题是,如果php返回’true‘,js脚本这边的判断句data==‘true’的结果是false,怎么可能是false???php返回的就是字符串类型的‘true’啊!
alert(data.length); //看一下返回数据的长度
alert(data.length); //看一下返回数据的长度
输对了,长度是5,输错了长度是1。奇怪了,返回空的怎么还有长度?
因为长度不同,所以 dada == 'true' 不会成立
你可以进一步看一下多出来的字符的内码是多少
alert(data.charCodeAt(0));
如果小于等于 32,就表示你的程序多输出了 空格、制表符、回车、换行
如果大于 255,就表示你的程序文件有 BOM 头
看来,经过这一折腾,你已经具有处理 ajax 隐性问题的能力了
因为长度不同,所以 dada == 'true' 不会成立
你可以进一步看一下多出来的字符的内码是多少
alert(data.charCodeAt(0));
如果小于等于 32,就表示你的程序多输出了 空格、制表符、回车、换行
如果大于 255,就表示你的程序文件有 BOM 头
看来,经过这一折腾,你已经具有处理 ajax 隐性问题的能力了
我测试了,输对了内码显示是116,输错了内码显示9。怎么还不一样呢?
因为长度不同,所以 dada == 'true' 不会成立
你可以进一步看一下多出来的字符的内码是多少
alert(data.charCodeAt(0));
如果小于等于 32,就表示你的程序多输出了 空格、制表符、回车、换行
如果大于 255,就表示你的程序文件有 BOM 头
看来,经过这一折腾,你已经具有处理 ajax 隐性问题的能力了
我测试了,输对了内码显示是116,输错了内码显示9。怎么还不一样呢?
如果输对了,charcodeat(4)就是9,这个跟输错时,charcodeat(0)相同,看来确实我以为php返回为空,其实是返回了一个编码为9的字符。
因为长度不同,所以 dada == 'true' 不会成立
你可以进一步看一下多出来的字符的内码是多少
alert(data.charCodeAt(0));
如果小于等于 32,就表示你的程序多输出了 空格、制表符、回车、换行
如果大于 255,就表示你的程序文件有 BOM 头
看来,经过这一折腾,你已经具有处理 ajax 隐性问题的能力了
我测试了,输对了内码显示是116,输错了内码显示9。怎么还不一样呢?
如果输对了,charcodeat(4)就是9,这个跟输错时,charcodeat(0)相同,看来确实我以为php返回为空,其实是返回了一个编码为9的字符。
我php那边无论输出‘true’还是‘false’甚至无输出时,js脚本这边接收的data字符串的最后都自动加了编码为9的字符,好奇怪!
你把程序文件的 bom 头去掉就行了
你把程序文件的 bom 头去掉就行了
谢大神,我用了trim,整个程序就正常了。但这样治标不治本,我还没弄清这个bom头是怎么加进去的,网上有说是编辑器的原因,我用的editplus,看来得换个编辑器试试。
保存文件时选择 utf-8 无 bom
保存文件时选择 utf-8 无 bom
我看了editplus的首选项,默认保存就是utf-8无bom
那还要看 标记外是否有空格、空行之类的
那还要看 标记外是否有空格、空行之类的
我查了php文件,果然是标记外有个制表符,哎,刚学php,埋了太多雷啊,膜拜大神!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
