Welcome Guest, Not a member yet? Register   Sign In
Insert record via AJAX
#1

[eluser]agubski[/eluser]
Dear community,

I have a difficulty to realize AJAX record insertion into a database. I'm calling a PHP file via following function
Code:
function ajaxFormPost1(){
    
    var ajaxRequest;
    
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            alert("Done!");

        }
        
    }
    ajaxRequest.open("POST", "query/writecomments.php", true);
    ajaxRequest.send(null);

    
}

and actual file looks like

Code:
<?php
$myimage = $_GET['image'];
//$myname = $_GET['name'];
//$myemail = $_GET['email'];
//$mycomment = $_GET['comment'];

$username = "myname";
$password = "mypass";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

mysql_select_db("agubski_lightmgc", $dbhandle);

mysql_query("INSERT INTO Comments (Date, Name, Email, Folder, Image, Comment, Validate)
VALUES ('2011-01-15', 'Peter', '[email protected]', 'studio', 'IMG_0014.jpg', 'Test comment', '1')");

//close the connection
mysql_close($dbhandle);
?>

similar calls to retrieve data work normally. I wonder if I can call a controller function via AJAX call and have it do the insertion?

Thanks for any advice


Messages In This Thread
Insert record via AJAX - by El Forum - 01-20-2011, 02:05 PM
Insert record via AJAX - by El Forum - 01-20-2011, 03:15 PM
Insert record via AJAX - by El Forum - 01-20-2011, 04:01 PM
Insert record via AJAX - by El Forum - 01-20-2011, 04:03 PM
Insert record via AJAX - by El Forum - 01-20-2011, 04:22 PM
Insert record via AJAX - by El Forum - 01-20-2011, 04:35 PM
Insert record via AJAX - by El Forum - 01-20-2011, 04:40 PM
Insert record via AJAX - by El Forum - 01-20-2011, 04:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB