1

So, I've been practicing with PDO, see my earlier asked question and now I am stuck at the following point:

I want to update the database variables without pushing on a button, and in my opinion is it best doable via AJAX.


A few codes: General.JS

var timeoutId;
$('form input').on('input propertychange change', function() {
    console.log('Invoer bewerking');

    clearTimeout(timeoutId);
    timeoutId = setTimeout(function() {
        // Runs 1 second (1000 ms) after the last change
        saveToDB();
    }, 1000);
});

function saveToDB()
{
    console.log('Opslaan naar Database');
    form = $('.formulier24');
    $.ajax({
        url: "ajax2.php",
        type: "POST",
        data: form.serialize(), // serializes the form's elements.
        beforeSend: function(xhr) {
            // Let them know we are saving
            $('.HowAbout').html('Opslaan...');
        },
        success: function(data) { console.error(data) ;
            var jqObj = jQuery(data); // You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
            // Now show them we saved and when we did
            var d = new Date();
            $('.HowAbout').html('Opgeslagen om: ' + d.toLocaleTimeString());
        },
    });
}

// This is just so we don't go anywhere
// and still save if you submit the form
$('.formulier24').submit(function(e) {
    saveToDB();
    e.preventDefault();
});

ajax.php FILE:

<?php
include('verbinding.php');
if(isset($_POST['formulier24'])) {
    $sql = "UPDATE INTO evenement SET username = :username, hours = :hours";
    $parameters = array($_POST["username"], $_POST["hours"]);
    try {
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':username', $_POST['username']);
        $stmt->bindParam(':hours', $_POST['hours']);
        $result = $stmt->execute($parameters);
        $return = "Updated data successfully!";
    } catch (PDOException $e) {
        $return = "Could not update data! Error: " . $e->getMessage();
    }

    header("Content-Type: application/json");
    echo json_encode($return);
}
?>

The connection file (verbinding.php) is 100% working. I think I made a fault in the ajax.php file but I dont know where. Please let me know what I did wrong, because I do not get the Saved at time date-mention and it does not save in the database. Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Willem-Jelle
  • 25
  • 1
  • 8

1 Answers1

0

Your PHP code seems to be expecting a POST variable that represents the form, but that's not what jQuery serialize produces. It produces a POST string containing the names of all the (successful) controls and their values, so PHP will receive a POST parameter per control.

EDIT: To clarify, what I mean is that your isset check on $_POST['formulier24'] will always return false if formulier24 is the name of the form.

GertG
  • 959
  • 1
  • 8
  • 21
  • But what do I have to put over there in stead of `$_POST['formulier24']`. Also, when I put there something else than the **Saved at time/date** won't show up. – Willem-Jelle Mar 15 '17 at 12:28
  • I can't write your code for you. Start by looking at what you're actually sending as POST variables, e.g. by doing a `console.log` of `form.serialize()`, so that you understand what you're sending and what the PHP script can work with. – GertG Mar 15 '17 at 13:13