• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

session variables within expandable/collapseable sections

Hi guys,

I have some code (shown below) that allows a user to add as many products as they wish (if available - $product_available) by selecting the checkbox at the bottom of each section. I would like to use this for collecting data from the user. Looking at the post variables that are sent to test5a.php, I have a seperate variable and its associated value for each field - great! I am thinking of storing these values into session variables... but I am not sure how to use a similar loop as with the creation of the fields for re-display / confirmation purposes of the data collected from the user so that they can make any changes to the data if necessary?

Thank you for your help with this. Please get back to me if something is not clear.

<html>
<head>
<script type="text/javascript">

var cnt = 0;
var field = 4;

var TABLE_START = "<table width='367' border='0' cellspacing='0' cellpadding='0'>";
var TABLE_END = "</table>";
var TR_START = "<tr>";
var TR_END = "</tr>";
var INFO_TD = "<td class='main'>&nbsp;&nbsp;";
var INPUT_TD = "<td class='main'><input type='text'";
var CHECKBOX_FLD_START = "<input id='createCheck";
var CHECKBOX_FLD_END = "' type='checkbox' value='' onclick='createFields(this);'>";
var DIV_START_ID_START = "<div id='appendDiv";
var DIV_START_ID_END = "'>";
var DIV_END = "</div>";

function init(){
    // document.account_edit.doc_copy.onclick();
} // end function init

function onDocSelect(frm, tp) {
      frm.className = frm.className.replace(/Doc_[a-z]+/i,''); //clear ordertype from classname
      frm.className += ' Doc_' + tp; //set net doctype
}

function createFields(elem)
{
     var ar = new Array();
     var cur = elem.id.split("createCheck")[1];
     var temp = parseInt(cur) + 1;
     var appendDivElem = document.getElementById("appendDiv" + cur);
     var test = document.getElementById("appendDiv" + temp);
     if (elem.checked && cur <= cnt && (test == null || test == "undefined"))
     {

               ++cnt;
               ar.push(TABLE_START);
               for (i = 1; i < 5; i++)
               {
                         ++field;                    
                         ar.push(TR_START);
                    ar.push(INFO_TD);
                    ar.push("Info " + i + ":</td>");
                    ar.push(INPUT_TD);
                    ar.push(" name='info_" + field + "' id='info_" + field + "'></td>");
                    ar.push(TR_END);

               }
               ar.push(TABLE_END);
               ar.push(CHECKBOX_FLD_START);
               ar.push(cnt);
               ar.push(CHECKBOX_FLD_END);

               var newDiv = document.createElement("DIV");
               newDiv.id = "appendDiv" + cnt;
               newDiv.innerHTML = ar.join("");

               appendDivElem.appendChild(newDiv);
     }
     else
     {
          appendDivElem.removeChild(test);
          cnt = cur;
     }
}

</script>
<style>
.onDoc {display:none;}
form.Doc_on                        .onDoc               {display:block;}
form.Doc_off                        .onDoc               {display:none;}
</style>
</head>

<body onLoad="init()">
<FORM action="test5a.php" method="post">

     <table width="398" border="0" cellpadding="0" cellspacing="0">

             <?php // remove query
             $product_available = 1;
             ?>
       <tr>
             <td class="main"><img src="/images/1ptrans.gif" width="1" height="2"><br>&nbsp;
          <?php if ($_SESSION['doc_copy'] == '1') { ?>
                                    <input type="checkbox" name="doc_copy" value="1" checked="checked"
                                onclick="onDocSelect(this.form, this.checked ? 'on' : 'off');"/>
                    <?php } else { ?>
                <?php if ($product_available == 1) { ?>
                                    <input type="checkbox" name="doc_copy" value="1"
                                onclick="onDocSelect(this.form, this.checked ? 'on' : 'off');"/>
                          <?php } else { ?>
                                    <input type="checkbox" name="doc_copy" value="1" disabled />
                          <?php } ?>
                    <?php } ?>
                    <b>Product X</b>                                        </td>
       </tr>
       <tr>
          <td class="main">
               <div class="onDoc" align="center">
               <div id="appendDiv0">
               <table width="367" border="0" cellspacing="0" cellpadding="0">
                 <tr>
                    <td class="main">&nbsp;&nbsp;Info 1:</td>
                    <td class="main"><input type="text" name="info_1" id="info_1"></td>
                 </tr>
                 <tr>
                    <td class="main">&nbsp;&nbsp;Info 2:</td>
                    <td class="main"><input type="text" name="info_2" id="info_2"></td>
                 </tr>
                 <tr>
                    <td class="main">&nbsp;&nbsp;Info 3:</td>
                    <td class="main"><input type="text" name="info_3" id="info_3"></td>
                 </tr>
                 <tr>
                    <td class="main">&nbsp;&nbsp;Info 4:</td>
                    <td class="main"><input type="text" name="info_4" id="info_4" ></td>
                 </tr>
               </table>
             <input id="createCheck0" type="checkbox" value="" onclick="createFields(this);">
             </div>
               </div>
          </td>
       </tr>
     </table>
<input name="" type="submit" value="submit">
</form>

</body>
</html>
0
JasonDell
Asked:
JasonDell
1 Solution
 
Joseph MelnickCommented:
Hello

Your javascript could be simplified to build a new input and place it into the dom inside your form to represent values clicked.
It could be as simple as a string
1|2|4|22|23|4|2

the script that your are posting to could intercept and break it up into something meaningful when processing the $_POST array.

Joseph Melnick
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now