Pass PHP variable to jQuery from different files

1

Good afternoon. I write after reading all the similar cases related to my query in this forum and not finding a solution.

What I need to know is if you can pass the variable from a PHP document to the jQuery code written in another document.

Code example:

File with name "pre_modificar.php" (HTML with JAVASCRIPT)

<html>
<head>
<meta charset="utf-8">
<title>Paso previo a modificar</title>

<script type="text/javascript" src="jquery.tablesorter/jquery-latest.js"></script> 

<script type="text/javascript">

$(document).ready(function() {
	
	$("#modificar").attr('disabled', 'disabled');
                         
      var consulta;
             
      $("#id").focus();
                                                 
      $("#id").keyup(function(e){
             consulta = $("#id").val();
                                      
             //hace la búsqueda
             $("#resultado").delay(100).queue(function(n) {      
                                           
                  $("#resultado").html('<img src="imagenes/loading/ajax-loader.gif" />');
                                           
                  $.ajax({
                       type: "POST",
                       url: "comprobar.php",
                       data: "b="+consulta,
                       dataType: "html",
                       error: function(){
                       alert("Error petición ajax");
                       },
                       success: function(data){    				
                                    
if (VARIABLE_PHP_QUE_NO_SE_COMO_OBTENER == 0) {
$("#resultado").html(data);
n();
} else { 										
$("#modificar").removeAttr('disabled');
$("#resultado").html(data);
n();
}
}

});
});                                   
});                             
});
</script>
</head>

<body>
	
	<form name="seleccionar" action="modificar.php" method="post">
       	<input type="int" required id="id" name="id" placeholder="Introduce ID..." value="" />
		<span id="resultado"></span>		
        <input type="submit" id="modificar" name="modificar" value="Modificar" /><br><br/>		
    </form>
</body>
</html>

I have written as VARIABLE_PHP_QUE_NO_SE_COMO_OBTENER the variable that I should get from the PHP document that I detail below.

File with name "check.php" (PHP)

<?php
      $user = $_POST['b'];
       
      if(!empty($user)) {
            comprobar($user);
      }
       
      function comprobar($b) {
			
			include("conexion_db.php");
			
			$query = mysqli_query($conexion, "SELECT * FROM formulario WHERE id = '".$b."'");        
			
            $contar = mysqli_num_rows($query);
             

            if($contar == 0){
                  echo "<span><img src=\"imagenes\web\action_delete.png\"></span>"; 
						
            }else{
                  echo "<span><img src=\"imagenes\web\action_check.png\"></span>";
            }
      }     
?>

The variable I want to pass from this PHP document to the first document where the jQuery code is is: $ count

Could you tell me if there is a possible solution to solve this case?

Thank you very much in advance!

    
asked by Joel 28.08.2017 в 20:28
source

1 answer

1

Return a json object with the html you want to show and the counter. For that you can use the class stdClass :

function comprobar($b) {

            include("conexion_db.php");

            $query = mysqli_query($conexion, "SELECT * FROM formulario WHERE id = '".$b."'");        

            $contar = mysqli_num_rows($query);
            $response = new stdClass();
            $response->contador = $contar;

            if($contar == 0){
                  $response->img = "<span><img src=\"imagenes\web\action_delete.png\"></span>"; 

            }else{
                  $response->img ="<span><img src=\"imagenes\web\action_check.png\"></span>";
            }

          echo json_encode($response);
      }  

Then in the response of the ajax you will have 2 properties that would be contador and html that you can use to do what you want:

 $.ajax({
    type: "POST",
    url: "comprobar.php",
    data: "b="+consulta,
    dataType: "html",
    error: function(){
        alert("Error petición ajax");
    },
    success: function(data){                    

        if (data.contador == 0) {
            $("#resultado").html(data.html);
            n();
        } else {                                        
            $("#modificar").removeAttr('disabled');
            $("#resultado").html(data);
            n();
        }
    }
});
    
answered by 28.08.2017 в 20:43