Erik
Erik

Reputation: 2530

can't call PHP script

I am trying to run/call a PHP file that is going to update a database.

Chrome is giving me this error: enter image description here

I'm using this code for my index.html file:

<!DOCTYPE html>
<html>
<body>
    <input type="text" id="descriptioninput">
    <input type="number" id="budgetin">
    <input type="number" id="budgetout">
    <button type="button" onclick="addToDB()">Add to database</button>

    <script>
        function addToDB()
        {
            var descriptioninput = document.getElementById('descriptioninput').value;
            var budgetin = document.getElementById('budgetin').value;
            var budgetout = document.getElementById('budgetout').value;

            $.ajax ( {
                TYPE: 'POST',
                url: 'addtodb.php',
                data:{descriptioninput:descriptioninput, budgetin:budgetin, budgetout:budgetout},
                success:function (data) {
                    // Completed successfully
                    alert('success!');
                }
            });
        }
    </script>
</body>
</html>

What am I doing wrong here? I've tried to place the code in the <head> element and specify type="text/javascript"

Upvotes: 0

Views: 140

Answers (3)

Artem Chernov
Artem Chernov

Reputation: 906

This function $.ajax() belongs to to Javascript library called jQuery, and you should include this library in your head block.

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

Upvotes: 1

Ahmad
Ahmad

Reputation: 12707

Add a reference to jQuery then remeber: javascript is case sensitive. Change TYPE to type

$.ajax ({
    type: 'POST',
    url: 'addtodb.php',
    data:{descriptioninput:descriptioninput, budgetin:budgetin, budgetout:budgetout},
    success:function (data) {
        // Completed successfully
        alert('success!');
    }
});

Upvotes: 1

Tushar Gupta
Tushar Gupta

Reputation: 15913

You forgot to reference the jquery in your code. Either add it locally or use CDN :

https://code.jquery.com/jquery-1.11.3.min.js

Upvotes: 1

Related Questions