0

I am using AJAX and PHP to process a form and I've tried for 3 hours and I can't figure out why it doesn't insert the data into the mysql server.

The connection to the server is working. The PHP and html form works when I combine it to one page. This signals to me that its the AJAX but the AJAX seems fine. I really don't know whats going on here. Please help me.

The php to process the code:

<?php
require_once 'config/config.php';
function test_input($data){
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

$que1 = $_POST['que1'];

$sql = "INSERT INTO question_answers 
(
    user_id, que1, que2
)
VALUES 
(
    '99' ,'$que1', '3345'
)";
$conn->close(); 
?>

And the AJAX is here:

$(document).ready(function() {
    $("#submit1").click(function() {
        var que1 = $('#que1').val();
        if (que1 == '')
        {
            alert("Please provide a response."); 
        }
        else {
            $.ajax({
                type: "POST",
                url: "process.php",
                data: {
                    que1: que1
                },
                success: function(msg) {
                    alert($que1);
                    var url = "process.php";
                    $(location).attr('href', url);
                },
                error: function() {
                    alert('Error');
                }
            });
        } 
    });
});

My form is standard:

<form action="" method="POST">  
    Question
    <textarea class="form-control" name="que1" rows="3" id="que1"></textarea>
    <button type="submit" class="btn btn-primary" id="submit1" name="submit1">Submit response</button>
</form>
  • 5
    Please [edit] to add a specific problem statement — "it doesn't work" can be assumed, but *how* does it not work? What error message or incorrect behavior is characteristic? – Nathan Tuggy Aug 02 '15 at 01:02
  • 1
    What if you tried to actually **do** the query you are setting in `$sql`? – D4V1D Aug 02 '15 at 02:46
  • Sorry, what do you mean "do"? I thought the AJAX would execute it? – user3504462 Aug 02 '15 at 20:23
  • Ajax just makes an HTTP request. You have a PHP program which stores an SQL query in a variable but never sends it to a database. – Quentin Aug 02 '15 at 20:33
  • So I need something like this? if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { Sorry for my dumbness.} – user3504462 Aug 02 '15 at 20:40

1 Answers1

0

Science your problem is not clear to me yet, but still I got some errors in your code. In your php file:

    <?php
    require_once 'config/config.php';
    function test_input($data){
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    $que1 = $_POST['que1'];

    $sql = "INSERT INTO question_answers 
    (
        user_id, que1, que2
    )
    VALUES 
    (
        '99' ,'$que1', '3345'
    )";

    /*
     You did not run the query here. 
     Execute the query here and try 
     to echo something as ajax response.
    */
    if (mysql_query($sql))//or mysqli or PDO whatever you use in your connection.
    {
       echo 'success';
    }
    else
    {
       echo 'Error';
    }
    $conn->close(); 
    ?>

And in your Ajax code:

$(document).ready(function() {
    $("#submit1").click(function() {
        var que1 = $('#que1').val();
        if (que1 == '')
        {
            alert("Please provide a response."); 
        }
        else {
            $.ajax({
                type: "POST",
                url: "process.php",
                data: {
                    que1: que1
                },
                success: function(msg) {
                    //alert($que1);
                    //var url = "process.php";
                    //$(location).attr('href', url);
                    alert(msg);
                },
                error: function() {
                    alert('Error');
                }
            });
        } 
    });
});

Check if the alert shows proper message or not.

Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
  • Sorry, what do you mean "do"? I thought the AJAX would execute it? – user3504462 Aug 02 '15 at 20:24
  • **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 **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 Aug 02 '15 at 20:36
  • @user3504462 Ajax will not do anything by itself. Ajax just submit your request asynchronously. You have to execute your query by yourself. – Al Amin Chayan Aug 03 '15 at 03:19
  • @Quentin I just figure him out the problem only and suggest him to use mysqli or pdo on that line too. – Al Amin Chayan Aug 03 '15 at 03:21
  • Ok, this answer helped. Now I understand why nothing worked, I wasn't executing the $sql. Thank you. – user3504462 Aug 03 '15 at 19:49