500 (Internal Server Error) with AJAX and PHP

1

I have a form which sends 3 data to the file php guardar_lugar.php and at the moment of pressing the button guardar the error occurs to me. I have read several responses from different forums but I have not been able to solve the problem.

Error

Form

<form class="form-horizontal" action="" method="POST">
                <div class="form-group">
                    <h3 class="col-sm-offset-2 col-sm-8 text-center">                   
                    Formulario de Edición de Lugares</h3>
                </div>
                <input type="hidden" id="idlugar" name="idlugar" value="0">
                <input type="hidden" id="opcion" name="opcion" value="registrar">
                <div class="form-group">
                    <label for="nombre" class="col-sm-2 control-label">Nombre</label>
                    <div class="col-sm-8"><input id="nombre" name="nombre" type="text" class="form-control"  autofocus></div>               
                </div>
                <div class="form-group">
                    <label for="direccion" class="col-sm-2 control-label">Dirección</label>
                    <div class="col-sm-8"><input id="direccion" name="direccion" type="text" class="form-control"></div>
                </div>
                <div class="form-group">
                    <label for="tipo" class="col-sm-2 control-label">Tipo</label>
                    <div class="col-sm-8"><input id="tipo" name="tipo" type="text" class="form-control"></div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-8">
                        <input id="" type="submit" class="btn btn-primary" value="Guardar">
                        <input id="btn_listar" type="button" class="btn btn-primary" value="Listar">
                    </div>
                </div>
            </form>

AJAX

var guardar = function(){
        $("form").on("submit", function(e){
            e.preventDefault();
            var frm = $(this).serialize();
            $.ajax({
                method: "POST",
                url: 'consultas/guardar_lugar.php',
                data: frm
            }).done( function( info ){
            console.log( info );        
            });
        });
    }

PHP

<?php 
error_reporting(0);
include("dbinfo.php");

$idlugar = $_POST["idlugar"];
$opcion = $_POST["opcion"];
$informacion = [];

if ($opcion == "modificar" || $opcion == "registrar") {

    $nombre = $_POST["nombre"];
    $dire = $_POST["direccion"];
    //$desc = $_POST["descripcion"];
    //$corr = $_POST["correo"];
    //$lati = $_POST["latitud"];
    //$longi = $_POST["longitud"];
    /*$urbRu = $_POST["selurbanorural"];
    $sector = $_POST["selsector"];*/
    $tipo = $_POST["tipo"];
}

switch($opcion){
    case 'modificar':
        modificar($nombre,$dire);
        break;
    case 'eliminar':
        eliminar($idlugar,$con);
        break;
}

function modificar($nombre,$dire){

    $query = "UPDATE Lugar 
    SET Nombre='$nombre', Direccion='$dire'
    WHERE Id=$idlugar;";

    $result = mysqli_query($con, $query);
    verificar_resultado($result);
    cerrar($con);
}

function eliminar($idlugar,$con){

    $query = "DELETE FROM Lugar
    WHERE Id=$idlugar;";

    $result = mysqli_query($con, $query);
    verificar_resultado($result);
    cerrar($con);
}

function verificar_resultado($result){
    if(!$result) $informacion["respuesta"] = "Error";
    else $informacion["respuesta"] = "Bien";
}

function cerrar($con){
    mysqli_close($con);
}
?>
    
asked by Daniel Carrillo 23.05.2017 в 16:55
source

2 answers

1

The best thing you can do in these cases is to activate the PHP errors, run the program to force the error and analyze the response of the file with the browser debug console (chrome is the best for my taste).

For this the first thing is to activate the php errors with the following functions:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Then you have to analyze the response of the server in the debug console, for this:

  • Open the debug console on the error page.
  • Execute the AJAX function to force the error.
  • Go to the "Networks" tab.
  • Find the php file you want to analyze (in your case
    save_place.php) and click on it.
  • Click on the tab
    "Response" of said file.
  • If everything went well, you should get the error.

        
    answered by 04.01.2018 в 12:05
    0
  • You should activate the errors to see where the problem is, with error_reporting (0); you turn off all error notifications.
  • Look at the errors.log file on the server, because it is a PHP error and the answer is probably there.
  • Since you use an include, the error may be in the dbinfo.php file, you should also post it.
  • answered by 04.01.2018 в 11:27