-1

i have a problem with an ajax request and a read/write froma database.

I have this code to submit a form to a database:

$(document).ready(function(){
    $('#myForm').on('submit',function(e) {
        $.ajax({
            url:'save.php',
        type:'POST',

        });
        e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
    });
});

and i have the save.php file into the same fold with this code:

<?php
include('dbconnection.php');

$Name= $_POST['Name'];
$surname= $_POST['surname'];
$Value= $_POST['Value'];

$sql = "INSERT INTO `results`(`Name`, `surname`, `Value`) VALUES('$Name', '$surname','$Value')";

$retval = mysql_query( $sql );

if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}   
?>

the only problem is that the values is not saved inside the db.. What am i doing wrong?

pmandell
  • 4,288
  • 17
  • 21
  • First of all you are not posting the form data in your AJAX call. – pmandell Jun 05 '14 at 13:38
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 05 '14 at 13:58
  • before you do sql insert check if all variables posted to php by adding this `"; print_r($_POST); ?>` this will print out all posted variables in your ajax script alert . – Kodr.F Jun 05 '14 at 13:58

2 Answers2

2

First of all you should know that the Mysql php extension is deprecated, you may consider using mysqli or PDO instead.

To solve your problem, you should send the data with your Ajax call. For example:

$(document).ready(function(){
    $('#myForm').on('submit',function(e) {
        $.ajax({
            url:'save.php',
        type:'POST',
        data: {'Name':'John', 'surname': 'Jonny', 'Value':'foo'}
        });
        e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
    });
});

To see if it works, you may debug your PHP with this:

var_dump($_POST);
lracicot
  • 364
  • 2
  • 15
1

You need to serialize your form data and send it to the database -

$(document).ready(function(){
    // var myData = $('#myForm').serialize();
    $('#myForm').on('submit',function(e) {
        $.ajax({
            url:'save.php',
            type:'POST',
            data: $(this).serialize()
        });
        e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
    });
});

You should also start using mysqli_ or PDO to handle your database with as the mysql_ functions are deprecated. More importantly you should always validate and cleanse your data - you're asking for a SQL Injection attack otherwise.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • You need to open your browser's console and watch the request / response cycle when you perform the action to see if you're getting any errors. – Jay Blanchard Jun 05 '14 at 14:05
  • using the chrome console, after the request, all the var are empty no error spotted – user3668732 Jun 05 '14 at 14:09
  • I made a change to the code, please try that and let me know what happens in your console. – Jay Blanchard Jun 05 '14 at 14:11
  • same as before, but i don't have any out request seen in the console – user3668732 Jun 05 '14 at 14:14
  • It is working perfectly in this fiddle - http://jsfiddle.net/jayblanchard/k9D9G/ Look at the network tab in your console. NOTE: I removed an errant semi-colon after the serialize function. – Jay Blanchard Jun 05 '14 at 14:23
  • ok, but if i use your code the vars are not saved inside the database... the save.php file seams like it wont do anything – user3668732 Jun 05 '14 at 14:25
  • Is your query running properly? We're transmitting the proper variables now, echo out your SQL statement to make sure it is formed properly. Are you seeing the SQL error returned? – Jay Blanchard Jun 05 '14 at 14:26
  • The save.php file works perfectly, cause i have a working code that refreshes the page. i wanted to run as ajax to prevent the page form reload. And you can check the code in my first post – user3668732 Jun 05 '14 at 14:28