ruzD ruzD - 10 months ago 50
Ajax Question

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('', 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;


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

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




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


$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;


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

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:

enter image description here

enter image description here

enter image description here

enter image description here

Answer Source

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);