Calling PHP method with ajax

Asked

Viewed 1,085 times

3

I have a code that does a validation in javascript, and I would like it to be validated as true, change a table of the database. I thought to use ajax, so as soon as validate as true, call a method in a.php file, but I don’t 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’s name is equal to that of Session. It would be 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
});
  • The question is about this Javascript snippet, or about the PHP part?

  • About ajax. The method to change the table I know, do not know how to call it by sending this SESSION

1 answer

5


The code is almost that, but the parameter data must contain an object (while 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 past value will be in $_POST["session"] (being "Session" the name of the key where you placed the value in JS.

The above code assumes that PHP can run in the context of this Javascript, and get Session. If it doesn’t work, try to cast 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
});
  • That will be good practice ?

  • In the script part, I manipulate this value sent in the date part as? $variavel = session; ?

  • 1

    @Edilson What exactly?

  • 1

    @William Avoid mixing PHP with JS. Don’t you have user code somewhere on the page but in PHP? For example, in the URL?

  • Not only do I have in PHP even... I know it’s a disgusting trick to mix the two languages, but I need that if the validation in JS returns true, perform a change in the SQL database, and unless I can manipulate the database through JS I see no other option :

  • 1

    @Guilherme Use PHP to save user code in a Hidden field (echo '<input type="hidden" id="usr" value="<?php echo $_SESSION["membro"]?>">). In JS you can take the value of this field to pass back to the server.

  • How do I send this input by the part data of JS? It would look something like this data: { session: usr } ? And how I would handle this variable within PHP?

  • @William data: { session: $('#usr').val() }

  • And I can treat her like that in PHP? $variavel = $_POST['usr'];

  • 1

    No, she’ll come as $_POST['session'], and not $_POST['usr']. The name of key that you use in the data JS will be the key in PHP as well. @Guilherme

  • Dude, I totally understood and I think I edited the code correctly, but it’s not running SQL. I edited the main post with JS and PHP ready, have you take a look on top to see if you think something is wrong? Because I don’t know if the error is in the JS that isn’t calling the script or in PHP itself

  • @William It is better to post a separate question about this, instead of editing this (here on the site we do so because it increases the chance of each question helping other people, and not only who asked).

  • Ok! I will open right now. Thank you very much xD

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.