Call PHP method with ajax

3

I have a code that does a validation in javascript, and would like it if validated as true, change a table from the database. I thought of using ajax, so as to validate as true, call a method in a .php file, however I do not know how to do it.

It would be necessary to send a session to ajax $_SESSION['membro']; and ajax with that session, change in the database where the member name is the same as the session. Was it something like this?

$.ajax({
    url: 'script.php', //caminho do arquivo a ser executado
    dataType: 'html', //tipo do retorno
    type: 'post', //metodo de envio
    data: session:$_SESSION['membro'], //valores enviados ao script
});
    
asked by anonymous 14.05.2015 / 00:38

1 answer

5

The code is almost this, but the parameter data must contain an object (whereas in your code there is a syntax error, hehe):

$.ajax({
    url: 'script.php', //caminho do arquivo a ser executado
    dataType: 'html', //tipo do retorno
    type: 'post', //metodo de envio
    data: { session: '<?php echo $_SESSION['membro'] ?>' } //valores enviados ao script
       // ^     ------ faltavam as chaves acima -------  ^ 
});

In PHP, the value passed will be in $_POST["session"] (being "session" the name of the key where you put the value in JS.

The above code assumes that PHP can run in the context of this JavaScript, and get the session. If it does not work, try to cast the session like this:

$.ajax({
    url: 'script.php', //caminho do arquivo a ser executado
    dataType: 'html', //tipo do retorno
    type: 'post', //metodo de envio
    data: { session: 100 } //valores enviados ao script
});
    
14.05.2015 / 00:46