简体   繁体   English

使用Ajax调用的php脚本重定向到另一个页面

[英]Redirecting to another page with a php script that is called with ajax

I am creating a login script (just the beginning, I will lookup how to encrypt the password later on). 我正在创建一个登录脚本(只是开始,稍后我将查找如何加密密码)。 But I can't figure out how to redirect to a certain page. 但是我不知道如何重定向到某个页面。

I have two possibilities now, an admin or a normal user. 我现在有两种可能性,管理员或普通用户。

My script: 我的剧本:

$conn = new Connection;

$username = $_POST['username'];
$userpassword = $_POST['userpassword'];

if(empty($username) && empty($userpassword)){
  echo 'Vul een gebruikersnaam en wachtwoord in';
}else if(empty($username)){
  echo 'Vul een gebruikersnaam in';
}else if(empty($userpassword)){
  echo 'Vul een wachtwoord in';
}else{
  //Both filled in, begin logincode:
  $getuser = "SELECT * FROM users WHERE username = '".$conn->real_escape_string($username)."'";
  $getusercon = $conn->query($getuser);
  $getuser = $getusercon->fetch_assoc();

  if($userpassword == $getuser['password']){
    if($getuser['rights'] == '1'){
      $_SESSION['user'] = 'admin';
      // header("Location: http://www.mysite.nl/addcompany.php");
      // exit();
      header("Location: http://www.mysite.nl/addcompany.php");
      die();
      echo 'Je bent ingelogd als '.$_SESSION['user'].'';
    }else{
      $_SESSION['user'] = 'user';
      echo 'Je bent ingelogd als '.$_SESSION['user'].'';
    }
  }else{
    echo 'Wachtwoord en gebruikersnaam komen niet overeen';
  }
}

My ajax code: 我的ajax代码:

// Login Ajax Code
$( "#content" ).on("submit", "#loginform", function( event ) {
  // Stop normal form behaviour
  event.preventDefault();
  // Haal de inputvelden op met zijn waardes
  var $form = $( this ),
  $username = $form.find( "input[name='username']" ).val(),
  $userpassword = $form.find( "input[name='userpassword']" ).val(),
  url = $form.attr( "action" );
  // Post above values to the action of the form
  var posting = $.post( url, { username: $username, userpassword: $userpassword} );
  // Show result in a div
  posting.done(function( data ) {
    $( "#loginresult" ).empty().slideDown('fast').append( data );
  });
});

I tried to use this: 我试图用这个:

header("Location: http://www.mysite.nl/addcompany.php");
die();

But I just see my preloader infinitely spinning and in my console I see the following: 但是我只是看到预加载器无限旋转,并且在控制台中看到以下内容:

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

index.php:1 [DOM] Found 8 elements with non-unique id #example-text-input: (More info: https://www.chromium.org/developers/design-documents/create-amazing-password-forms)

But that id is on the page I want to redirect too, except it doesn't redirect and just stays on the same page with my preloader spinning and those messages. 但是该ID也在我要重定向的页面上,除了它不重定向之外,只是与预加载器旋转和这些消息保持在同一页面上。

What can I do? 我能做什么?

You can send on the PHP response a flag for redirect and on you JS you can add. 您可以在PHP响应上发送重定向标志,并在JS上添加。

For example: 例如:

window.location.replace("http://stackoverflow.com");
window.location.href = "http://stackoverflow.com";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM