-1
$(document).ready(function(){
    $('.button').click(function(){
        var clickBtnValue = $(this).val();
        var ajaxurl = 'functions/delivered.php',
        data =  {'action': clickBtnValue};
        $.post(ajaxurl, data, function (response) {
            // Response div goes here.
            alert("action performed successfully");
        });
    });
});

This is the ajax code and I am trying to update my database

functions/delivered.php:

<?php
$db=new mysqli("localhost","root","","restaurant")
if(isset($_POST['onum'])){
    $onum=$_POST['onum'];
    $query="UPDATE `revenue` SET `payment`='y' WHERE onum=$onum";
    $db->query($query);
}
?>
Xen
  • 89
  • 1
  • 8
  • Did you checked that view reach at delivered.php or not ? – Forge Web Design Jan 16 '20 at 17:56
  • Please describe what's not working. What are you expecting to happen? What actually happened? What error messages are you getting? Please see [**How do I ask a good question?**](https://stackoverflow.com/help/how-to-ask) and [**What topics can I ask about here?**](https://stackoverflow.com/help/on-topic). – Alex Howansky Jan 16 '20 at 17:58
  • 3
    Your post request only has one key on it, `action`. There is no `onum` key included. – Taplar Jan 16 '20 at 17:58
  • 1
    Also please note that your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. Instead of building queries with string concatenation, always use [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) with [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). See [**this page**](https://phptherightway.com/#databases) and [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) for some good examples. – Alex Howansky Jan 16 '20 at 17:58
  • 2
    `$db=new mysqli("localhost","root","","restaurant")` < you didn't close that off with a `;` and should have thrown you a parse error notice for it. – Funk Forty Niner Jan 16 '20 at 18:03
  • didn't throw a parse error, fixed it but still doesn't work – Xen Jan 18 '20 at 07:18

1 Answers1

2

You're not sending an onum parameter in the AJAX call.

$(document).ready(function(){
    $('.button').click(function(){
        var clickBtnValue = $(this).val();
        var onumValue = $("#onum").val();
        var ajaxurl = 'functions/delivered.php',
        data =  {'action': clickBtnValue, onum: onumValue};
        $.post(ajaxurl, data, function (response) {
            // Response div goes here.
            alert("action performed successfully");
        });
    });
});

Replace $("#onum") with the actual selector for the input that contains the value you want to send to PHP.

Barmar
  • 741,623
  • 53
  • 500
  • 612