Need help in adding jQuery to PHP!

Discussion in 'jQuery' started by Zero55, Dec 30, 2010.

Thread Status:
Not open for further replies.
  1. #1
    Hello,
    I have a text based game, i have done the register and login page using PHP and jQuery but i can't use that knowledge with the other PHP files, such as crimes , topics ... and so on.
    What i want is to add jQuery into my PHP files in order to remove the refresh on submit, for example player want to do crime or send message, it will submit using jQuery and display a message without refresh.

    Any help is appreciated , i have a file already testing it on it , its working but not all of it!

    Please if you have a good knowledge with this or you believe that you can help me out, PM me!

    Am ready to do anything in return.
    Thanks in advance.
     
    Zero55, Dec 30, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    tvoodoo, Dec 30, 2010 IP
  3. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have already started that without any problem (Login and Registering) but am having some problems in long PHP files!
     
    Zero55, Dec 31, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    What kind of problems ? Simply separate the actions of the AJAX requests in a PHP file and the caller (the JS code) in another PHP.
     
    tvoodoo, Jan 1, 2011 IP
  5. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have the problem that if the page didn't finish the load and the user clicked on submit it will view the message in the Submit PHP file, else it will show the message in the same page.
    So what i want is , the user can't submit until the page is loaded, how can i do that?
     
    Zero55, Jan 2, 2011 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    use in every $.ajax request the option async : true
     
    tvoodoo, Jan 2, 2011 IP
  7. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok, i have just found out that FireFox browser is only loading some times , how can i solve it?
     
    Zero55, Jan 2, 2011 IP
  8. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $(document).ready(function(){
    	
    	$('#send_msg').submit(function(e) {
    
    		send_msg();
    		e.preventDefault();
    		
    	});
    	
    });
    
    
    function send_msg()
    {
    	hideshow('loading',1);
    	error(0);
    	
    	$.ajax({
    		type: "POST",
    		url: "http://www.mafiafamilies.net/submit/send_msg.php",
    		data: $('#send_msg').serialize(),
    		dataType: "json",
    		success: function(msg){
    			
    			if(parseInt(msg.status)==1)
    			{
    				succ(1,msg.txt);
    
    			}
    			else if(parseInt(msg.status)==0)
    			{
    				error(1,msg.txt);
    
    			}
    			
    
    			hideshow('loading',0);
    		}
    
    	});
    
    }
    
    
    function hideshow(el,act)
    {
    	if(act) $('#'+el).css('visibility','visible');
    	else $('#'+el).css('visibility','hidden');
    }
    
    function error(act,txt)
    {
    	hideshow('error',act);
    	if(txt) $('#error').html(txt);
    }
    
    
    function succ(act,txt)
    {
    	hideshow('succ',act);
    	if(txt) $('#succ').html(txt);
    }
    Code (markup):
    This is my JS
     
    Zero55, Jan 2, 2011 IP
  9. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Replace your jQuery document ready function with :
    
    $(document).ready(function(){
    	$('#send_msg').submit(function() {
    		send_msg();
    		return false;
    	});
    });
    
    HTML:
     
    tvoodoo, Jan 2, 2011 IP
  10. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Same problem, if user click before the page is loaded it will show the message from the Submit PHP file, and on Firefox some times it work and some times it just loading...

    All help is appreciated , thank you tvoodoo for your time :)
     
    Zero55, Jan 2, 2011 IP
  11. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Fixed on FireFox , but still having the other problem.
    What i need is to disable the Submit button until the page is being fully loaded.
    And how can i refresh image when user submit , for example , i have Slot Machine , i want to refresh to refresh the 3 images when user submit using AJAX.
    Is that possible?

    Again , thank you.
     
    Zero55, Jan 2, 2011 IP
  12. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #12
    Do something like this :
    
    <script>
    function send_msg()
    {
    	hideshow('loading',1);
    	error(0);
    	
    	$.ajax({
    		type: "POST",
    		url: "http://www.mafiafamilies.net/submit/send_msg.php",
    		data: $('#send_msg').serialize(),
    		dataType: "json",
    		success: function(msg){
    			
    			if(parseInt(msg.status)==1)
    			{
    				succ(1,msg.txt);
    
    			}
    			else if(parseInt(msg.status)==0)
    			{
    				error(1,msg.txt);
    
    			}
    			
    
    			hideshow('loading',0);
    		}
    
    	});
            return false;
    }
    
    
    function hideshow(el,act)
    {
    	if(act) $('#'+el).css('visibility','visible');
    	else $('#'+el).css('visibility','hidden');
    }
    
    function error(act,txt)
    {
    	hideshow('error',act);
    	if(txt) $('#error').html(txt);
    }
    
    
    function succ(act,txt)
    {
    	hideshow('succ',act);
    	if(txt) $('#succ').html(txt);
    }
    </script>
    <form action="#" method="POST" onsubmit="send_msg()">
    </form>
    
    HTML:
     
    tvoodoo, Jan 2, 2011 IP
  13. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #13
    Yes it is ... you will have to try some basic ajax request so that you can understand how it works.
    The slot machine can be achieved using 2 solutions :
    1.Generating a random number that will return one of your images for each slot (repeating for the number of slots you have)
    OR
    2.Call one ajax request and generate all slots directly in one php script call
     
    tvoodoo, Jan 2, 2011 IP
  14. Zero55

    Zero55 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I have the PHP script already for changing those images, but i want to refresh them with the output on succ for example.
     
    Zero55, Jan 2, 2011 IP
Thread Status:
Not open for further replies.