Jump to content

Recommended Posts

Here's the basic premise of the function I am trying to create.  

 

1. User 1 sends a trade request to User 2.

2. User 2 accepts the request.

3. It redirects both users to new page(trade.php).

 

 

Below is the code where User 2 accepts the request.I am using jquery dialog box for pop windows. The current issue I am having is that "window.location." is not showing the "by=$session_requestById" variable. It appears as NULL if I var_dump on trade.php page. But it is showing the second variable correctly, which is "by=$session_requestToId".

 

It seems like it only happens on trade.php page.  Both variables show up fine on request-php.page and in heading when I var_dump.  Perhaps you can tell me what's wrong with this ?

							window.location.href='trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>';
<script>
	$(function() {
		$( "#dialog-confirm" ).dialog({
			resizable: true,
			autoOpen: true,
			modal: true,
			width: 600,
			height: 300,
			buttons: {
				"Accept Trade": function() {
					$( this ).dialog( "close" );
					
					$.ajax({
						type:"post",
						url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>",
						data:"action=acceptTrade",
						success:function(data){
							window.location.href='trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>';
							
						}
					});
					 
				},
				Cancel: function() {
					$( this ).dialog( "close" );
					
					$.ajax({
						type:"post",
						url:"request-trade?by=<?php echo  $session_requestById; ?>&to=<?php echo $session_requestToId; ?>",
						data:"action=cancelTrade",
						success:function(data){
							if(data == false) {
							
								alert('not cancelled');
							
							} else {
							
								alert('cancelled success');
							
							}
					
						}
					});
				}
			}
		});
	});

</script>

Where are those two variables set? When did you do that in request.php? Does trade.php have it too?

 

Here I have it layed out on a page to give you a better look.  I should also mention that it won't delete(using action=cancelTrade) my database row when I cancel my trade session as User 2.  But it does delete the row when I cancel it as User 1.

<?php require_once 'core/init.php';

$user         = new User();
$myUserid     = $user->data()->user_id; // this is my user_id as a logged in user

$session_requestById    =    $_SESSION['requestById'];
$session_requestToId    =    $_SESSION['requestToId'];

?>

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <script src="js/jquery-1.11.0.min.js"></script>    
        <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">    
        <script> // this script when User 1 makes a trade request.
            $(function() {
                $("#new-dialog").dialog({
                    resizable: true,
                    autoOpen: false,
                    modal: true,
                    width: 600,
                    height: 300,
                    buttons: {
                        "Cancel Trade": function(e) {
                            $(this).dialog("close");
                            
                            $.ajax({
                                type:"post",
                                url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>",
                                data:"action=cancelTrade",
                                success:function(data){
                                    
                                }
                            });
                        }
                    }
                });
                $(".dialogify").on("click", function(e) {
                    e.preventDefault();
                    $("#new-dialog").html("");
                    $("#new-dialog").dialog("option", "title", "Loading...").dialog("open");
                    $("#new-dialog").load(this.href, function() {
                        $(this).dialog("option", "title", $(this).find("h1").text());
                        $(this).find("h1").remove();
                    });
                });
            });
        </script>
        <script> // this script when User 2 accepts the trade request.
            $(function() {
                $( "#dialog-confirm" ).dialog({
                    resizable: true,
                    autoOpen: true,
                    modal: true,
                    width: 600,
                    height: 300,
                    buttons: {
                        "Accept Trade": function() {
                            $( this ).dialog( "close" );
                            
                            $.ajax({
                                type:"post",
                                url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>",
                                data:"action=acceptTrade",
                                success:function(data){
                                    window.location.href='trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>';
                                    
                                }
                            });
                            
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                            
                            $.ajax({
                                type:"post",
                                url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>",
                                data:"action=cancelTrade",      
                                success:function(data){
                                    if(data == false) {
                                    
                                        alert('not cancelled');
                                    
                                    } else {
                                    
                                        alert('cancelled success');
                                    
                                    }
                            
                                }
                            });
                        }
                    }
                });
            });
        
        </script>
    </head>
    
    // assume the content in the body below is trade.php
    <body>
        <?php
        
        //query here
        if(count($result) > 0) {
            foreach($result as $row) {
                $user_id            =     intval($row['user_id']);
            }
                $_SESSION['requestById']    = $myUserid;
                $_SESSION['requestToId']    = $user_id;
        }    
        ?>
    </body>
