I am trying to create a PHP page that uses JQuery and AJAX to insert data into 
a database. Here is the code I've written so far:

The form is a simple form with 3  user input fields and 2 hidden data fields:

<form name="addAnno" method="post" action="">
<label for="subject">Title:</label><br />
<input type="text" id="subject" size="25"><br />
                                        
<label for="author">Author:</label><br />
<input type="text" id="author" size="25"><br />
                                        
<label for="newanno">Annotation:</label><br />
<textarea id="newanno"></textarea><br />
                
<input type="hidden" id="pid" value="<?php echo $pid; ?>" />
<input type="hidden" id="annoid" value="<?php echo $annoid; ?>" />
                                        
<input type="submit" value="Add" name="submit" id="submit" />
</form>



The JQuery part of the page is as follows:

$(form#annoAdd).submit(function() {
        $.ajax({
                type: "POST",
                url: "addanno.php",
                data:     "subject=" + document.getElementById("subject").value 
+ "&author=" + document.getElementById("author").value + "&newanno=" + 
document.getElementById("newanno") + "&pid=" + document.getElementById("pid") 
+"&annoid=" + document.getElementById("annoid") ,
                success: function(html){
                                alert(html);
                                $("#response").html(html);
                        }
        });
});

It basically creates the variable string and calls the addanno.php file with 
the string generated as the arguements. Below is the getanno.php file. It 
simply opens the db, inserts a row and closes and prints a message in the span 
specified in the main file:

<?php
        $pid = $_GET['pid'];
        $annoid = $_GET['annoid'];
        $title = $_POST['subject'];
        $author = $_POST['author'];
        $anno = $_POST['newanno'];
        
        $query = "INSERT INTO annodb VALUES('asda', 1, 'asdasd', 'asdasd', 
'asdadasd');";
        
        include("includes/dbconnect.php");
        $result = mysql_query($query);
        include("includes/dbclose.php");
        
        if($result)
                echo "New annotation added\n";
        else
                echo "An error occured\n";
?>


This whole setup isn't seem to be working. Am I doing something wrong? My DB 
connectivity works and all the required files exist and are included. If you 
find a syntax error like a missing bracket, it's probably because I pasted it 
wrong...

Regards
Manohar Vanga

Reply via email to