如果var为空,则需要退出函数,并使ajax显示错误PHP/ ajax


Need to exit a function if var is empty and make ajax display an error PHP/AJAX

好了,我有一个AJAX函数,它将数据发送到.php文件,由.php文件处理数据(就这么简单):

$(document).on('click','#submit',function () {
    var title = $('#title_submit').val();
    var content = $('#content_submit').val();
    var image = $('#image_url').val();
    $.ajax({
        type: "POST",
        url: "../parts/add.php",
        data: {
            title: title,
            content: content,
            image: image
        },
        success: function (data) {
            redirect(); //a function that redirects to the main page
        }
    });
});

内容通过,函数处理它。所以,如果没有图像("image_url"字段是空的),它只是忽略它,只是添加标题和内容…但是如果有一个图像,它检查它的类型(switch ($info) { case 'image/jpg':...)如果没有匹配:

default:
$new_name = "";
break;
}

这是最难的部分(对我来说):

if ($new_name == "") {
...//stop from executing and say "That ain't no image you be sendin'"; 
} else { //continue with adding an image to the folder and than creating a thumbnail of it...

我尝试die(),但如果我在图像URL输入中输入类似"11111111111111111111111111111"的东西,它会在数据库中输入"111111111111111111111111111",并输出它而不是图像。

你检查错误的方式,这就像给$name分配空字符串使用==操作符检查空字符串

if ($new_name == "") {