Take the 2-minute tour ×

I have seen many tutorials, but they're so confusing, and to do what I want to do, I just don't get how to use existing stuff from those tutorials and make them work they way I want them to.

I have a very simple form, containing a textbox, label and a submit button. When the user enters something into the form, then clicks submit, I would like to use php and ajax (with jquery) to insert the result of the form into a mysql database.

Can someone please show me how this can be achieved? Just something very basic is all i'm after to get me started. Any help is appreciated.

Thank you

share|improve this question
As far as PHP's concerned, data submitted via AJAX is indistinguishable from data submitted via a regular form. It's just a GET/POST call. –  Marc B Feb 28 '11 at 14:31
@Lucifer keep in mind ajax cross-domain is abit complex –  kjy112 Feb 28 '11 at 14:37
Thanks @Marc B, that really helped. –  anon271334 Mar 1 '11 at 22:41
Thanks @kjy112, i'll keep that in mind. –  anon271334 Mar 1 '11 at 22:41

4 Answers 4

up vote 11 down vote accepted

Hi here is just a quick example of how one might do it:


        <title>Quick JQuery Ajax Request</title>

        <!-- include the jquery lib -->
        <script type="text/javascript">
            var ajaxSubmit = function(formEl) {
                // fetch where we want to submit the form to
                var url = $(formEl).attr('action');

                // fetch the data for the form
                var data = $(formEl).serializeArray();

                // setup the ajax request
                    url: url,
                    data: data,
                    dataType: 'json',
                    success: function() {
                        if(rsp.success) {
                            alert('form has been posted successfully');

                // return false so the form does not actually
                // submit to the page
                return false;


        <form method="post" action="process.php"
              onSubmit="return ajaxSubmit(this);">
            Value: <input type="text" name="my_value" />
            <input type="submit" name="form_submit" value="Go" />


The process.php script:


function post($key) {
    if (isset($_POST[$key]))
        return $_POST[$key];
    return false;

// setup the database connect
$cxn = mysql_connect('localhost', 'username_goes_here', 'password_goes_here');
if (!$cxn)
mysql_select_db('your_database_name', $cxn);

// check if we can get hold of the form field
if (!post('my_value'))

// let make sure we escape the data
$val = mysql_real_escape_string(post('my_value'), $cxn);

// lets setup our insert query
$sql = sprintf("INSERT INTO %s (column_name_goes_here) VALUES '%s';",

// lets run our query
$result = mysql_query($sql, $cxn);

// setup our response "object"
$resp = new stdClass();
$resp->success = false;
if($result) {
    $resp->success = true;

print json_encode($resp);

Please note that none of this has been tested. I hope it helps you thou.

share|improve this answer
Oow! Thanks heaps, @ZeSimon. :) –  anon271334 Mar 1 '11 at 22:43

The jQuery part is often quite simple. It just redirects the ordinary form action= over a Javascript handler. $.post is easy to use and you just need .serialize() to package up the existing form values into a string:

<form id="example">
<input name="textbox" ...>
<input type=submit name="submitbuttonname" value="submit"
   onClick="$.post('save.php', $('form#example').serialize())">

And on PHP side you simply receive the content via $_POST and save it to the database (using the old mysql_ functions would also be possible, just more cumbersome):

$db = new PDO("mysql:...");

if ($_POST["submitbuttonname"]) {

   $q = $db->prepare("INSERT INTO save (textbox, label) VALUES (?, ?)";
   $q->execute(array($_POST["textbox"], $_POST["label"]));
share|improve this answer
Are you serious? That's just as easy as inserting the normal way. Thanks! :-D –  anon271334 Feb 28 '11 at 14:32
@Lucifier: Yes. jQuery transforms it into an ordinary POST request. That's why everybody likes it. –  mario Feb 28 '11 at 14:33
@Lucifier i believe PDO support is >= php 5.1.0 make sure your server supports it. –  kjy112 Feb 28 '11 at 14:41
@kjy112: It's available as PECL module since 5.0 and there is also a PDO emulation for PHP4 (and PHP5.0). See xpdo.org or upgradephp –  mario Feb 28 '11 at 14:43
It's cool, I just used the mysql thingy I already had on another page ;) :) –  anon271334 Mar 1 '11 at 22:42

Have a look at this tutorial. It is really easy to follow and to learn it quickly. I have used it myself.


share|improve this answer
Oww darn, i don't have enough bandwidth to see that tutorial :( –  anon271334 Feb 28 '11 at 14:26

Here is a simple connection between AJAX PHP and MySQL for data injection


share|improve this answer

Your Answer

