Marco
Marco

Reputation: 2737

serializeArray not sending the data

This is a sample of the php and javascript.

<form id="image-comment" method="post" action="includes/insert_image_comment.php">
    <textarea id="comment-area" name="comment-area"></textarea>
</form>


// javascript
$("#image-comment").submit(function(event) {
    event.preventDefault();
    var action_url = event.currentTarget.action;
    var id = 4;
    var params = $("#image-comment").serializeArray();
    params.push({imageid: id});

    $.ajax({
        url: action_url,
        type: 'post',
        data: params,
        success: function(data) {
           alert(data);
        }
    });
});


// insert_image_comment.php
echo $get_image = $_POST['imageid'];
$comment = $_POST['comment-area'];

When echoing $_POST['imageid'] i get an error 'Undefined index: imageid'. When echoing $_POST['comment-area'] it's ok.

Why one works and the other not?

Thanks

Upvotes: 1

Views: 252

Answers (1)

Kevin
Kevin

Reputation: 41885

Try to use the format that serializeArray uses: Example:

$("#image-comment").submit(function(event) {
    event.preventDefault();
    var id = 4;
    var params = $("#image-comment").serializeArray();
    params.push({name: 'imageid', value: id}); // this one

    $.ajax({
        url: document.URL,
        type: 'POST',
        data: params,
        success: function(data) {
           alert(data);
        }
    });
});

Upvotes: 1

Related Questions