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

[eluser]theighost[/eluser]
Hi,

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

regards!
#2

[eluser]mdowns[/eluser]
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.
#3

[eluser]theighost[/eluser]
well the prob is that i don't know how to apply ajax Sad
#4

[eluser]xwero[/eluser]
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
#5

[eluser]mdowns[/eluser]
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.
#6

[eluser]imzyos[/eluser]
piece of cake...

$('#dot').click(function(){
$.post(
'http://127.0.0.1/app.php/changedot/id',
function(){
//toggle the dot to green or red...
}
);
});

controller

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

}

or you can use $.get too
#7

[eluser]theighost[/eluser]
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.
#8

[eluser]xwero[/eluser]
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
Code:
$.ajax({
   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.
#9

[eluser]theighost[/eluser]
very much appreciated xwero i will continue reading
#10

[eluser]imzyos[/eluser]
I attach one zip file with an example of the script, just adapt it for your application

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB