Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.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
Javascript 使用Pjax提交表单时PHP未定义索引_Javascript_Php_Forms_Pjax - Fatal编程技术网

Javascript 使用Pjax提交表单时PHP未定义索引

Javascript 使用Pjax提交表单时PHP未定义索引,javascript,php,forms,pjax,Javascript,Php,Forms,Pjax,我一直在试验Pjax,到目前为止我很喜欢它。它工作得很好,但我遇到了一个问题。使用pjax提交表单(post)时,一切正常,但我在错误日志中看到了“PHP注意事项:未定义索引:名称” 以下是我提交表格的代码: $(document).on('submit', 'form[data-pjax]', function(event) { event.preventDefault(); $.pjax.submit(event, '#containercontainer'); re

我一直在试验Pjax,到目前为止我很喜欢它。它工作得很好,但我遇到了一个问题。使用pjax提交表单(post)时,一切正常,但我在错误日志中看到了“PHP注意事项:未定义索引:名称”

以下是我提交表格的代码:

$(document).on('submit', 'form[data-pjax]', function(event) {
    event.preventDefault();
    $.pjax.submit(event, '#containercontainer');
    return false;
});
我已经包含了所有必需的.js文件,但为了简单起见,这里没有

这是我表格的简略版本:

<form data-pjax method="POST" action="add-process.php" id="addform">
    <div class="row">
        <div class="col-sm-5">
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" placeholder="Name" name="name" autocomplete="off" autofocus>
            </div>
        </div>
        ...
    </div>
    <button id="submit_btn" type="submit" class="btn btn-success btn-block" style="font-weight:bold;">ADD</button>
</form>
这解决了一些问题,但我仍然在日志中发现错误:

PHP Notice:  Undefined variable: reason
PHP Notice:  Undefined variable: userID
再次感谢您的帮助

你得到了什么

PHP Notice:  Undefined variable: reason
PHP Notice:  Undefined variable: userID
因为您的isset($\u POST['userID'])和isset($\u POST['reason'])返回false,因为它们不存在。这就是为什么变量没有定义。您可以添加“默认”值

您可以跟踪表单发送到服务器端的内容。试试这个:

print_r($_POST);


关于表单:检查输入的名称。是否存在名为“userID”和“reason”的输入?

在条件中设置变量时,最好先初始化它们:

$reason = null;
$userID = false;

/* .... your current code starts here   */

PHP抛出这些通知来帮助您避免由打字错误引起的错误(即稍后引用uesrID等)。首先初始化它们可以清楚地表明您实际上使用的是您想要的名称

谢谢大家!!我注释掉了所有的标题并加入了echo语句,让我知道它走了多远,然后试着打印(print_r($_POST);得到了这个:“数组([userID]=>7[reason]=>1[otherreason]=>)完成了!”我认为这很好!你能帮我弄清楚现在该怎么办吗?非常感谢你!!!你到底想说什么?解释一个bitI只是希望能够在php中使用这些作为常规变量。。。例如,将它们用作“echo$userID;”
print_r($_REQUEST);
$reason = null;
$userID = false;

/* .... your current code starts here   */