I need to update row in database by id using Ajax.
My Form:
<form id="update_ajax">
<input type="text" name="name" class="test_hide">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<input type="submit" value="<?php echo $row['id']; ?>" class="pure-button">
</form>
My Ajax function:
$("#update_ajax").on("submit", function () {
var id = $(this).attr('id');
var name = $(this).attr('name');
var dataall={'id' : id, 'name' : name};
$.ajax({
type: "post",
url: "update.php",
data: dataall,
success: function (data) {
alert(data);
}
});
});
And my php file is:
if ((isset($_GET['id'])) && (isset($_GET['name']))) {
$id = $_GET['id'];
$name = $_GET['name'];
}else{
die("Not set");
}
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "8169x5it");
$query = $pdo->prepare("UPDATE test SET name=:name WHERE id=:id");
$query->bindParam(':name', $name);
$query->bindParam(':id', $id);
$query->execute();
And I have the error that my name value is not set in $_GET['name']. And I can't understand why it's not set. Because I send it in data.
var id = $(this).attr('id');that's not correct see docs$(this).attr('name')will be undefined, because the form doesn't have anameattribute. You want$("input[name=name]").val().