Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: JQuery Ajax

  1. #1
    New Coder
    Join Date
    Jun 2011
    Location
    Bristol, United Kingdom
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JQuery Ajax

    Code:
    // JavaScript Document
    function JNChatroom_Submit() {
    	var field_a = $("#chatareabox").val();
    	$.ajax({
    		type: "POST",
    		url: "php-script.php?fun=post",
    		data: "message="+field_a,
    		success: function(done) {
    			$("#chatareabox").val('');
    		}
    		error: function(e) {
    			alert("Error: "+e);
    		}
    	}) 
    }
    For some reason i cant get this code working, it used to work but now it wont i cant find the problem and i have been looking for 2 hours. Please help

    This is for a chatroom, after typeing there post they click a button 'onClick="JNChatroom_Submit()" ' but nothing happens, not alert or clearing of the textarea, there is also no submition to my database.

    below is my PHP
    PHP Code:
    if($MySQL){

        if(
    $_GET['fun'] == 'post'){
            
    $POST mysql_query("INSERT INTO chatroom (user_id, post) VALUES ('".$User['id']."', '".$_POST['message']."')");
        }



  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You should start debugging

    There's at least a simple syntax error. A missing comma after the success callback

    Then you are mixing a POST request with a GET request ... I don't know if jQuery is tolerant about that, but it's not good habit. Hint: Appending a parameter with ? to the URL usually will make the request a GET request

  • #3
    New Coder
    Join Date
    Jun 2011
    Location
    Bristol, United Kingdom
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Mixing them together seems to work with chatrooms i have created in the past, all it does is looks thought the PHP page to find the correct code

    PHP Code:
    <?PHP
    if($_GET['fun'] == 'post'){
     
    // Submit
    }
    if(
    $_GET['fun'] == 'retreave'){
     
    // Retrive Data
    }
    ?>
    This seems to work and does the job very well rather then making many different pages it read the code you ask it to.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    How do you access the second parameter "message" in PHP then? With $_GET['message'] or $_POST['message']?

  • #5
    New Coder
    Join Date
    Jun 2011
    Location
    Bristol, United Kingdom
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    $_POST['message']

    GET And POST are used in different ways

    $_GET['id'] is in the URL index.php?id=1
    $_POST['message'] has been posted from a form within the last page.

    PHP Code:
     if($_GET['fun'] == 'submit'){
      
    mysql_query("INSERT INTO chatroom (user_id, post) VALUES ('".$_SESSION['abc123']."', '".$_POST['message']."'");
    }
    if(
    $_GET['fun'] == 'retrive'){
     echo 
    $_POST['message'];

    my chatroom is finished and i would quite happyly show you it, please email me jnicholls92@hotmail.co.uk


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •