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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Location
    Nottingham, England, United Kingdom
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    make it a click button, not a submit button.

    besides that, your script is open to SQL Injection.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Location
    Nottingham, England, United Kingdom
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,132
    Thanks
    12
    Thanked 332 Times in 328 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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