Php as a function of javascript

2

Good evening, someone to guide me, I want to execute PHP code from a function in js, I can do that.

I have tried and it just throws the result in an input text, but it does not send me the alert I can do this, Thanks in advance.

 function folio_generate()
  {
    alert('<?php 
        $folios_key = ReceiptData::getAllFolios();
          if(count($folios_key)>0):
            foreach($folios_key as $items):
              $folio = $items->ask;
              if(@$folio=="")
                {
                  $folio = 1;
                } else {
                  $folio++;
                }
                echo "Folio generado: ".$folio;
            endforeach;
          endif;

      ?>');

  }
    
asked by Ever 12.06.2017 в 03:47
source

3 answers

3

I recommend you use ajax to achieve your goal.

$.ajax({
url:'miphp.php?codigo=1',
type:'post',
data:'{nombre:"Carlos"}',
success:function(r){
//hacer algo
   console.log(r);
}
});

php code - file: miphp.php

$codigo = $_GET["codigo"];
switch ($codigo){
case 1:
     //Llama a tu funcion php
     echo hacerAlgoPhp();
     break;
case 2:
// ...
}

function hacerAlgoPhp(){
return "hola ".$_POST["nombre"];
}

This way you can execute php code from javascript

    
answered by 12.06.2017 в 04:20
0

You must make an AJAX for your purpose. You have to see well what type of method you use, if it is a request GET or POST

AJAX POST:

$.ajax({
   url:'miphp.php';
   type : 'POST';
   dataType : 'json',
   data:'{nombre:"Carlos"}';
   success:function(r){
      console.log(r);
   },
   error: function(error) {
   }
});

PHP FOR AJAX POST

The return must be a json, therefore in PHP it occupies json_encode($result) ;

AJAX GET

var nombre = "Carlos";
$.ajax({
    type: "GET",
    url: "miphp.php",
    data: nombre,
    success: function(data){
      console.log(data)
    },
    error : function(error){
    }
});
    
answered by 13.06.2017 в 18:25
0

This answer is only a complement to the answer given by @joseFranciscoSotteccani. As he says in his answer, the best thing in your case is to use Ajax .

The reason for using Ajax is not a whim. Apart from not wanting an innocent kitten to die, the code that runs on the server must remain on the server, separate from the code that runs on the client. This will allow you to have your code more controlled in each environment apart from making it more maintainable and scalable. Imagine how it would be over time to maintain a code where the HTML , the CSS , the JavaScript and the PHP were all mixed. How would you know where to find a specific functionality? How do you debug a code with these characteristics quickly and efficiently? If you start a project using bad practices I assure you that the more you grow, the problems and the headaches will grow with you.

Regarding your specific problem, to achieve what you want you should not change your code PHP almost nothing. You just have to call it using Ajax and use in JavaScript the variables that you receive. Just keep in mind that as the variable $folio you receive within a cycle for it is best to save all the values returned $items->ask within a Array . Here is an example:

PHP Code:

$folios_key = ReceiptData::getAllFolios();

$folios_array = array();

if (count($folios_key) > 0) {

    foreach ($folios_key as $items) {

        $folio = $items->ask;

        $folios_array[] = ($folio == "") ? 1 : $folio + 1;

    }

}

echo json_encode($folios_array);

JavaScript code:

function folio_generate () {

    $.get("path/a/tu/fichero.php", function (folios) {

        alert("Los folios generados son: " + folios.join(", "));

    }, "json");

}
    
answered by 13.06.2017 в 18:13