Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-05-2011, 04:02 PM   PM User | #1
jnicholls92
New Coder

 
Join Date: Jun 2011
Location: Bristol, United Kingdom
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
jnicholls92 is an unknown quantity at this point
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']."')");
    }


jnicholls92 is offline   Reply With Quote
Old 07-05-2011, 11:20 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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
devnull69 is offline   Reply With Quote
Old 07-06-2011, 12:00 AM   PM User | #3
jnicholls92
New Coder

 
Join Date: Jun 2011
Location: Bristol, United Kingdom
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
jnicholls92 is an unknown quantity at this point
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.
jnicholls92 is offline   Reply With Quote
Old 07-06-2011, 08:13 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
How do you access the second parameter "message" in PHP then? With $_GET['message'] or $_POST['message']?
devnull69 is offline   Reply With Quote
Old 07-06-2011, 10:26 AM   PM User | #5
jnicholls92
New Coder

 
Join Date: Jun 2011
Location: Bristol, United Kingdom
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
jnicholls92 is an unknown quantity at this point
$_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
jnicholls92 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:56 AM.


Advertisement
Log in to turn off these ads.