Error sending input data to php with Ajax

Asked

Viewed 98 times

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’m uploading the contents of input user for 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 the PHP i update my record with the amount received:

<?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 Success message is displayed, but I go to the database and look at the record, in the field username it is empty, probably the error is either when passing the die or when 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?

1 answer

0


I used another described way here and here, was 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');
    }
  });

In place of the line $.post ..... Or by changing the line to:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.