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
#2

[eluser]Krzemo[/eluser]
Am I missing somthing or there is nothing related to CodeIgniter here?
Why is there POST in JS code
Code:
ajaxRequest.open("POST", "query/writecomments.php", true);
and GET in PHP
Code:
$myimage = $_GET['image'];
?? It doesnt impact anything here but just out of curiosity.
What Firebug says? Are you really making a request? What is the server response?
And why are you 'reinventing the wheel' with JS code? Use jQuery - JS part will be much more pleasant.

If calling the PHP file via URL in browser doent insert data too, apparently there is something wrong with PHP code...
#3

[eluser]agubski[/eluser]
Actually some of my comments were lost Smile

My question was if I can send AJAX request to a controller function rather that calling an external php file.

please disregard
Code:
$myimage = $_GET['image'];
. It is irrelevant at this time.

Thanks

Alex
#4

[eluser]Krzemo[/eluser]
In proper CodeIgniter application you can make requests only to controller functions.
#5

[eluser]agubski[/eluser]
So, will something like this work?

CI controller function. (I know it works as I made calls to it from views and id does). Function is in the controller named "Welcome"
Code:
function insert()
    {
          $comment['Date']=Date('Y-m-d, G:i:s');
          $comment['Name']='Andy';
          $comment['Email']='[email protected]';
          $comment['Folder']='studio';
          $comment['Image']='IMG_0014.jpg';
          $comment['Comment']='Test comment!';
          $comment['Validate']='1';
          $this->db->insert('Comments', $comment);
    
        
    }

The JS on the other hand doesn't seem to do much.

Code:
rest of AJAX function same as above
...
    ajaxRequest.open("POST", "welcome/insert", true);
    ajaxRequest.send(null);

    
}
#6

[eluser]Krzemo[/eluser]
So what is the question? Smile
#7

[eluser]agubski[/eluser]
Sorry fro confusion if I wasn't clear enough. I'd like to pass variables from the form via AJAX call to controller function and save data into the database. I need proper way to write AJAX call to the above controller function. I'm not familiar with jQuery, but has written JS calls before which don't seem to work now for some reason Smile

Appreciate you being that patient so far Smile
#8

[eluser]Krzemo[/eluser]
I would insist on giving a jQuery a try. It has gr8 docs and making it run will take you 30mins max.
If you have a form you are going to submit and you want to do it 'via ajax' jQuery plugin from here http://jquery.malsup.com/form/




Theme © iAndrew 2016 - Forum software by © MyBB