Send PHP command via Ajax?

1

Is there a way 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 the case, I'm still studying Ajax, I did not quite understand, I did this example code, but for example, what does it do?

Does it send what it has in data: to url index.php or does it get content from index.php?

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

asked by anonymous 24.10.2017 / 14:38

4 answers

3

To better understand how ajax works, let's break your code into parts:

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

$.ajax({

});

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

url: 'index.php',

The type specifies the type of the request, that is, it talks about whether the data will be sent as GET or POST :

type: 'POST',

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

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

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

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

And how to run the php code sent by ajax?

Considering that you have given a name to the uploaded data, you can use eval for this:

eval($_POST['teste']);

Caution! The eval can bring serious invulnerability to your system, especially when the client-side is involved!

  

See more in AJAX documentation   and the eval documentation

    
24.10.2017 / 15:59
0

You can send parameters (via data:{parametro1:"limpar", parametro2:"xxx"} ) so that the data is handled in different ways in the request link. There are several ways to serialize these data / parameters. For example, to send an integer form to the request you can send it as follows:

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

Of course there are more complex forms if I use AJAX with MVC drivers and the like, but I think that for the beginning of studies this already clarifies a little.

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

url

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

type

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

data

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 already a string. It is attached to the url for GET requests. The object must be key / value pairs. If the value is an array, jQuery serializes multiple values with the same key based on the value of the traditional configuration.

Source and supplementary reading:

  

link

    
24.10.2017 / 14:54
0

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

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

Now the return of the request that in the case is your index.php, is in the success.

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

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

    
24.10.2017 / 14:57
0

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

Passing code index.php mixed with Ajax works, however your file must have a .php extension and your ajax code has to be wrapped by PHP tags.

Functional example (inside a file name.php):

<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>
    
24.10.2017 / 14:53