execute a select sending parameters via ajax

0

I have the following AJAX code that passes variables from one form to another page and shows results.

    $(document).ready(function() {
    $('#form, #asigped').submit(function() {
      
        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
    
            success: function(data) {
               console.log(data);
                $('#result').html(data);
            },
            error: function(data) {
               console.log(data);
                $('#result').html(data);
            }
        })        
        return false;
    }); 
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form class="form-inline" id="asigped" 
action="pedidos_clientes_asignapedidoscript.php" method="post" name="fo3">
<div class="form-title">
	<div class="form-group">
		<input name="pedidoid" type="hidden" value="<?php echo ''.$pedidoid.'';?>">
		<label for="nropedidosistema">N&ordm; de Pedido del Sistema de 
			Tr&aacute;fico</label>
			<input type="text" name="nropedidosistema" class="form-control" 
			id="nropedidosistema" size="10">
			
			<div class="form-group"><button type="submit" class="btn label label-
				danger">Asignar</button></div>
				
			</div>
		</div>		
	</form>
	<div id="result"></div>

This works correctly, but what I can not do is use a variable for a query mySQL .

What I want to do is the following:

<?php
$idpedido = $_POST['pedidoid'];

$p=mysql_query("SELECT id_pedido,id_pedido_trafico,id_cliente,id_sucursal,id_interno,emailconfirmacion 
                                  FROM pedidos_clientes WHERE id_pedido = '$idpedido'");
    while($rp= mysql_fetch_array($p)){
        $Email = $rp["emailconfirmacion"];
        $sucursal = $rp["id_sucursal"];
        $interno = $rp["id_interno"];
    }

?> 

That is, it does not return any results.

If I do var_dump to the variable $idpedido it shows me the result correctly.

    
asked by pointup 13.09.2017 в 15:09
source

3 answers

1

In AJAX if you do not do a echo or a return from php you're not "answering" anything, it's say if the PHP that executes the code does not output what you ask AJAX will not show you anything (if the problem is that AJAX shows nothing).

I would first tell you to do a variable where you save the query and then show it (to make sure the query is well armed)

<?php
$sql="SELECT id_pedido,id_pedido_trafico,id_cliente,id_sucursal,id_interno,emailconfirmacion 
FROM pedidos_clientes WHERE id_pedido = '$idpedido'";
// Luego ejecutas el query, pero en tu caso hacemos echo del query para evaluar que esta bien armado.
echo $sql;
$p=mysql_query($sql);
//Si por aca todo esta bien simplemente sacas la data y la envias de regreso
while($rp= mysql_fetch_array($p)){
$Email = $rp["emailconfirmacion"];
$sucursal = $rp["id_sucursal"];
$interno = $rp["id_interno"];
}
//Podemos hacer un echo con todo o un arreglo (yo prefiero el arreglo se ve mas elegante y menos desordenado)
//El While cambiaria a:
$repuesta = array();
foreach ($rp as $key => $value) {
    $repuesta[$key]=$value;//asignamos todos los campos que te traes del select dentro de un arreglo
}
//enviamos el arreglo a ajax
print_r($repuesta);
?>

When Ajax receives it, do whatever you want with the values

$.ajax({
        type: 'POST',
        url: $(this).attr('action'),
        data: $(this).serialize(),

        success: function(data) {
        /* Si queremos solo el Email, o ponemos cada valor donde deseamos */
            $('#result').html(data.emailconfirmacion);
        }
    })
    
answered by 13.09.2017 / 16:35
source
0

Well, apparently you're not returning anything back, it would be something like that

echo $Email . ' ' . $sucursal . ' ' . $interno;

With that you would receive the results in the data that returns the function success

I hope it works for you:)

    
answered by 13.09.2017 в 16:01
0

I think you just need to deliver the results .. If in javascript you do:

$('#result').html(data);

What you lack in php (at the end) is to do the "echo" .. for example:

echo "Registro insertado, el email es " . $Email . " la sucursal " . $sucursal . "<br/>";

Or something like that ..

    
answered by 13.09.2017 в 16:00