Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

inserting to MySQL with AJAX

Hi,

on my research site, I have a few radio buttons and I want to send their values to MySQL table using jquery and AJAX. All buttons and jquery code are put in single-project.php (I modified WordPress theme) and the bits of code that should handle interaction with the MySQL are put in the db.php in the same folder.

However, something is not in order, because values do not appear on the database table. Could someone help?

jquery:

          //the last button
            $('#submit_last_button').click(function(){
            SomeVariable = $('input:radio[name=lastRadio]:checked').val();
                 if (!$("input:radio[name=lastRadio]").is(":checked")) {
                  $("label#lastRadio_error").show();
                  $("input#lastRadio").focus();
                  return false;
                } else {
                if ('input:radio[name=lastRadio]:checked')
                $('#PreviousButtonDiv').hide();
                $('#NextDiv').show();
                $.post('db.php',{action: "submit_last_button", previous_variable:SomePreviousVariable, last_variable:SomeVariable},function(res){
                $('#result').html(res);
                });
                }
              });
            });   

db.php:

<?php
    $con = mysql_connect('localhost','user', 'password');
    $db     = mysql_select_db('my_database');

    if($_POST['action'] == 'submit_last_button'){

        $previous_variable  = mysql_real_escape_string($_POST['previous_variable']);
        $last_variable = mysql_real_escape_string($_POST['last_variable']);




        $sql   = "insert into MyTable (id, variable1, variable2) values ( NULL, '$previous_variable', '$last_variable')";
        $query = mysql_query($sql);
        if($query){
            echo "Record Inserted.";
        }else {
            echo "Something Wrong!";
        }
    }
?>

Whole site in action (if that helps) is in here , though in my native language, so all variable names are different. Please do give some suggestions, thanks in advance!

Dave McFarland
Dave McFarland
Treehouse Teacher

Where do you define the SomePreviousVariable variable in JavaScript? I don't see it defined anywhere?

Also the link to the site, doesn't have any forms on it.

Hi Dave! I`ve putted whole code in here, there are a few variables defined before this given excerpt, in lines 59, 73, 88, 111, 132, 147.

And the div`s has forms, or is it not what you are asking here?

2 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Aidas Praninskas

I can't tell by your code what's wrong. Use Google Chrome to analyze the XHR request. Open the console. Right click in the console and choose the "Log XMLHttpRequests" option. Then submit the form. The request will appear in the console. You can click on it to get more data -- such as what information was sent and what information was received.

Goodness gracious, Dave! I tried that, and it showed that the problem was 404 error with my db.php - a little tweaking and it works! If I could, I would buy you a beer - many thanks again!

Dave McFarland
Dave McFarland
Treehouse Teacher

Ha, ha! A beer -- I'll keep you to that promise ;)