Welcome Guest, Not a member yet? Register   Sign In
Ajax and mysql


I need help, i will not go in the details of my problem, i will tell u just the script i want.

lets say i have a database and a table, in this table there is a column that takes only the values 1 and 0.

in the site if the value is 1 a green dot appears if its 0 then a red dot appears.

i need ajax or javascript to help me change the value of the column from 1 to 0 and from 0 to 1, just by clicking on the dot.

so the result is: when i click on the green dot the value of the database will be set to 0 and then the red dot appears and verso.

all that without refreshing the page.

btw i am using codeigniter so the method get will have to be changed with the uri segment or something like that!

really needed


Set up a controller and model that will do exactly what you want. This could be a simple function that takes the previous value (1 or 0) and toggles it in the database and returns the html.

Then, on the client side use prototype or some other library to make an Ajax request. Simply update the html with the Ajax response.

Hope that helps.

well the prob is that i don't know how to apply ajax Sad

javascript libraries make it easy for you because they do the heavy lifting, you only have to follow the documentation. For example the jQuery ajax documentation

Sadly, Ajax is one of those things that should be simple but is misunderstood. I suggest you read up on it.

The prototype library has Ajax functions that you may find useful.

Good luck.

piece of cake...

//toggle the dot to green or red...


function changedot(){
$id = $this->uri->segment(n);


or you can use $.get too

sorry imzyos but i seem not to know how to use your script, i think i will read more Query ajax documentation

unless someone gives a more specific script.

i would be glad if anyone could tell me generally what is the structure to use ajax, what is the connection between it and the database.

Ajax provides javascript with the opportunity to call other urls to manipulate or fetch data. It's like using an api.

So you can call any controller method want. You can create an ajax specific controller or you can add the ajax methods to the 'normal' controller. That is for you to decide.

A basic example. You have a controller and method user/update. With jQuery you do
   type: "POST",
   url: "index.php/user/update",
   data: $("form").serialize(), // get all the form field values
   success: function(msg){
     alert( "Data Saved: " + msg );
msg is a string that is echoed by the method. you can send a plaintext string or a formatted string (json,xml). If you send success and error strings in plain text you can prefix them with error : and success : .

I suggest you read as much as you can about ajax and learn to use the methods of the library you are going to use.

very much appreciated xwero i will continue reading

I attach one zip file with an example of the script, just adapt it for your application

i comment the part of $.post
you can get the vars in the controller using

$id = $this->uri->segment(3);

$value = $this->input->post('value');

Theme © iAndrew 2016 - Forum software by © MyBB