ruzD ruzD - 10 months ago 58
Ajax Question

Bootstrap - jquery php to load input fields of modal-window

I would want to load a input fields of modal window using php. I have read that the best option is call with ajax. I do not know anything about ajax.

The idea is:

1) The user click in edit button.
2) Jquery launch on show modal window.
3) Query using php to get all of fields of the a record. (POST code value to use in WHERE)
4) Add this values in input fields of the modal window.


$('#edit').on('', function (event) {

url: 'queryProduct.php?codigo=value',
method: 'POST'
response = JSON.parse(response);


The first step would be change codigo=value by codigo=variable. After that the second step would be recovery the query values of the script.php to assign in the modal-window, I do not know it.

Code php:


if(isset($_SESSION['username']) and $_SESSION['username'] <> ''){


$conn = Conectarse("localhost", "5432", "dbname", "dbuser", "dbpass");

$codigo = $_POST['codigo'];

$query = "SELECT pagina, edicion, descripcion_esp FROM produccion.ma_producto WHERE codigo={$codigo}";

$result = pg_query($conn, $query);

if ($result == TRUE) {
echo json_encode($result);
} else {
echo "Error query: " . $conn->error;

} else{
?><p>Session inactive</a></p>

Now How do it to get back the values of the query of the queryProduct.php?

@Zakaria Acharki knows to get values using jquery but it would like better option to use ajax and launch a query.

Thanks masters!

Answer Source

There are a few ways to catch the output of the AJAX call in jQuery. My favorite looks like this:

    url: 'queryProduct.php',
    data: {codigo: "some value or variable here"}, // will be available on the server as $_POST['codigo']
    method: "POST" 

This will put whatever is outputted by the PHP script into the input and then open a modal.