Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ajax不向数据库添加数据_Php_Ajax - Fatal编程技术网

Php ajax不向数据库添加数据

Php ajax不向数据库添加数据,php,ajax,Php,Ajax,我试图使用ajax将数组selectedItems发送到我的数据库,但由于某些原因,这对我不起作用 每次我单击调用deposit()的按钮时,我首先会收到警报: “谢谢,我们将尽快向您发送交易请求!” 但是我得到了警告: “出现问题,请再试一次。” 因为未设置$\u POST[“setDeposit”] Javascript: 控制器: public函数depositone(){ 如果(isset($_POST['setDeposit'])){ $array=$_POST['setDeposit

我试图使用ajax将数组
selectedItems
发送到我的数据库,但由于某些原因,这对我不起作用

每次我单击调用
deposit()
的按钮时,我首先会收到警报:

“谢谢,我们将尽快向您发送交易请求!”

但是我得到了警告:

“出现问题,请再试一次。”

因为未设置
$\u POST[“setDeposit”]

Javascript:

控制器:

public函数depositone(){
如果(isset($_POST['setDeposit'])){
$array=$_POST['setDeposit'];
$depositItems=内爆(“,$array);
$steamid=$steamprofile[“steamid”];
$tradeurl=$this->home\u model->get\u trade\u url($streamid);
如果($tradeurl!==NULL){
$this->home\u model->add\u deposit($tradeurl、$steamid、$depositems);
标题(“位置:/home”);
}否则{
echo“警报(“您没有贸易通设置,请单击您的姓名设置!”);
标题(“位置:/home”);
}
}否则{
回声“
如果(确认('出现问题,请重试')){
window.location.replace('http://csgodonut.com/home');
}否则{
window.location.replace('http://csgodonut.com/home');
}
";
}
}

出现故障时,您不会返回任何内容

ajax函数作为调用的错误函数

请看调整后的样品。如果你掩盖了错误,就没有办法解决它

我还建议在服务器端进行一些日志记录,以帮助诊断问题

$.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);
      }
        });

到目前为止,您采取了哪些调试步骤?不要返回脚本标记--这太奇怪了。将所需内容存储在某种JSON对象中,例如
{消息:“您不…”,重定向:http://csgodonut.com/home“}
并在成功处理程序中,根据给定的键执行相应的操作。您如何调用
存款()
?听起来您并没有阻止正常的表单提交,所以这是在AJAX请求之后发生的。
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>";
    }
}
$.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);
      }
        });