Stuart
Stuart

Reputation: 43

Posting data using Ajax

I've been trying to post data using AJAX that will update a field in my database however I am having trouble doing so. Everything seems like it should run fine and I get no errors in the console but I've no idea why my db won't update.

Can someone help me out here please?

AJAX:

function ajaxUpdate() {
        var arr = {var1: name, var2: age};
            $.ajax({
                url: 'ajax/confirm.php',
                type: 'POST',
                data: JSON.stringify(arr),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function(data) {
                    console.log("success");
                }
            });
        }

Confirm.php:

$name=$_POST['var1'];
$age=$_POST['var2'];

if($name == "Stuart") {
    mysqli_query($connection,"UPDATE people SET age='$age'");
}
else if($name == "Peter") {
    mysqli_query($connection,"UPDATE people SET age='$age'");
}

The connection to my database is working as I have $connection setup and went to the page /ajax/confirm.php in my browser and I see "Connection successful" in my console as I defined if successful.

So I am unsure as to why this isn't updating?

Are my values not being posted correctly?

I'm new to AJAX so forgive me if this is something very simple!

Thanks

Upvotes: 1

Views: 113

Answers (2)

Masivuye Cokile
Masivuye Cokile

Reputation: 4772

Maybe this well help.

<script type="text/javascript">

    function ajaxUpdate() {
        var data = $('#formID').serialize();
            $.ajax({
                url: 'ajax/confirm.php',
                type: 'POST',
                data: data,
                dataType: 'json',
                encode : true,
                success: function(data) {

                    if(data == "ok"){
                        console.log("success");
                    }else{

                        console.log(data);
                    }
                }
            });
        }
</script>

confirm.php

<?php

$name = $_POST['name'];
$age  = $_POST['age'];


switch ($name) {
    case 'Stuart':
        $sql  = "UPDATE people SET age = ? WHERE name = ? ";
        $stmt = mysqli_prepare($connection, $sql);
        mysqli_stmt_bind_param($stmt, 'si', $name, $age);
        if (mysqli_stmt_execute($stmt)) {
            echo json_encode('ok');
        } else {

            echo json_encode(mysqli_stmt_error($stmt));
        }
        break;
    case 'Peter':
        $sql  = "UPDATE people SET age = ? WHERE name = ? ";
        $stmt = mysqli_prepare($connection, $sql);
        mysqli_stmt_bind_param($stmt, 'si', $name, $age);
        if (mysqli_stmt_execute($stmt)) {
            echo json_encode('ok');
        } else {

            echo json_encode(mysqli_stmt_error($stmt));
        }
        break;

    default:

        echo json_encode('Unknown name ');
}

Upvotes: 0

mega6382
mega6382

Reputation: 9396

Try the following:

function ajaxUpdate() {
    var arr = {var1: name, var2: age};
        $.ajax({
            url: 'ajax/confirm.php',
            type: 'POST',
            data: arr,
            success: function(data) {
                console.log("success");
            }
        });
}

Instead of converting the object into json string send it as is.

Edit: Also remove dataType and probably contentType too. Your code is at risk of SQL Injection. Look into prepared statements and escaping mysql data.

Upvotes: 1

Related Questions