A: 

You have to use data as key for your data.

 $.ajax(url: "ajax_subscribe.php", 
         method : 'POST',
         data: { email: email_value , category: category_value },           
         success: function(response) {

                if (response!='') {alert(response)};
                alert('Thank You !');

            });
Teja Kantamneni
It still doesn't work. Now the "Thank You" message doesn't pop up. humm..
0SX
-1: Incorrect. You are describing the use of jQuery.ajax() _not_ jQuery.post()
hobodave
Fixed it. This should work although not exact sol.
Teja Kantamneni
+1  A: 

Your PHP sql_quote function is very naive with it's str_replace() filtering. It is trivial to bypass this and insert unwanted data in your database.

I suggest the following rewrite of your code:

<?php
$host = "localhost";
$user = "some_user";
$password = "some_pass";
$database = "newsletter";

$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);

function sql_quote($value)
{
    if (get_magic_quotes_gpc()) {
        $value = stripslashes($value);
    }
    return mysql_real_escape_string($value);
}

$email = $_POST['email'];
$category = $_POST['category'];
if (filter_var($email, FILTER_VALIDATE_EMAIL) 
    && FALSE !== filter_var($category, FILTER_VALIDATE_INT)
) {
    $q = sprintf("INSERT INTO emails (email, category) VALUES ('%s', '%s')",
        sql_quote($email),
        sql_quote($category)
    );
    // execute query
} else {
    // Do what you want with invalid data
}

I'd also suggest the following changes:

Edit:

Why are you even using AJAX to process this form submission? I don't see any benefit in it. You're not doing anything special, just submitting a form.

I'd suggest removing the AJAX altogether and just using the submit button as it's intended.

If you insist though, you can at least temporarily remove it to simplify your testing.

hobodave
Thanks, I'll take your advice.
0SX
The reason why is because some my backend is written in AJAX so just I just thought I would use it here. In my backend I use AJAX to submit emails and it works fine but for some reason I can't get it working with the ajax_subscribe.php script. I'm just going to remove the AJAX until I have time to fix it. Thanks again for the help.
0SX
+1  A: 

You should definitely rewrite your code as hobodave suggests. I think something is wrong with your db configuration, though. Try this in the meantime, to execute your query:

$result = mysql_query($q);
if( $result ){
    echo( 'OK' );
} else {
    echo( 'Invalid query: ' . mysql_error() );
}
postpostmodern
Finally, I got it working with this solution. Thanks!!!
0SX
A: 

You have a syntax error in your query try this

$email = sql_quote($_POST['email']);
$category = $_POST['category'];
$q = "INSERT INTO emails (email,category) VALUES ('$email','$category')";
streetparade