Send PHP command via Ajax?

Asked

Viewed 1,339 times

2

There is how to send a PHP command via Ajax?

For example:

$.ajax({
        url: 'index.php',
        type: 'POST',
        data: '<?php echo "teste"; ?>',
        success: function(r) {
         $('body').html(r);
    }
}); 

In case, I’m still studying Ajax, I did not understand very well, I made this example code, but for example, what it does?

He sends what’s on data: to url index.php or it takes the content of index.php?

If the answer is the second, then what is the date in ajax for?

But going back to my question, what I really wanted to know is if you have, send PHP command via Ajax.

4 answers

4


To better understand the functioning of ajax, we will break your code in parts:

Here your code is saying that you want to use ajax to send and/or receive data from another page asynchronously:

$.ajax({

});

The url determines the URI from which it will make the request:

url: 'index.php',

The type specifies the type of the request, ie it tells whether the data will be sent in the form of GET or POST:

type: 'POST',

The data is the information to be sent to the request url, it is common to give "name to oxen":

data: { teste: '<?php echo "teste"; ?>' },

The success decides what to do if the request is successful, the parameter it receives is the data returned from the page:

success: function(r) {
     $('body').html(r);
}


And how to execute php code sent by ajax?

Whereas you have given a name to the data sent, you can use Val for this:

eval($_POST['teste']);

Watch out! The eval can bring serious invulnerabilities to your system, especially when the client-side is involved!

See more on documentation ajax and in the documentation of the Eval

  • How can I use a direct PHP command in Ajax? For example, without having to touch the file . php

  • @Lucascarvalho You want to send the result of the command, or the command to be executed?

  • in case, already execute the command. I guess this does not have how right? For example, send an echo and it automatically run.

  • Yes, it is exactly the way you are already doing it. But the file that ajax is in should be .php.

  • Hi Francisco. I think I bumped into the cell phone screen and caused a -1 unintentionally in your answer. Only now the system only lets you withdraw if the answer is edited. Do not want to take advantage and put a danger alert in the part that speaks of Eval? ;)

  • @There’s something very strange about what you’re saying. By ajax you will always transmit text such as field values (or a file in case of upload). At most you can send PHP code (as text) and run with Eval as Francisco showed. But in 99.99999% of cases this is unnecessary, and can be extremely risky in terms of safety.

  • I saw it, I got it now. It sends like a "url," one for example? text=asdas I asked if you noticed, put was trying to send something like this to my server and did not know how it arrived there.[

  • @bfavaretto I’ve edited the answer.

  • @Lucas If you change the method from POST to GET in the code, the data is sent in the URL in the format you mentioned. With post is similar, but not in the URL, but in the body of the HTTP request.

Show 4 more comments

1

What do you put in data is what will be sent to the file you put in url, in your case is the index.php, in your code, you would be passing the string "test" pro index.php.

Code PHP mixed with Ajax works, but your file must have the . php extension and your ajax code must be surrounded by the tags <script></script>.

Functional example (inside a.php name file):

<script src="jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $.ajax({
            url: 'index.php',
            type: 'POST',
            data: '<?php echo "teste"; ?>',  
            success: function(r) {
                $('body').html(r + '<?php echo "código php" ?>');
            },
            error: function(r){
                alert('deu erro');
            }
        });
    })
</script>

1

this 'date' option of ajax serves for you to pass parameters or values that will be encapsulated in the body of the request and can be used in the url that you are calling.
For example, imagine an ajax that takes the data of a specific user, you can go through the date the id of that user for the server to know the user and return that data to you. Date example:

$.ajax({                    
  url: 'index.php',     
  type: 'GET',
  data : {
    id_usuario : '1' // vc pode pegar esse id_usuario dentro do index.php
  },             
  success: function(data)         
  {
    // etc...
  } 
});

The return of the request, which in this case is your index.php, is in Success.

success: function(r) {
         $('body').html(r);

In case the return of index.php is inside the variable r and inside the Function of Success you can treat it or do what you need

0

You can send parameters (via data:{parametro1:"limpar", parametro2:"xxx"}) for the data to be processed in different ways in the request link. There are several ways to serialize this data/parameters. For example, to send an entire form for the request you can send as follows:

data: $("#meu_form").serialize(),

And of course there are more complex forms if using the AJAX with controllers MVC and such, but I believe that for the beginning of studies this already clarifies a little.

Your code sends the data with parameters POST to the index.php and their return (within the success) is written in tag body of the page ($('body').html(r);)

url

A string containing the URL to which the request is sent.

type

An alias for method. You must use type if you are using versions of jQuery before 1.9.0.

date

Type: Plainobject or String or Array The data to be sent to the server is defined. It is converted into a query string if it is not yet a string. It is attached to the GET request url. The object must be key pairs / value. If the value is a matrix, jQuery serializes several values with the same key based on the traditional configuration value.

Source and additional reading:

http://api.jquery.com/jquery.ajax/

Browser other questions tagged

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