My Ajax call does not submit to php

Following snippet should be collecting data and sending it to a php file. It does output proper values on click of the button.

var dataString = 'username='+SessionVars.my_username+'&lessonid='+SessionVars.my_lesson_number;
$('#tracking_submit').click(function(){
$.ajax({
    url: "php/tracking.php",
    type:'POST',
    data: dataString,
    success: function(){
    $('#tracking_message').replaceWith(SessionVars.my_username+"Thank you for updating."+SessionVars.my_lesson_number);
    }                   
    });
return false;   
});

Then the php file portion i'm using is this:

session_start(); 
mysql_connect("stuff i tested and it works");
mysql_select_db("same");

$username=$_POST['username'];
$lessonid=$_POST['lessonid'];

mysql_query("INSERT INTO tracking ( username, lessonid ) VALUES ( ".$username.", ".$lessonid." );");

When I check the database, there is nothing in it.

Answers


This cannot work, since you're not putting values in your query into quotes. This should help and prevent you being hacked as well:

mysql_query("INSERT INTO tracking ( username, lessonid ) VALUES ( '".mysql_real_escape_string($username)."', '".mysql_real_escape_string($lessonid)."' )");

mysql_query("INSERT INTO tracking ( username, lessonid ) VALUES ('".$username."', ".$lessonid." );");

You missed quotes in that line. Also, please read about SQL Injection.


change to..

$.ajax({
    url: "php/tracking.php",
    type:'POST',
    cache: false,
    dataType: "json",
    data: {username: SessionVars.my_username, lessonid: SessionVars.my_lesson_number}, 
    success: function(){
    $('#tracking_message').replaceWith(SessionVars.my_username+"Thank you for updating."+SessionVars.my_lesson_number);
    }                   
    });
return false;   
});

do a print_r ($_POST) on the next page. Also yes, you should readup on some sql injection prevention.


Need Your Help

How can I set a background image for a UILabel and adjust its alpha without changing the transparency of the text?

iphone uikit ios

How can I create a UILabel with a background image and adjust the transparency (alpha channel) of the background image without changing the transparency of the text?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.