dongmufen8105 2017-02-23 18:16
浏览 34

使用单独的jquery文件重定向到另一个页面

I'm aware this question has been asked millions of times but hopefully this is slightly different as i can't seem to find an answer. I'm trying to redirect back to the index page (where login takes place) after a successful sign up process. Ideally

header("Location: index.php");

would be used. However, i can't use that as part of my PHP validation uses echo's. The code i have below works perfectly. Using

<script>
  window.location.href = "index.php";
</script>

This is the end part of my validation code:

//loads of other form validation using echo's
...

 {
  $resultusername = queryMysql("SELECT * FROM members WHERE username='$username'");

  if ($resultusername->num_rows)
  {
echo <<<_END
<div id=usernameinuse>
   <p>This username is already in use!</p>
</div>
_END;

 }
else
 {
            $qresult = queryMysql("INSERT INTO members (firstname, 
surname, gender, dob, email, username, password) 
VALUES('$firstname', '$surname', '$gender', '$dob', '$email', '$username', '$password')");



echo <<<_END
<script>
 window.location.href = "index.php";
</script>
_END;

}

Like i said this works perfectly, however, I'd ideally like all jquery and javascript to be in separate files from my php and linked at the top of the file (other jquery files are already up there) and want to avoid going into javascript in the middle of a php file. So my question is, is there a way to make that script external and catch it in a jquery file like

$('input[type=submit]').click && ($qresult)(function() {
window.location.href = 'index.php';
return false;
}

This works without the && ($qresult) bit, but then if you just click submit, non of the validation takes place and nothing is entered into the database.

I'm also aware PDO is a lot better to use and mysql functions are slightly obsolete but i'm just getting used to everything. Thanks

  • 写回答

1条回答 默认 最新

  • dongqijuan3786 2017-02-23 18:46
    关注

    Lets say you have a controller file that has this function:

     function insert_user($data) {
       $qresult = false;
       $resultusername = queryMysql("SELECT firstname FROM members WHERE username='$username'");
       if ($resultusername->num_rows < 1)
       {
          $qresult = queryMysql("
            INSERT INTO members (firstname, surname, gender, dob, email, username, password) 
            VALUES('{$data['firstname']}', '$data['surname']', '$data['gender']', '$data['dob']', '$data['email']', '$data['username']', '$data['password']')
          ");
       }
       return $qresult;
     }
    

    If your form action sends a POST request to this same file, where you'll be calling insert_user() something like this:

     $inserted = insert_user($_REQUEST);
     if($inserted) {
        include("successful_registration.php");
     }
     else {
        include("user_already_exists.php");
     }
    

    where these two included files are two different views. You may want to tweak this logic to your liking but the idea here is to separate your business logic from view logic. If not using include as I suggested, you may be able to redirect to different pages using header() before you even get to rendering and views.

    If you are using ajax to insert a user then again a POST to controller will give you back the information you need in client side to decide what you want to do (eg. manipulate content, re-render a part of the page, etc).

    This may be just an opinion, but separation of query code from view code is the first step to getting this problem solved correctly.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题