0

I have a problem with an application. Notice: Undefined variable: nombre in C:\wamp\www Notice: Undefined variable: puesto in C:\wamp\www Notice: Undefined variable: area in C:\wamp\www Notice: Undefined variable: id in C:\wamp\www

here's the codes:

    <?php
session_start();
header('Content-type: application/json');
//si es una llamada ajax
function is_ajax() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
//Funcion para verifica si el request  en un tipo Ajax rquest
if (is_ajax()) {//si esta seteado el pots   
    if (isset($_POST["nombre"]) && !empty($_POST["nombre"])) 
    { //verificar si nombre existe y tiene valor
        $nombre = $_POST["nombre"];
        if (!preg_match("/^[a-zA-Z ]*$/", $nombre)) 
        {
            echo "Solo letras y espacios estan permitidos para el Nombre"; 
            die;
        }
    }    //set variable puesto
    if (isset($_POST["puesto"]) && !empty($_POST["puesto"])) 
    { //verificar si puesto existe y tiene valor
        $puesto = $_POST["puesto"];
        if (!preg_match("/^[a-zA-Z ]*$/", $puesto)) 
        {
            echo "Solo letras y espacios estan permitidos para el puesto"; 
            die;
        }
    }    //set varia
    if (isset($_POST["area"]) && !empty($_POST["area"])) 
    { //verificar si area y tiene valor
        $area = $_POST["area"];
        if (!preg_match("/^[0-9]*$/", $area)) 
        {
            echo "Error"; 
            die;
        }
    }
    if (isset($_GET["id"]) && !empty($_GET["id"])) 
    { //verificar si area y tiene valor
        $id = $_GET["id"];
        if (!preg_match("/^[0-9]*$/", $id)) 
        {
            echo "Error"; 
            die;
        }
    }
}
require_once 'connection.php';
try     
{
    $sql = "Update tbl_empleados SET Nombre = '$nombre', Puesto = '$puesto', fk_idarea= '$area' where idEmpleados = '$id'";
    // use exec() because no results are returned

    $stmt = $conn->prepare($sql);
    $stmt->execute();

    echo "Empleado Actualizado Satisfactoriamente";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
    die;
}
$conn = null;
?>

JavaScript:

    $(document).ready(function () {
    var urid = getUrlVars()["id"].replace("#", '');
    $('#btn-actualizar').click(function () {//evento click del boton:
        parametros = validarFormulario(document.frmactualizarempleado);
        //Funcion para validar los campos
        if (parametros.Status) {
            //ejecutar Ajax para cargar usuarios URL: altaempleados.php
            parametros.id = urid;
            console.log(parametros);
            $.ajax({
                type: "POST",
                url: "resources/includes/actualizare.php",
                data: parametros,
                success: function () {
                    $('#frmactualizarempleado').html("<div id='message'></div>");
                    $('#message').html("<h2>Los datos han sido guardados correctamente!</h2>")
                        .hide()
                        .fadeIn(1500, function () {
                        $('#message').append("<a href='index.php?action=see'>Ver empleados registrados</a>");
                    });
                }
            });
            return false;
            runOnLoad(function () {
                $("input#name").select().focus();
            });
        }
    });
});

Form:

 <?php require_once 'resources/includes/header.php';
require_once 'resources/includes/connection.php';

try
{   
    $sql = "select idAreas as `Key`, nombre as `Value` from tbl_areas";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    // set the resulting array to associative array
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //fetch query result to array
    $result = $stmt->fetchAll();       
}
catch (Exception $e)
{
    echo "Ocurrio un Error:" . "'".(string)$e->getMessage()."'";
    die();
}
$stmt = null;
?>

<link rel="stylesheet" href="resources/css/tablestyle.css">
<body class="">
    <div class="panel bottom-padding-0">
        <div class="row">
            <div class="large-5 columns"><h3 class="h3">Actualizar Empleados.</h3></div>
            <div class="row">
            </div>
            <div class="large-12 columns">
                <form name="frmactualizarempleado">
                    <div class="row">
                        <div class="large-4 columns">
                            <label>Nombre:</label>
                            <input type="text" name="nombre" placeholder="Nombre" />
                        </div>
                        <div class="large-4 large-rigth columns">
                            <label>Puesto:</label>
                            <input type="text" name="puesto" placeholder="Puesto" />
                        </div>
                        <div class="large-4 large-rigth columns">
                            <label>Area:</label>
                            <select id="areas"  name="area">
                                <option value="" disabled selected>Selecciona tu Area</option>
                                <?php 
                                // Echo $result while row >= EOF 
                                foreach($result as $results) { ?>
                                <option value="<?php echo $results['Key'] ?>"><?php echo $results['Value'] ?></option>
                                <?php }?>
                            </select>
                        </div>
                    </div>
                    <div class="small-3 columns large-centered">
                        <a href="#" id='btn-actualizar' class="button postfix">Actualizar</a>
                    </div>  



                </form>
            </div>        
        </div>
    </div>


</body>
<?php require_once 'resources/includes/footer.php';?>
<script src="resources/js/actualizaremp.js"></script>        
  • Your variables are only defined if the `if` statements are true. Give them a default value outside your if statements. – andrewsi Aug 21 '15 at 16:30
  • How can i do that? i'm a trainee in that way, i don't know many things – Alfredo Lopez Aug 21 '15 at 16:37
  • Just before the if statement where you set `$nombre = $_POST["nombre"];`, add `$nombre = ''`. That will remove the warning you're getting. However. You also need to check before you run your UPDATE statement - I assume you just want to update values that have been checked, and right now you're updating every value. – andrewsi Aug 21 '15 at 16:41
  • What I'd suggest would be to add each value you want to update to an array; then implode that array to generate your SQL statement. That way, you'll skip values that haven't been set, and so no change them. That should be enough to point you in the right direction. – andrewsi Aug 21 '15 at 16:42
  • what do you mean with i'm updating every value? – Alfredo Lopez Aug 21 '15 at 17:03
  • Thanks for adding your code, however that's way too much. Ideally, it should be a [mcve], so please [edit] your question to reduce the amount of debugging we need to do. (You'll often find that you answer your own question while producing a minimal example - consider that a side-benefit!) – Mogsdad Aug 21 '15 at 18:25

0 Answers0