ruzD ruzD - 1 month ago 5
Ajax Question

JQUERY & PHP POST - show 00 in console.log

I make this jquery to call a php file via post. I put a console.log to see the return of the Ajax. At moment return 00.

I'm not sure what it is the problem?

The code is:

$('input[type="submit"]').click(function(event){
event.preventDefault();

// Get the value of the input fields
var inputvalue = $(this).attr("value");

$.ajax({
url:"updateEstado2.php",
type:"POST",
data:{"codigo": inputvalue},
dataType:"text",
success:function(data){
console.log(data);
alert(inputvalue);
}
});
});


The PHP code:

<?php
session_start();

if(isset($_SESSION['username']) and $_SESSION['username'] != ''){

include("db_tools.php");

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


$estado = $_POST["estado"];

$codigo = $_POST["codigo"];

$query = "UPDATE produccion.ma_producto SET estado={$estado} WHERE codigo={$codigo}";

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

if ($result == TRUE) {
header('Location: produccio.php');
} else {
echo "Error updating record: " . pg_last_error($conn);
}

pg_close($conn);

} else{
?><p>La sessió no està activa, si us plau ingresa <a href="login.php">aquí</a></p>




The alert window show the value of the variable correctly but the console.log show 0. I do not understand well...

Please Could you help me.

Answer

Please edit statement after if condition.

            if ($result == TRUE) {
                echo 'Done';
            } else {
                echo "Error updating record: " . pg_last_error($conn);
            }