1

I am trying to update my database through a JS Function. After googling for a while I understood that it is clearly not possible without using AJAX.

So it is my first time and I tried, here is my JS code:

$("#update").click(function(event){
    var showArr = document.forms['ownForm'].elements['showArr'].value;

    $.ajax({
        url: './../pages/updateDatabase.php',
        type: 'POST',
        data: {
            showArr: showArr
        } ,
        success: function( msg ) {
            alert( "Data Saved: " + msg );
        }
    });
    return false;
});

and my PHP file:

<?php

try {
    $conn = new PDO('mysql:host=localhost;dbname=+++', '+++', '');
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE Users SET SHOWARR = true WHERE ID = 1";

    $stmt = $conn->prepare($sql);
    $stmt->execute();

    echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;

?>

But nothing happens! Where is my mistake?

3
  • Of course I removed credentials from the code and I pass a "showArr" variable, but I am not using it right now in the php file because I thought maybe this could be the mistake. In this case I just want to trigger the SQL Statement Commented Aug 6, 2017 at 15:08
  • If you just point your browser at the update page, does it work correctly. It's a case of working out which part is failing - also calling the page directly is easier to show errors and work out how to bug fix it (although it only works if your using not using parameters). Commented Aug 6, 2017 at 15:22
  • Calling the PHP Page directly successfully updates the database! So it seems to be a problem with the AJAX Method I guess Commented Aug 6, 2017 at 15:47

1 Answer 1

1

Your URL in ajax function does not look right:-

give the full URL like this

url:'http://yourdomain.com/page.php';

I hope it works

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

Comments

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.