Results 1 to 7 of 7

Thread: Jquery to post to database

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    376
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default Jquery to post to database

    I am currently using jquery and ofc php to post to a database. However, i dont know what im doing wrong.

    here is my html:
    HTML Code:
    <div id="output"><input type="text" id="username" /><span class="info"></span></div>
    
    <div id="check"></div>
    
    <div id="button">Register</div>
    here is my Jquery:
    Code:
    $(document).on("click", "#button", function(){
    		var info = $('#username').val();
    		$.ajax({
    			type: 'POST',
    			url: 'save.php',
    			data: {username:info},
    			//dataType: 'html',
    			success: function(){
    				alert(info);
    			}
    		});
    	});
    And here is my php:
    PHP Code:
    $server $_SERVER['HTTP_HOST'];
        
    $user "root";
        
    $pass "";
        
    $db "landr";
        
        
    $table "reg";

        
    $con mysqli_connect($server,$user,$pass$db) or die('Failed to connect!');
        
        
    $name $_POST['username'];

        if(
    mysql_query("INSERT INTO $table VALUES('$name')"))
          echo 
    "Successfully Inserted";
        else
          echo 
    "Insertion Failed"
    I am able to grab the information from the database i am just not able to upload the information to the database. Does anyone know my problem?
    Thanks
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Three things:

    1. Have you checked that the value of $_POST['username'] is what you expect?
    Likewise, since your query is failing, have you checked what error message is being returned from the DB?

    2. Your code is vulnerable to SQL Injection Attacks and/or unintentional errors. Never place user input directly in an SQL query!

    3. The mysql_* functions are deprecated (observe, the pink box of doom). If at all practical, you should use mysqli or PDO.

  3. #3
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    376
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default

    1) im trying to send the jquery information to php without reloading the page.
    2) You dont see all of my coding actually
    3) I forgot to change that to mysqli. thanks for the reminder
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    I hope my post above didn't come off as too "blunt." I mean to be constructive, here.

    Quote Originally Posted by Crazykld69 View Post
    1) im trying to send the jquery information to php without reloading the page.
    Right - but you're still sending a response, so -during development at least- include the value and/or error message with the failure response.

    Quote Originally Posted by Crazykld69 View Post
    2) You dont see all of my coding actually
    I see
    $name = $_POST['username'];
    and
    if(mysql_query("INSERT INTO $table VALUES('$name')"))
    right next to each other. If you simply omitted a line from your example for some reason, then I'm glad to hear you've got it covered. Either way, it bears mentioning.

  5. #5
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    376
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default

    The problem is that I can not check the value that is being sent. I'm trying to send a value from JQuery to PHP under the value $_POST['username'] getting the value from the id='username'

    JQuery grabs the value from text box username then it sends the value that it has taken and send to save.php as $_POST['username'].
    I dont know if that makes sense.

    It still connects to the mysqli_queue because it pops out the value. I guess one thing i can do to check if it works or the error message is echo as json and make the json print out onto the page.

    Also, i used JQuery to check the validation of the textbox. You will not be able to hit the button unless everything is correct.
    Last edited by Deadweight; 11-14-2013 at 10:56 AM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by Crazykld69 View Post
    The problem is that I can not check the value that is being sent. I'm trying to send a value from JQuery to PHP under the value $_POST['username'] getting the value from the id='username'

    JQuery grabs the value from text box username then it sends the value that it has taken and send to save.php as $_POST['username'].
    I dont know if that makes sense.

    It still connects to the mysqli_queue because it pops out the value. I guess one thing i can do to check if it works or the error message is echo as json and make the json print out onto the page.
    Exactly.

    Did you try? What was the error?

    Quote Originally Posted by Crazykld69 View Post
    Also, i used JQuery to check the validation of the textbox. You will not be able to hit the button unless everything is correct.
    Of course I could.

    JavaScript is for convenience, not security. Your javascript code runs on the user's browser and is under the complete control of the user. It can be changed, it can be turned off. I could even make my own form and submit it to your script myself, without even needing to visit your website. You always need to validate and sanitize user input on the server.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    ...I could even make my own form and submit it to your script myself, without even needing to visit your website. ...
    As far as I know, that's what bots do most of the time, for example.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. Replies: 1
    Last Post: 02-06-2013, 02:31 AM
  2. jQuery POST tinyeditor
    By ggalan in forum JavaScript
    Replies: 1
    Last Post: 10-22-2011, 03:41 AM
  3. Resolved jQuery post
    By ggalan in forum JavaScript
    Replies: 5
    Last Post: 10-06-2011, 05:57 PM
  4. Cant get form to Post to mysql database.
    By trazix in forum MySQL and other databases
    Replies: 4
    Last Post: 02-16-2010, 07:49 PM
  5. Form won't post to database.
    By Snapa02 in forum ASP
    Replies: 1
    Last Post: 03-25-2007, 04:43 AM

Bookmarks

Posting Permissions

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