我使用以下AJAX来调用PHP函数:
$.ajax({
url : 'php/jstranslator.php',
type : 'post',
data : {'action' : 'campaignnamecheck',
'newcampaignname' : campaignname,
'oldcampaignname' : oldcampaignname },
success : function(result) {
check = result;
console.log("campaignnamecheck: " + check);
},
error : function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
接收此电话的php如下所示:
if ($_POST['action'] == "campaignnamecheck") {
$newcampaignname = $_POST['newcampaignname'];
$oldcampaignname = $_POST['oldcampaignname'];
campaignnamecheck($newcampaignname,
$oldcampaignname);
}
campaignnamecheck函数如下所示:
function campaignnamecheck($newcampaignname, $oldcampaignname) {
$conn = conn();
echo "php oldcampaignname: " . $oldcampaignname;
// Select any campaigns
$campaignsql = "SELECT CampaignName " .
"FROM campaignstest " .
"WHERE CampaignName = '" . $newcampaignname . "' ";
if(!$campaignresult = $conn->query($campaignsql)){
die('There was an error running the query "' . $campaignsql . '" [' . $conn->error . ']');
}
// If the campaign name remained the same as before it is a safe campaign name
if ($newcampaignname == $oldcampaignname) {
} else {
// If the campaign name changed, or if it's a new campaign, then check to see if the name already exists in the database
if ($campaignresult->num_rows == 0) {
echo "true";
} else {
echo "false";
}
}
}
我在apache错误日志中收到的错误如下:
[Wed Mar 07 16:30:00.330318 2018] [:error] PHP Warning: Missing argument 2 for campaignnamecheck(), called in /var/www/html/test/php/jstranslator.php on line 110 and defined in /var/www/html/test/php/campaignsfuncs.php on line 747
[Wed Mar 07 16:30:00.330779 2018] [:error] PHP Notice: Undefined variable: oldcampaignname in /var/www/html/test/php/campaignsfuncs.php on line 749
当我在chrome上查看控制台的网络选项卡时,我可以看到您设置的值:
action:campaignnamecheck
newcampaignname:test
oldcampaignname:none
我到底错过了什么?
答案 0 :(得分:0)
我不知道为什么这解决了我的问题。但是为了调试我添加了一行:
if ($_POST['action'] == "campaignnamecheck") {
$newcampaignname = $_POST['newcampaignname'];
$oldcampaignname = $_POST['oldcampaignname'];
var_dump($oldcampaignname);
campaignnamecheck($newcampaignname,
$oldcampaignname);
}
现在它似乎正在发挥作用。
答案 1 :(得分:-1)
所以我觉得很明显。不知何故,你没有将第二个参数传递给campaignnamecheck()
。表示您尚未从$_POST
收到。
if ($_POST['action'] == "campaignnamecheck") {
$newcampaignname = $_POST['newcampaignname'];
$oldcampaignname = $_POST['oldcampaignname'];
if(!empty($newcampaignname) && !empty($oldcampaignname)) {
campaignnamecheck($newcampaignname, $oldcampaignname);
}
}
首先,当你编写代码时,你需要检查值是否适合ifs或try catch语句。
并且调试代码也是必不可少的技能