</html>

And here is my request-trade.php

<?php require_once 'core/init.php';

$user 			= new User();
$mainUserid 	= intval($user->data()->user_id);


$session_requestById	= 	intval(Input::get('by'));
$session_requestToId	= 	intval(Input::get('to'));


$action = $_POST['action'];

if($action == "cancelTrade") {

	try {
		
		$delete = $db->prepare("DELETE FROM trade_session WHERE request_by = :request_by AND request_to = :request_to AND session >= :session");
		$delete->bindParam(':request_by', $session_requestById);
		$delete->bindParam(':request_to', $session_requestToId);
		$delete->bindValue(':session', 1);
		$delete->execute();
		
	} catch(Exception $e) {
		die($e->getMessage());
	}

} else if($action == "acceptTrade") {

	try {
		
		$stmt = $db->prepare("UPDATE trade_session SET session = :session WHERE request_to = :request_to");
		$stmt->bindValue(':session', 2);
		$stmt->bindParam(':request_to', $session_requestToId);
		$stmt->execute();
		
				
	} catch(Exception $e) {
		die($e->getMessage());
	}


} else if($action == "requestTrade") {
								
	try {
	
		$find = $db->prepare("SELECT request_by, request_to, session FROM trade_session WHERE request_by = :request_by AND request_to = :request_to AND session >= :session");
		$find->bindParam(':request_by', $session_requestById);
		$find->bindParam(':request_to', $session_requestToId);
		$find->bindValue(':session', 1);
		$find->execute();
		$resultFind = $find->fetchAll(PDO::FETCH_ASSOC);
		if(count($resultFind) > 0) {
		
			echo 'Your request is already sent.';
			
		} else {
			
			
			echo 'You have sent trade request to ' . $session_requestToId;
			
			$session_date	= date('Y-m-d H:i:s');
			
			$stmt = $db->prepare("INSERT INTO trade_session(request_by, request_to, session, session_date) 
				VALUES(:request_by, :request_to, :session, :session_date)");
			
			$stmt->bindParam(':request_by', $session_requestById);
			$stmt->bindParam(':request_to', $session_requestToId);
			$stmt->bindValue(':session', 1);
			$stmt->bindParam(':session_date', $session_date);
			$stmt->execute();
			
		
		}	
		
	} catch(Exception $e) {
		die($e->getMessage());
	}

} else {
	
	// hide
}
Edited by helloworld001

    // assume the content in the body below is trade.php
    <body>
        <?php
        
        //query here
        if(count($result) > 0) {
            foreach($result as $row) {
                $user_id            =     intval($row['user_id']);
            }
                $_SESSION['requestById']    = $myUserid;
                $_SESSION['requestToId']    = $user_id;
        }    
        ?>
    </body>
Have you confirmed that this code is (1) executing and (2) setting the requestById and requestToId values? Are you doing any redirects after this executes (eg, to move them to the confirmation page)?

 

And side note: should you be verifying that new requests are made "by" the current user? That is, $session_requestById == $mainUserid. If so, do you even need to bother with a "by" value at all?

Edited by requinix
    // assume the content in the body below is trade.php
    <body>
        <?php
        
        //query here
        if(count($result) > 0) {
            foreach($result as $row) {
                $user_id            =     intval($row['user_id']);
            }
                $_SESSION['requestById']    = $myUserid;
                $_SESSION['requestToId']    = $user_id;
        }    
        ?>
    </body>
Have you confirmed that this code is (1) executing and (2) setting the requestById and requestToId values? Are you doing any redirects after this executes (eg, to move them to the confirmation page)?

 

And side note: should you be verifying that new requests are made "by" the current user? That is, $session_requestById == $mainUserid. If so, do you even need to bother with a "by" value at all?

 

 

 

Actually I just noticed something else.  Since I am doing user to user request, I have 2 browsers open. Firefox and Chrome. I am logged into 2 different accounts.  Well I just noticed that on firefox account(User 1), it displays both session variables correctly.  But on Chrome(User 2), it only displays 1 variable correctly(which is $session_requestToId).  

So besides answers to my previous questions,

 

What if you switch browsers? User 1 on Chrome and User 2 on Firefox?

 

Just tried switching browsers.  Definitly weird. I will have to go back and recheck the queries and match them correctly to relative user.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.