Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 否则,如果没有';不能在ajax登录表单中工作_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 否则,如果没有';不能在ajax登录表单中工作

Javascript 否则,如果没有';不能在ajax登录表单中工作,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我通过查看google并结合proccess.php、submit form和ajax编写了下面的代码,但我不知道我的错误在哪里。代码似乎不起作用 process.php <?php session_start(); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); //check login status and etc if ($da

我通过查看google并结合proccess.php、submit form和ajax编写了下面的代码,但我不知道我的错误在哪里。代码似乎不起作用

process.php

<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

  //check login status and etc

      if ($data['status'] == 'ok' && $data['loggedinuser']['username'] == "$username") {  
          echo 'ok';
      } elseif ($data['message'] == 'checkpoint' && $data['status'] == 'fail') {
          echo 'checkpoint';
      } elseif ($data['errortype'] == 'invaliduser' && $data['status'] == 'fail') {
          echo 'wrongusername';
      } elseif ($data['errortype'] == 'badpassword' && $data['status'] == 'fail') {
          echo 'wrongpassword';
      } elseif ($data['errortype'] == 'unusablepassword' && $data['status'] == 'fail') {
          echo 'unusablepassword';
} else {
          echo '';
}
?>

index.php

<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

  //check login status and etc

      if ($data['status'] == 'ok' && $data['loggedinuser']['username'] == "$username") {  
          echo 'ok';
      } elseif ($data['message'] == 'checkpoint' && $data['status'] == 'fail') {
          echo 'checkpoint';
      } elseif ($data['errortype'] == 'invaliduser' && $data['status'] == 'fail') {
          echo 'wrongusername';
      } elseif ($data['errortype'] == 'badpassword' && $data['status'] == 'fail') {
          echo 'wrongpassword';
      } elseif ($data['errortype'] == 'unusablepassword' && $data['status'] == 'fail') {
          echo 'unusablepassword';
} else {
          echo '';
}
?>
$(文档).ready(函数(){
$(“#loginform”).submit(函数(){
if($('#username').val().length>3('#password').val.length>3){
var pdata=$(this).serialize();
$.ajax({
url:“proccess.php”,
数据:pdata,
类型:“POST”,
beforeSend:function(){
$(“#pesan”).html(“加载…”);
},
成功:功能(pesan){
控制台日志(pesan);
if(console.log(pesan)=“ok”)
$(“#pesan”).html(“登录成功…PHP中需要的修复程序:

请接收Javascript发送的数据:

$data = $_REQUEST['data'];
window.location = "redirect.php";

Javascript中需要的修复:
if
语句更改如下:

if (pesan == "ok"){
    $("#pesan").html("<h4>Login Success...");
    window.location = "redirect.php";
}
else if (pesan == "gagal") 
    $("#pesan").html("<h4>Error!</h4>");
else if (pesan == "wrongusername") 
    $("#pesan").html("<h4>Wrong Username!</h4>");
else if (pesan == "wrongpassword") 
    $("#pesan").html("<h4>Wrong Password!</h4>");
else if (pesan == "checkpoint") 
    $("#pesan").html("<h4>need checkpoint</h4>");
else if (pesan == "accountblocked") 
    $("#pesan").html("<h4>your account blocked.</h4>"); 
else 
    $("#pesan").html("<h4>Invalid username and/or password.</h4>");

必须先检查$data数组中是否存在键,然后才能在if else中运行它。必须使用isset()检查数组元素是否存在

尝试按以下方式重新表述代码:

if($data['status']=="ok" && $data['loggedinuser']['username'] == "$username")
{
    echo "ok";
}
else
{
    if( isset($data['message']) ) {
      echo $data['message'];
    } 
    elseif( isset($data['errortype']) ) {
      echo $data['errortype'];
    }   
}

注意:您可以回显$data[“消息”]和$data[“错误”]与您所做的完全不同。

您有错误吗?什么不起作用?Echo/var\u在运行检查之前转储您的变量,然后您可以查看它是否应该与您的一个ELSE匹配。@aynber我在process.php中有Echo变量,并且输出与上面的Echo相同……因为看起来您想要比较PHP要确定HTML应该是什么,所有类似以下的语句:
if(console.log(pesan)=“ok”)
应该更改为
if(pesan==“ok”)
我现在更改它,但仍然没有什么不同:(AJAX代码只发送将在
$data
中接收的
用户名
密码
。现在,是否存在一些代码,上面的代码片段只包含一条注释:
//检查登录状态等
?如果是的话,那么不必将所有内容都塞进
$data
-使用其他代码。)用于保存
状态
消息
等变量,并在所有
elseif
条件下使用这些变量。如果现在没有代码,则此处提供的代码仍然不完整,需要做大量工作来验证和验证用户凭据。my proccess.php只需要用户名和密码,然后继续正在访问它…$data['status']是json_decode中的数组,因为这里没有显示该代码,所以这里的人无法判断它是如何影响或参与问题的。