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 01-11-2012, 03:01 PM   PM User | #1
dwatson251
New to the CF scene

 
Join Date: Jan 2012
Location: Nottingham, England, United Kingdom
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
dwatson251 is an unknown quantity at this point
Update Database with Jquery + AJAX

I've only found a couple of tutorials that show this but as I am new to Jquery, every time I try to follow I get something wrong down the line and I have no idea what I am doing or what anything does.

Here is my HTML + JS code:
Code:
<!DOCTYPE html>
<html>
	<head>
		<title>AJAX</title>
		<link rel='stylesheet' type='text/css' href='style.css' />
		<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){  
				$("form#submit").submit(function() { 
				var textdata     = $('#textdata').attr('value');  
					$.ajax({  
						type: "POST",  
						url: "db.php",  
						data: "textdata="+ textdata,  
						success: function(){  
							$('div.success').fadeIn();
						});  
			  
						}  
					});  
				return false;  
				});  
			});  
		</script>
	</head>
	<body>
		<div id='content'>
			<form id='submit' method='post'>
				<fieldset>
					<legend>Enter some information</legend>
					<input type='text' name='textdata' />
					<input type='submit' value='submit' />
				</fieldset>
			</form>
			<div id='success' style='display:none;'>Data Updated</div>
		</div>		
	</body>
</html>
And the PHP in db.php
PHP Code:
<?php
    
// MYSQL Connection:
    
mysql_connect("localhost""root");
    
mysql_select_db("test");
    
//------------------
    
    
mysql_query("UPDATE test SET text = '".$_POST['textdata']."' WHERE id = 1") OR die(mysql_error());

?>
I'm not sure what the problem is, but the page seems to refresh everytime I click submit?
dwatson251 is offline   Reply With Quote
Old 01-12-2012, 06:15 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
make it a click button, not a submit button.

besides that, your script is open to SQL Injection.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-12-2012, 12:08 PM   PM User | #3
dwatson251
New to the CF scene

 
Join Date: Jan 2012
Location: Nottingham, England, United Kingdom
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
dwatson251 is an unknown quantity at this point
I managed to get it working by deleting parts of the "success bit. JQuery is so confusing...

And as for the security, I'm not that bothered right now as long as it works, but I would be happy for you to tell me can do to make it more secure.
dwatson251 is offline   Reply With Quote
Old 01-12-2012, 12:16 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
it may be that your php script never returned data. there could be the possibility that this is not interpreted as success. you could send a 204 status code (header("HTTP/1.1 204 No Content", true, 204);), indicating that there will be no content.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich 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 12:19 AM.


Advertisement
Log in to turn off these ads.