简体   繁体   中英

jquery & ajax response to get values of the php script

I'm beginner to use jquery and Ajax functions.

I make a website with a table with multiple rows, this rows loaded from database with php. Now when I click to edit button show the info of this fields. To get it, I have read the best way is use ajax.

In this jquery code, the idea is loop every cells and when the cell is codigo exit loop each and set this value at the post method of the ajax function.

My code:

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

            var $button = $(event.relatedTarget) // Button that triggered the modal
            var row = $button.closest("tr"), // edit button is in the same row as data you want to change
            $tds = row.find("td"); // get all table cells in that row
            var cod;

            $.each($tds, function(index,value) {

                var field = $(this).data("field");

                if (field == 'codigo'){
                    cod = $(this).text();
                    return false;   
                }
            });

            alert(cod);

            $.ajax({ 
                method: 'POST',
                dataType: 'json',
                url: 'queryProduct.php',
                data: {codigo: cod}

            }).done(function(response){
                response = JSON.parse(response);

                // Here get the values of the JSON      

            });

            var src_value = $tds.closest("td").find('img').attr('src');         // Get attrib src de img and set to a modal window element
            $('[name="imagen"]').attr("src",src_value);

        });

queryProduct.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'];

                echo $codigo;

                $query = "SELECT * 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;
                }

                $conn->close();

        } else{
            ?><p>La sesión no está activa, por favor ingrese <a href="login.php">aquí</a></p>
<?php   
        }?>

The problem is from the developer console show me an error message in the queryProduct.php I do not sure the problem is sending the code variable to method post :(

Show error message from developer console:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

将您的数据更改为此:

 data: {codigo: cod}. // since your cod is already a variable

The problem was with the pg commands of php.

Normally I used mysql database and in my code was a mysql commands.

Now it's working:

            if (!$result) {
                echo "Error query: " . pg_last_error($conn);
            } else {
                echo json_encode($result);
            }

            pg_close($conn);      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM