1

I am having an issue with passing a PHP variable ($itemID in the code below) to a form using a pure CSS modal. The first echo within the IF statement returns the correct value. The $itemID in the form (the second echo within the IF statement) gets "stuck" on a single $itemID.

Specifically, the IF is first entered when $itemID = 8. Each subsequent entry into the IF shows the correct value for $itemID in the first echo (outside of the form) but only shows 8 in the second echo, which is within the form.

I originally used a JQuery modal solution, so I thought my problem was with something in the script, but I'm running into the same issue without using any Javascript.

All of the "Questions that may already have your answer" and "Similar Questions" involve issues with Javascript. (3 of many cited.)

When I comment out the modal DIV tags, $itemID is accurate. What I'm presuming (but not understanding) is that when using the modal the DIV that contains the modal is only loaded the first time the IF statement is entered. If someone can help me understand why, I can try to figure out a solution.

One thing I've tried is using SESSION variables (before and after the form) but ran into the same problem, not suprisingly.

PHP:

while ($row_item = $item_result->fetch_assoc()) {
    $item_name = $row_item['txtDisplayName'];
    $itemID = $row_item['itemID'];
    echo '<div class="col-1"><div class="item">' . $item_name . '<br>';


    //add a file upload form if needed
    if ($row_item['numResponseType']=='1'){

        echo $itemID;

        echo '<a href="#openModal">Upload</a>'; 
        echo '<div id="openModal" class="modalDialog">';
        echo '<div><a href="#close" title="Close" class="close">X</a>';
        echo '<h2>Upload</h2>';
        echo '<form enctype="multipart/form-data" action="upload.php" method="POST" id="modal-form">';

        echo $itemID;

        echo '<input type="hidden" name="itemID" value="' . htmlspecialchars($itemID) . '"></input>';
        echo '<input type="hidden" name="userIDnum" value="' . htmlspecialchars($userIDnum) . '"></input>';
        echo '<input type="hidden" name="groupName" value="' . htmlspecialchars($group_name) . '"></input>';
        echo '<input type="hidden" name="itemName" value="' . htmlspecialchars($item_name) . '"></input>';
        echo '<input type="file" name="uploadFile"></input><br>';
        echo 'Name: <input type="text" name="displayName"></input><br>';
        echo '<input type="submit" name="submit" value="Submit"></input></form><br>';

        echo '</div></div>';

CSS: (credit)

.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 99999;
    opacity:0;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    pointer-events: none;
}
.modalDialog:target {
    opacity:1;
    pointer-events: auto;
}
.modalDialog > div {
    width: 400px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
}
.close {
    background: #606061;
    color: #FFFFFF;
    line-height: 25px;
    position: absolute;
    right: -12px;
    text-align: center;
    top: -10px;
    width: 24px;
    text-decoration: none;
    font-weight: bold;
    -webkit-border-radius: 12px;
    -moz-border-radius: 12px;
    border-radius: 12px;
    -moz-box-shadow: 1px 1px 3px #000;
    -webkit-box-shadow: 1px 1px 3px #000;
    box-shadow: 1px 1px 3px #000;
}
.close:hover {
    background: #00d9ff;
}

Complete Block of Code, Per Request

while ($row_item = $item_result->fetch_assoc()) {
    $item_name = $row_item['txtDisplayName'];
    $itemID = $row_item['itemID'];
    //start the column for this item
    echo '<div class="col-1"><div class="item">' . $item_name . '<br>';


    //add a file upload form if needed
    if ($row_item['numResponseType']=='1'){

        echo $itemID;

        echo '<a href="#openModal">Upload</a>'; 
        echo '<div id="openModal" class="modalDialog">';
        echo '<div><a href="#close" title="Close" class="close">X</a>';
        echo '<h2>Upload</h2>';
        echo '<form enctype="multipart/form-data" action="upload.php" method="POST" id="modal-form">';

        echo $itemID;

        echo '<input type="hidden" name="itemID" value="' . htmlspecialchars($itemID) . '"></input>';
        echo '<input type="hidden" name="userIDnum" value="' . htmlspecialchars($userIDnum) . '"></input>';
        echo '<input type="hidden" name="groupName" value="' . htmlspecialchars($group_name) . '"></input>';
        echo '<input type="hidden" name="itemName" value="' . htmlspecialchars($item_name) . '"></input>';
        echo '<input type="file" name="uploadFile"></input><br>';
        echo 'Name: <input type="text" name="displayName"></input><br>';
        echo '<input type="submit" name="submit" value="Submit"></input></form><br>';

        echo '</div></div>';

        $doc_query = "SELECT docID, resID, itemID, strDocPath, txtDisplayName FROM tblresdocs WHERE itemID='$itemID'";
        if(!$doc_result = $db2->query($doc_query)){
            die('There was an error running the document query [' . $db2->error . ']');
        } 

        while ($row_doc = $doc_result->fetch_assoc()) {
            $doc_id = $row_doc['docID'];        
            $doc_path = $row_doc['strDocPath']; 
            $doc_name = $row_doc['txtDisplayName'];
            echo '<a href="' . $doc_path . '">' . $doc_name . '</a>';
            echo '<form enctype="multipart/form-data" action="deletefile.php" method="POST" style="display: inline;">';
            echo '<input type="hidden" name="strDocPath" value="' . htmlspecialchars($doc_path) . '"></input>';
            echo '<input type="hidden" name="txtDisplayName" value="' . htmlspecialchars($doc_name) . '"></input>';             
            echo '<input type="hidden" name="docID" value="' . htmlspecialchars($doc_id) . '"></input>';
            echo '<input type="submit" name="delete" value="Delete File" onclick="return confirm(\'Are you sure you want to delete ' . $doc_name . '?\');"></input></form><br>';

        } echo '</div></div>';

    }

    //link
    if ($row_item['numResponseType']=='2'){
        echo 'link here';
        echo '</div></div>';
    }

    //scholarly activity or QI project
    if ($row_item['numResponseType']=='3'){
        echo '<form enctype="multipart/form-data" action="addscholar.php" method="POST">';
        echo 'Activity Description: <input type="text" value="description"><br>';
        echo 'Activity Date: <input type="text" class="datepicker"><br>';
        echo '<input type="radio" name="status" value="Complete">Complete</input><br>';
        echo '<input type="radio" name="status" value="In Progress">In Progress</input><br><br>';
        echo '</form></div></div>';
    }

    //datepicker 
    if ($row_item['numResponseType']=='4'){
        echo 'Date: <input type="text" class="datepicker"><br><br>';
        echo '</div></div>';
    }

    //self-assessment
    if ($row_item['numResponseType']=='5'){
        session_start();
        $_SESSION['userIDnum'] = $userIDnum;
        session_write_close();
        echo '<a href="selfassess.php">Self Assessment Form</a>';
        echo '</div></div>';
    }

    //not yet id'd
    if ($row_item['numResponseType']==''){
        echo 'nada';
        echo '</div></div>';
    }

}
Community
  • 1
  • 1
bizco
  • 63
  • 8
  • I think you might have missed including a small amount of your PHP. I do not see where your `if` and `while` statements are being closed. – hungerstar Mar 13 '15 at 16:11
  • have you checked your sourcecode? maybe you dont see it but it should be there. – steven Mar 13 '15 at 16:16
  • @hungerstar: I added the entire `while` statement, which includes the `if`. @steven: I have it `echo` onto the form, and it shows the wrong number. It also inserts into the wrong place in my database (inserts into the `$itemID = 8` row every time). Thanks! – bizco Mar 13 '15 at 16:22
  • Just a thought: the second echo is inside a div with the class modalDialog which has opacity:0 set in CSS. This may be the reason why you don't see it but it won't answer the database problem. – Octav Mar 13 '15 at 16:46
  • @Octav: I'm seeing it on the modal, but it's not the correct value. Thanks for looking! – bizco Mar 13 '15 at 17:32
  • I wanted to clarify that this behavior happens when calling `$itemID` from within the modal `DIV`, not just the `form`. – bizco Mar 13 '15 at 19:54

0 Answers0