Halfpint
Halfpint

Reputation: 4079

PHP script only posting to db after first submission

I built my own custom CMS back end to one of the websites I have been working and have been experiencing some strange behavior with the functionality of my site.

The admin is first presented with a form that has two sections to fill out "Banner" and "Section 1", he/she is then able to click a button off to the side of the screen to append new sections to the page, this is handled via some jQuery to append the new element to the document.

The script till here behaves as usual, all sections are able to be filled out, but on submission a tonne of errors are thrown by the script. If the info is inputted again, the form submits as expected and all the correct information is submitted to the database. What I want to know is, is jQuery conflicting with the way my DOM is behaving upon submission or have I made a mistake in my PHP script? I have been stuck with this the past few days, any suggestions would be much appreciated.

Here we check which view to render

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

Markup

getsection_view.php (default view)

<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>

Finally upon submission this script runs to input the data into the database

  # 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;   
    }

}
}

As I stated earlier, the code here works, but only once getsection_view_return.php is called, it does not work with the getsection_view.php script, if anyone has any ideas as to why I am experiencing this behavior I would be most grateful of any pointers.

If anymore code information is required please request and I will include it in an edit.

NOTE: If anybody needs the full markup please check edit log, this is minified.

Upvotes: 1

Views: 96

Answers (1)

Fabien TheSolution
Fabien TheSolution

Reputation: 5050

I don't know if this will fix your main problem but I noticed a possible "bug" in your code like using $_POST['submit'] instead of $_POST['submission'] and also a suggestion to replace the $title+$i, $body+$i thing that sounds weird for me :

...
    $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>
...

Upvotes: 1

Related Questions