2

I want to store data into database using jquery ajax using php but when i click on submit button a empty alert shows up and a message below submit button submitted succesfully but no data is added on my databse.i am at very beginer level in jquery and ajax...Any Help will be appreciated.

Here is my HTML

<form method="POST" >
        <pre>
            <label for="">Enter Post Topic</label><input type="text" id="txt_topic_name" name="txt_topic_name"><br>
            <label for="">Enter Detail</label><textarea id="txt_detail" name="txt_detail"></textarea><br/>
            <label for=""></label><input type="button" id="btn_submit" name="btn_submit" value="submit"><br>
        </pre>
    </form>
    <div id="results"></div>

And here is my javascript

$(document).ready(function(){
        $('#btn_submit').on('click',function(){
            var topic_name = $('#txt_topic_name').val();
            var detail     = $('#txt_detail').val();
            $.ajax({
                url   : "ajax/add_topic.php",
                type  : "POST",
                data  : {'txt_topic_name' : topic_name ,'txt_detail' : detail},
                success : function(data){
                    alert(data);
                    console.log(data);
                    $('#results').html("submitted succesfully");
                },
                error  : function(data){
                    // alert(data);
                    // console.log(data);
                }

            });
             // return false;

        });

     });

And PHP

if (isset($_POST['btn_submit'])) {
    mysql_connect("localhost","root","") or die("Could not coonnect");
    mysql_select_db("forum") or die("could not select db");

    $topic_name  = mysql_real_escape_string($_POST['txt_topic_name']);
    $detail = mysql_real_escape_string($_POST['txt_detail']);

    $sql    = "INSERT INTO Topics(name,detail)  VALUES('$topic_name','$detail')";
    $query  = mysql_query($sql);
    if ($query) {
        echo "Sucess";
    }
    else{
        echo "Failed";

    }

}
12
  • what is the error you got? Commented Jul 18, 2014 at 7:00
  • You can't able to use Form Submit and Ajax both at same time... Both are different from each other Commented Jul 18, 2014 at 7:00
  • No error just a alert pops up from success function and displays nothing Commented Jul 18, 2014 at 7:01
  • @AntoKing that was only a button not the submit button Commented Jul 18, 2014 at 7:02
  • so how should i do it can you explain it... Anto King Commented Jul 18, 2014 at 7:02

2 Answers 2

1
if (isset($_POST['txt_topic_name'])) {
    mysql_connect("localhost","root","") or die("Could not coonnect");
    mysql_select_db("forum") or die("could not select db");

    $topic_name  = mysql_real_escape_string($_POST['txt_topic_name']);
    $detail = mysql_real_escape_string($_POST['txt_detail']);

    $sql    = "INSERT INTO Topics(name,detail)  VALUES('$topic_name','$detail')";
    $query  = mysql_query($sql);
    if ($query) {
        echo "Sucess";
    }
    else{
        echo "Failed";

    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Feel free to add some text to explain what you did and why.
Thnx Man it's added in the database.Can you plz explain to me what was i doing wrong.
@Er. Sajal Tiwari can you please explain it what i did wrong.?
actually you are posting the following data 'txt_topic_name' : topic_name ,'txt_detail' : detail ,and in your php file yor are catching button which has not been posted.
0

The data object should be like this

data: {txt_topic_name : topic_name ,txt_detail : detail}

instead of this

data: {'txt_topic_name' : topic_name ,'txt_detail' : detail}

And for a better check for a post, use a hidden input

<input type='hidden' value='true' name='submission' />

Then in php check for submission

if(isset($_POST['submission']) && $_POST['submission'] == 'true'){
  //the rest of your code
}

5 Comments

it still displays nothing on alert
@Xlander The data object should be like this... Both are the same
Have you include submission: 'true' in the data object?
@hindmost: Hmmm... Well, I used to write it this way :p... nevermind.
data: {txt_topic_name : topic_name ,txt_detail : detail, submission: $('input[name="submission"]').val()} Then in your success function, check for the value data: if(data == 'Sucess') $('#results').html("submitted succesfully"); else $('#results').html("error");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.