Return Multiple values with PHP jQuery

0

I have a function that returns me the values of a Grid in a Modal I call the function it does a Select in PHP and it returns me the data inside a Modal. But in addition to bringing this data I need to bring image names that are in another table but I can not do it.

JS

function GetUserDetails(id) {
    // Add User ID to the hidden field for furture usage
    $("#hidden_user_id").val(id);
    $.post("ajax/readUserDetails.php", {
            id: id
        },
        function (data, status) {
            // PARSE json data
            var user = JSON.parse(data);
            // Assing existing values to the modal popup fields
            $("#show_id").val(id);
            $("#show_emailcontato").val(user.EmailContato);
            $("#show_titulo").val(user.titulo);
            $("#show_tipo_material").val(user.tipo_material);
            $("#show_acabamento").val(user.acabamento);
            $("#show_quantidade").val(user.quantidade);           


        }
    );
    // Open modal popup
    $("#show_user_modal").modal("show");
}

readUserDetails.php

// include Database connection file

include("db_connection.php");

// check request
if(isset($_POST['id']) && isset($_POST['id']) != "")
{
    // get User ID
    $user_id = $_POST['id'];

    // Get User Details
    $query = "SELECT * FROM TblPedidos ped LEFT JOIN TblCadastros cli ON ped.TipoCadastro = cli.TipoCadastro AND ped.idcliente = cli.id   WHERE ped.id = '$user_id'";
    if (!$result = mysqli_query($con, $query)) {
        exit(mysqli_error($con));
    }
    $response = array();
    if(mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $response = $row;
        }
    }
    else
    {
        $response['status'] = 200;
        $response['message'] = "Data not found!";
    }
    // display JSON data
    echo json_encode($response);
}
else
{
    $response['status'] = 200;
    $response['message'] = "Invalid Request!";
}

I need to insert this query in readUserDetails.php

$query = "SELECT * FROM 'tblpedidos_upload' WHERE idpedido = '$userid' ";

if (!$result = mysqli_query($con, $query)) {
    exit(mysqli_error($con));
}
    
asked by anonymous 18.07.2016 / 20:45

2 answers

0

I decided to create a new GetUserImage (id) function and called the 2 functions in

<a href="#" onclick="GetUserDetails('.$row['id'].');GetUserImage('.$row['id'].');"" ></a>

Not the best way but solved!

    
20.07.2016 / 06:40
1

Play the result of the two queries in an array

$query1 = "SELECT * FROM 'tblpedidos_upload'...";
$query2 = "SELECT * FROM TblPedidos ...";

if (!$result1 = mysqli_query($con, $query1)) {
    exit(mysqli_error($con));
}

if (!$result2 = mysqli_query($con, $query2)) {
        exit(mysqli_error($con));
}
$response = array();
if(mysqli_num_rows($result1) > 0) {
  while ($row = mysqli_fetch_assoc($result1)) {
    $response['tblpedidos_upload'][] = $row;
  }
}
if(mysqli_num_rows($result2) > 0) {
  while ($row = mysqli_fetch_assoc($result2)) {
    $response['TblPedidos'][] = $row;
  }
}
....
echo json_encode($response);

So in js you will have an array for each query

data.TblPedidos
data.tblpedidos_upload
    
19.07.2016 / 03:19