ruzD
ruzD

Reputation: 555

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.

Code:

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

            $.ajax({ 
                url: 'queryProduct.php?codigo=value',
                method: 'POST' 
            }).done(function(response){
                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:

<?php   
        session_start();

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

                include("functions.php"); 
                include("tools.php"); 

                $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>
<?php   
        }?>

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!

Upvotes: 1

Views: 84

Answers (1)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

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

$.ajax({ 
    url: 'queryProduct.php',
    data: {codigo: "some value or variable here"}, // will be available on the server as $_POST['codigo']
    method: "POST" 
}).done(function(response){
    $("#myInput").val(response);
    $("#myModal").modal('show');
});

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

Upvotes: 1

Related Questions