Home ajax not adding data to database
Reply: 1

ajax not adding data to database

Smokey
1#
Smokey Published in 2018-01-12 20:57:54Z

I'm trying to send the array selectedItems to my database using ajax but for some reason this isn't working for me.

Every time I click on the button that calls to deposit() I first get alert:

"Thank you, We will send you a trade request as soon as possible!"

but then i get the alert:

"something went wrong, please try again."

because $_POST["setDeposit"] isn't set.

Javascript:

function deposit() {
    var selectedItems = ["Banana", "Orange", "Apple"];
    if (selectedItems.length !== 0) {
        $.ajax({
            type: "POST",
            url: 'http://csgodonut.com/home/depositdone',
            data:{setDeposit:selectedItems},
            success:function() {
                alert("Thank you, We will send you a trade request as soon as possible!");
            }
        });
    } else {
        alert("Please select atleast one item.");
    }
}

Controller:

public function depositdone() {
    if(isset($_POST['setDeposit'])) {
        $array = $_POST['setDeposit'];
        $depositItems = implode("<>", $array);
        $steamid = $steamprofile["steamid"];

        $tradeurl = $this->home_model->get_trade_url($steamid);

        if ($tradeurl !== NULL) {
            $this->home_model->add_deposit($tradeurl, $steamid, $depositItems);
            header("Location:/home");
        } else {
            echo '<script>alert("You don\'t have a Tradelink set, please click on your name to set it!");</script>';
            header("Location:/home");
        }
    } else {
        echo "<script>
        if (confirm('something went wrong, please try again.')) {
            window.location.replace('http://csgodonut.com/home');
        } else {
            window.location.replace('http://csgodonut.com/home');
        }
        </script>";
    }
}
Ctznkane525
2#
Ctznkane525 Reply to 2018-01-12 21:02:27Z

You are not returning anything on failure.

the ajax function as an error function that is called.

Please look at the adjusted sample. There's no way to troubleshoot an error if you mask it.

I also recommend some logging on the server side that will help diagnose problems.

$.ajax({
            type: "POST",
            url: 'http://csgodonut.com/home/depositdone',
            data:{setDeposit:selectedItems},
            success:function() {
                alert("Thank you, We will send you a trade request as soon as possible!");
            },
      error: function (xhr, ajaxOptions, theError) {
        alert(xhr.status);
        alert(theError);
      }
        });
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.34271 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO