On Sat, Dec 13, 2008 at 5:48 PM, Abethebabe <abrahamalra...@gmail.com> wrote:
>
> Hello, I was wondering how to get a value from a php file after I send
> data to it using the $.ajax() method. Here's my code.
>
> jQuery:
> var path = $(this);
> var addtask = $('.inputtask').val();
>        $.ajax({
>                url: 'todolistadd.php',
>                data: 'addtask= ' + addtask,
>                success: function() {
>                        // Stuff
>                }
>        });

http://docs.jquery.com/Ajax/jQuery.ajax#options

var path = $(this);
var addtask = $('.inputtask').val();
       $.ajax({
               url: 'todolistadd.php',
               data: 'addtask= ' + addtask,
               success: function(msg) {
                       alert(msg);
               }
       });


> PHP:
>
>        $addtask = $_POST['addtask'];
>
>        $q = "INSERT INTO task (tasks) VALUES ('$addtask')";
>        $ans = mysql_query($q);
>
>        $q = "SELECT task_id FROM task WHERE tasks = '$addtask' LIMIT 1";
>        $newid = mysql_query($q);
>

header('Content-type: text/plain');
echo $newid;

And, you can simplify that some. The 2nd query is unnecessary.

header('Content-type: text/plain');
echo mysql_insert_id();

I do hope you'll do some validation/sanitizing of that POSTed input,
though. I suggest you have a look at the MDB2 PEAR module.

Reply via email to