首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP脚本只在第一次提交后发布到db

PHP脚本只在第一次提交后发布到db
EN

Stack Overflow用户
提问于 2013-09-09 00:03:10
回答 1查看 91关注 0票数 1

我建立了我自己的自定义CMS后端的一个网站,我一直在工作,并经历了一些奇怪的行为与我的网站的功能。

管理员首先会收到一个表单,其中有两个部分来填写"Banner“和”Sect1“,然后他/她可以单击屏幕一侧的一个按钮,将新的部分添加到页面中,通过一些jQuery将新元素附加到文档中。

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

在这里,我们检查要呈现的视图

代码语言:javascript
复制
    <?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 (默认视图)

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
  # 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脚本一起工作,如果有人对我为什么会遇到这种行为有任何想法的话,我将非常感激任何指针。

如果需要更多的代码信息,请请求,我将把它包含在编辑中。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-09 04:38:56

我不知道这是否会解决您的主要问题,但我注意到您的代码中有一个可能的"bug“,比如使用$_POST['submit']而不是$_POST['submission'],还建议替换听起来很奇怪的$title+$i$body+$i之类的东西:

代码语言:javascript
复制
...
    $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>
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18689870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档