开发者

redirect when condition met

开发者 https://www.devze.com 2023-02-23 01:08 出处:网络
I\'m having trouble redirecting after a count hits 13. I have bunch of images that a user can click on to add to their list in the db, when they click, an on click js script fires and triggers update

I'm having trouble redirecting after a count hits 13.

I have bunch of images that a user can click on to add to their list in the db, when they click, an on click js script fires and triggers updatelist.php.

That part works fine, within updatelist.php I need it to insert and then check the table to see if its reached a count of 13 yet, if so, it needs to redirect to a new page.

The insert works fine, but its not redirecting. below is my updatelist.php code

<?php
include('connect.php');
session_start();
$username=$_SESSION['username'];

$image = $_GET['image'];


    $query = mysql_query("SELECT * FROM tbl_movies cross join tbl_users WHERE image_on = '$image' and username = '$username'");
    while ($row = mysql_fetch_assoc($query))
                        {
                            $movieid = $row['id'];
                            $userid = $row['UserID'];
                        }
    $query2 = mysql_query("select * from tbl_movies cross join tbl_users where image_off = '$image' and username = '$username'");   
    while ($row2 = mysql_fetch_assoc($query2))
                                {
                                    $mid = $row2['id'];
                                    $uid = $row2['UserID'];
                                }   

    $numrows = mysql_num_rows($query);
    $numrowz = mysql_num_rows($query2);

        if ($numrows!=0)
            {
                $queryInsert = mysql_query("insert into tbl_user_lists (userid_fk,movie_id_fk) values ('$userid','$movieid')"); 

                $query5=mysql_query("select l.* from tbl_user_lists as l join tbl_users on l.userid_fk=tbl_users.UserID where username='$username'");
                $updatecount = mysql_num_rows($query5);
                if ($updatecount == 13)
                  开发者_运维知识库  {
                        header('Location: http://localhost/main.php');
                    }
            }
        else if ($numrowz!=0)   
            {
                $queryUpdate = mysql_query("delete from tbl_user_lists where userid_fk = '$uid' and movie_id_fk = '$mid'");
            }




?> 

Below is the script on the main page that calls the above php page

<script>
$(function(){
    $('.img-swap').live('click', function() {
        var _src = $(this).attr('src');
        if (-1 == _src.indexOf('_on')) {
            $(this).attr('src',_src.replace('_off','_on')).removeClass('off');
        } else {
            $(this).attr('src',_src.replace('_on','_off')).addClass('on');
        }

        //  Update server
        $.get( 'updatelist.php?image='+escape($(this).attr('src')));
        $(this).toggleClass("on");
    });
});
</script>    


You want:

    if ($updatecount == 13)
        {
            header('Location: main.php');
            exit();
        }

Basically you were doing an assignment operator rather than a comparison, and you should always call exit() after you redirect.


So you're calling a remote PHP script via JS? I don't think sending a header response to the JS code will do anything in the page that calls it. You could have your PHP script return a predetermined code depending on its result, then redirect in the JS code if the "max" code was returned.

Something like (assuming jQuery is available)

<script>
$.get( "your_php_script.php" , function (response){
     if( response == "max" ) {
          // redirect
          window.location="other_page.php";
     }}
);
</script>


Some problems that might have caused the issue:

  1. after Location: you should write a full url;
  2. if ($updatecount = 13) will always return true, use a comparison operator instead (if ($updatecount == 13));
  3. no code should be executed after the redirect: add exit; after the header line;
  4. make sure there's no output at all, or php won't be able to set the header: is your <?php opening tag the first thing in the file? Is there no html before?;
  5. as a test for counts, when in doubt, always output the result after every loop: it will help avoiding several errors as you start with php, most notably off-by-ones.

Also, I don't know if your application will be publicly accessible: in that case, remember that users are not bound to the options in your html as for the values they can post, so escape your query (or use prepared statements) to avoid the risk of sql injection.

0

精彩评论

暂无评论...
验证码 换一张
取 消