0

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.

2
  • 2
    var id = $(this).attr('id'); that's not correct see docs Commented Aug 25, 2016 at 21:16
  • 1
    $(this).attr('name') will be undefined, because the form doesn't have a name attribute. You want $("input[name=name]").val(). Commented Aug 25, 2016 at 21:17

2 Answers 2

2

This is wrong:

var id = $(this).attr('id');
var name = $(this).attr('name');

$(this) is the form, so $(this).attr('id') is "update_ajax", not the value of the hidden input. And $(this).attr('name') is undefined because the form doesn't have a name=something attribute. What you really want is:

var id = $(this).find("input[name=id]").val();
var name = $(this).find("input[name=name]").val();

But you can simplify it all to:

var dataall = $(this).serialize();

serialize() will find all the inputs in the form and return a URL-encoded string containing all their values.

Finally, you either have to change the jQuery to use type: 'GET', or change the PHP to use $_POST instead of $_GET.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! Now I understand.
Why When I press update it's one time updates without reloading page and second time it not work without reloading? And I need to press F5 to update data.
It sometimes work and some time not. What it could be?
The script should echo data that can be used to update the page, and the success: function should use it.
The whole point of AJAX is that the page isn't reloaded. You have to write Javascript that makes the changes you want to the page.
|
0

What you have, http://api.jquery.com/attr/

What you wanted http://api.jquery.com/val/

Give that a shot, it should work doesn't look like any other errors in your source. If not let us know.

EDIT:

Also you change:

$(this).attr('id');
$(this).attr('name');

To

$("input[name=id]").val();
$("input[name=name]").val();

Or

$(this).serialize();

See https://api.jquery.com/serialize/ for the later

1 Comment

he also needs to change the selectors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.