1

我为我一直在工作的网站之一构建了自己的自定义 CMS 后端,并且在我的网站功能上遇到了一些奇怪的行为。

管理员首先会看到一个包含两个部分的表单来填写“横幅”和“第 1 部分”,然后他/她可以单击屏幕侧面的按钮以将新部分附加到页面,这通过一些 jQuery 处理以将新元素附加到文档中。

到这里的脚本和往常一样,所有部分都可以填写,但是在提交时脚本会抛出大量错误。如果再次输入信息,则表单按预期提交,所有正确信息都提交到数据库。我想知道的是,jQuery 是否与我的 DOM 在提交时的行为方式发生冲突,或者我在 PHP 脚本中犯了错误?过去几天我一直坚持这一点,任何建议将不胜感激。

这里我们检查要渲染哪个视图

    <?php if(!isset($_POST['submission'])){
        include_once("includes/getsection_view.php");
    } else if(isset($_POST['submission'])){
        include_once("includes/getsection_view_return.php");
    }
?>

标记

getsection_view.php(默认视图)

<form method="POST" enctype="multipart/form-data" id="theForm">
    <?php 
        $title = "";
        $body = "";

        if(isset($_POST['submit'])){
             for($i=0; $i <=count($nodes); $i++)
             {
                        if(isset($_POST['sectionTitle'.$i])){ $title+$i = htmlentities($_POST['sectionTitle'.$i]);};
                        if(isset($_POST['sectionContent'.$i])){ $body+$i = htmlentities($_POST['sectionContent'.$i]);};
                }
            }
    ?>
        <div id="sectionInfo">
            <div id="sectionWrap">
                <div id="sectionInner">
                    <label class="inst head">Section 1</label>
                    <input class="textOver" type="text" name="sectionTitle1" value="<?php echo $title; ?>" placeholder="Section Title" onfocus="this.select();">
                    <label class="inst ib">Please enter any text you would like associated with the section.</label>
                    <textarea style="margin-top: 3px" name="sectionContent1" value="<?php echo $body; ?>" onfocus="this.select();" placeholder="Section Description"></textarea>
                    <label class="inst ib">Please upload the image associated with this section, .PNG required (588x514px).</label>
                    <input type="file" style="visibility:hidden;" name="sectionImg1" class="upload" />
                    <input type="button" id="fileStyle" class="fSOver fileStyle" value="Upload Section Image!" />
                </div>
            </div>
        </div>          
        <br>
        <div align="center" class="logBut" id="postBut">
            <input style="width: 942px !important; margin-left: -40px !important" type="submit" name="submission" value="Add pump sections to the database" />
        </div>
    </form>
</div>

最后在提交时,此脚本运行以将数据输入数据库

  # Get the total section node count (not banner)
     $nodes = 0;
     foreach($_POST as $key => $section){ 
    $nodes++;
     }

    // Get the correct total of apended nodes
     $nodes = (($nodes-3)/2)-1; 
     $i = 1;


if(isset($_POST['submission'])){

// Check for errors on the banner section
if(empty($errors) === true){

    // Perform any last checks
    if(isset($_POST['ptype'])){$serial = $_POST['ptype'];};

    // Banner section validation and input was here...

    // Handle each section
    //*********************
    $sSectionId = 1;

    // Validate each section input
    for($i=1; $i<$nodes+1; $i++){
        if(empty($_POST['sectionTitle'.$i]) || empty($_POST['sectionContent'.$i])){
            $errors[] = 'Please fill in all fields at Section ' .$i;
        }   

        // Image validation
        if (!isset($_FILES['sectionImg'.$i])) {
            $errors[] = 'Please upload a file in PNG format at Section'.$i.'.';
        } else if (isset($_FILES['sectionImg'.$i])) {
            $fileName = $_FILES['sectionImg'.$i]['name'];

                $target_path = IMG_DIR . basename($_FILES['sectionImg'.$i]['name']);

                if(move_uploaded_file($_FILES['sectionImg'.$i]['tmp_name'], $target_path)) {

                } else {
                    $errors[] = "There was an error whilst uploading " . $fileName . " please try again.";
                }

        }

        // Initiate variables for PDO insert
        $sImg = "/" . $fileName;
        $sHeader = $_POST['sectionTitle'.$i];
        $sInfo = $_POST['sectionContent'.$i];

        // If the errors are 100% empty then input the new section to DB
        if(empty($errors)===true){
            $users->add_section($sHeader, $sInfo, $sImg, $serial, $sSectionId);
            $success[] = $nodes+1 . " sections have been added to " . $serial . ".";
        }

        else {
            $errors[] = "It seems something went wrong when trying to add your data to the system, please try again.";
        }

        $sSectionId += 1;   
    }

}
}

正如我之前所说,这里的代码有效,但只有在调用 getsection_view_return.php 时,它不适用于 getsection_view.php 脚本,如果有人对我遇到这种行为的原因有任何想法,我将不胜感激指针。

如果需要更多代码信息,请提出要求,我会将其包含在编辑中。

注意:如果有人需要完整的标记,请检查编辑日志,这是缩小的。

4

1 回答 1

1

我不知道这是否会解决您的主要问题,但我注意到您的代码中可能存在“错误”,例如使用$_POST['submit']而不是,$_POST['submission']并且还建议替换$title+$i$body+$i这对我来说听起来很奇怪:

...
    $title1 = "";
    $body1 = "";

    if(isset($_POST['submission'])){
         for($i=0; $i <=count($nodes); $i++)
         {
                    $title = 'title'.$i;
                    if(isset($_POST['sectionTitle'.$i])){ $$title = htmlentities($_POST['sectionTitle'.$i]);};
                    $body = 'body'.$i;    
                    if(isset($_POST['sectionContent'.$i])){ $$body = htmlentities($_POST['sectionContent'.$i]);};
            }
        }
...

                <input class="textOver" type="text" name="sectionTitle1" value="<?php echo $title1; ?>" placeholder="Section Title" onfocus="this.select();">
                <label class="inst ib">Please enter any text you would like associated with the section.</label>
                <textarea style="margin-top: 3px" name="sectionContent1" value="<?php echo $body1; ?>" onfocus="this.select();" placeholder="Section Description"></textarea>
                <label class="inst ib">Please upload the image associated with this section, .PNG required (588x514px).</label>
...
于 2013-09-09T04:38:56.493 回答