1

So I'm trying to post/update data in a MySQL table but it seems like the post data is not being received. I need help getting the POST data to output.

users.php

<form id="myForm" method="post" action="" >
    <input hidden name="identifier" id="identifier" value="<?php echo $identifier; ?>" />
    <input type="submit" name="submit" value="Update" id="sub" class="btn btn-info pull-right"/>
</form>

actions/users_manage.php

$db = new PDO('mysql:host=127.0.0.1;dbname=db_name', 'db_user', 'db_pass');
$identifier = htmlspecialchars($_POST['identifier']);

if(isset($_POST['submit'])){
    try {
    $stmt = $db->prepare("UPDATE users SET identifier=:identifier WHERE identifier=:identifier");
    $stmt->execute([':identifier' => $identifier]);
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }
die();
}

actions/users_manage.js

$(document).ready(function(){
$('#myForm').submit(function(){
    var data = $(this).serialize();

    $.ajax({
        url: "actions/users_manage.php",
        type: "POST",
        data: data,
        success: function( data )
        {
            alert( data );
        },
        error: function(){
            alert('Error');
        }
    });

    return false;
    });
});

When I try adding a var_dump($_POST) into the users_manage.php above the if(isset) it displays array(0){}, so the processing is working. It's just not receiving any POST data, please help :) Thanks greatly appreciated!

When I add this to the users_manage.php:

var_dump($_POST['identifier']);

It displays the alert with "NULL".

Harvey Connor
  • 137
  • 3
  • 18

1 Answers1

0

So it turns out, I needed to change the var data = $(this).serialize(); to var data = $(this).serialize(); it manages to work.

Working ajax:

$(document).ready(function(){
$('#myForm').submit(function(){
    data = $(this).serialize();
    // alert(data);
    $.ajax({
        url: "actions/users_manage",
        type: "POST",
        data: data,
        success :   function(){
            alert(data);
        },
        error   :   function(){
            alert('Error');
        }
    });

    return false;
});
});
Harvey Connor
  • 137
  • 3
  • 18