Javascript PHP错误警报,不支持';t重定向,但保持在同一页上

Javascript PHP错误警报,不支持';t重定向,但保持在同一页上,javascript,php,alert,Javascript,Php,Alert,我正在尝试在表单提交到数据库之前进行一些表单验证。如果所有字段都没有值,但当前提交时,表单重定向到同一页面并清除表单中的所有内容,我希望显示错误消息。这是我的密码 $error = ''; if (isset($_SESSION['logged_in'])) { if (isset($_POST['submit'])) { if (isset($_POST['title'], $_POST['content'], $_POST['short_desc'], $_POST[

我正在尝试在表单提交到数据库之前进行一些表单验证。如果所有字段都没有值,但当前提交时,表单重定向到同一页面并清除表单中的所有内容,我希望显示错误消息。这是我的密码

$error = '';
if (isset($_SESSION['logged_in'])) {
    if (isset($_POST['submit'])) {
        if (isset($_POST['title'], $_POST['content'], $_POST['short_desc'], $_POST['file'])) {
            $title = $_POST['title'];
            $content = $_POST['content'];
            $short_desc = $_POST['short_desc'];

            $targetDir = "../images/blog-images/";
        $fileName = basename($_FILES["file"]["name"]);
        $targetFilePath = $targetDir . $fileName;
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

        $allowTypes = array('jpg','png','jpeg','gif','pdf');
            if(in_array($fileType, $allowTypes)){
                move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath);
            }

        //if (empty($title) or empty($content)) {
        //  $error = 'All fields are required!';
        //  echo "<script type='text/javascript'>alert('$error');</script>";
        //} else {
            $query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_timestamp, article_short_desc, article_image) VALUES (?,?,?,?,?)');

            $query->bindValue(1, $title);
            $query->bindValue(2, $content);
            $query->bindValue(3, time());
            $query->bindValue(4, $short_desc);
            $query->bindValue(5, $fileName);

            $query->execute();
            header('Location: add-new-post.php');
        }else {

        $error = 'All fields are required!';
        echo "<script type='text/javascript'>alert('$error');</script>";
    }

    } 
$error='';
如果(isset($_会话['logged_in'])){
如果(isset($_POST['submit'])){
如果(isset($\u POST['title'],$\u POST['content'],$\u POST['short\u desc'],$\u POST['file'])){
$title=$_POST['title'];
$content=$_POST['content'];
$short_desc=$_POST['short_desc'];
$targetDir=“../images/blog images/”;
$fileName=basename($_文件[“文件”][“名称]);
$targetFilePath=$targetDir.$fileName;
$fileType=pathinfo($targetFilePath,pathinfo_扩展名);
$allowTypes=array('jpg'、'png'、'jpeg'、'gif'、'pdf');
if(在数组中($fileType,$allowTypes)){
移动上传的文件($文件[“文件”][“tmp文件名”],$targetFilePath);
}
//if(空($title)或空($content)){
//$error='所有字段都是必需的!';
//回显“警报('$error');”;
//}否则{
$query=$pdo->prepare('插入到文章(文章标题、文章内容、文章时间戳、文章简短描述、文章图像)值(?,,,,));
$query->bindValue(1,$title);
$query->bindValue(2$content);
$query->bindValue(3,time());
$query->bindValue(4$short\u desc);
$query->bindValue(5,$fileName);
$query->execute();
标题('Location:addnewpost.php');
}否则{
$error='所有字段都是必需的!';
回显“警报('$error');”;
}
} 
由于我对AJAX不太熟悉,有没有一种方法可以在没有AJAX的情况下做到这一点


提前感谢!

由于您的PHP脚本与HTML表单位于同一页面上,您可以执行以下操作:

<input type="text" name="someField" value="<?php echo isset($_POST['someField']) 
? $_POST['someField'] : '' ?>" required />

您可以只需要输入字段。然后会出现一条消息,说明必须填写空白字段并添加表单HTML代码以获得理解。