Return Multiple values with PHP jQuery

Asked

Viewed 277 times

0

I have a Function that returns me the values of a Grid in a Modal I call the function it makes a Select in PHP and returns me the data within a Modal. Except that besides bringing this data I need to bring names of images that are in another table only that I’m not getting.

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 into readUserDetails.php

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

if (!$result = mysqli_query($con, $query)) {
    exit(mysqli_error($con));
}
  • sql Injection, use prepare...

2 answers

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
  • Cool @fernandoandrade liked the idea I’ll test here

  • It didn’t work out stopped working everything

  • Jeez, what a mistake you’re making?

0


I solved otherwise created a new function Getuserimage(id) and called the 2 functions in the

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

It’s not the best way but it’s solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.