Error sending input data to php with Ajax

0

I have the following structure:

<div class="username">
    <label>Usuário: </label>
    <p><?php echo $result[0]['username']; ?></p>
    <a>Editar</a>
    <input name="user" value="<?php echo $result[0]['username']; ?>" type="text">
    <button>Salvar</button>
    <span>Sucesso</span>
</div>

I am sending content from input user to my php :

$('div.username button').click(function() {
    $.post("forms/editprofile.php?edit=username", $("div.username input").val(), function(retorno) {
        if(retorno == "success") {
            $("div.username span").show('250');
        } else {
            $("div.username span").val("Falha").show('250');
        }
    });
});

And in PHP I update my registry with the received value:

<?php
require_once '../libs/minpdo.php';
session_start();
$mnpdo = new MinPDO();
$edit = $_REQUEST['edit'];

if($edit == "username") {
    $user = $_REQUEST['user'];
    try {
        $mnpdo->update("users", "username", $user, "id = {$_SESSION['id']}");
        echo "success";
    } catch (MinPDOException $ex) {
        echo "fail";
    }   
}

The message Success appears, but I go to the database and look at the record, in the field username it is empty, probably the error is either passing the data or receiving:

To pass:

$.post("forms/editprofile.php?edit=username", $("div.username input").val(), function(retorno) ....

To receive:

$user = $_REQUEST['user'];

What would be my mistake?

    
asked by anonymous 31.12.2015 / 02:11

1 answer

0

I used another way described here and here , looks like this:

$.ajax({
    type: "POST",
    url: "forms/editprofile.php?edit=username",
    data: "user=" + $("div.username input").val(),
    success: function(retorno) {
      $("div.username span").show('250');
    },
    error: function() {
      $("div.username span").val("Falha").show('250');
    }
  });

Instead of the line $.post .... . Or also changing the line to:

$.post("forms/editprofile.php?edit=username", "user=" + $("div.username input").val() ....
    
31.12.2015 / 02:23