PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Displays a linked set of drop-down lists the user can select. This code is from a page that also contains text boxes. The text boxes (with no associated JavaScript) posts data to the next page fine, but these two, chk_Country and chk_Location do not. I am trying to read $_POST['chk_Country'] and $_POST['chk_Location'] on my next page, but they are turning up blank (empty).
Okay. Well obviously my initial suspicion was the JavaScript, however this has been working previously until I changed the page structure of my site. The JavaScript simply changes the contents of the 'Location' drop-down list when the user changes the Country in the 'Country' drop-down list.
What I don't understand is, am I correctly attempting to attain the POST variables by using the NAME of the SELECT objects? On one hand, those objects without any JavaScript of any kind involved seem to work, but these don't. But then it only seemed to stop working when I change the page layout of my site. Am I correct by trying to attain the VALUE of the OPTIONS in the SELECT object by using the $_POST['name_of_select_object'] form?
the value will be stored under the name of the selects. As for why it's not posting, I'll need to see more code.. specifically the entirity of the form and the two or more javascript functions involved.
function DynamicOptionList_populate() {
var theform = this.form;
var i,j,obj,obj2;
// Get the current value(s) of all select lists this list depends on
this.dependentValues = new Object;
var dependentValuesInitialized = false;
for (i=0; i<this.dependencies.length;i++) {
var sel = theformїthis.dependenciesїi]];
var selName = sel.name;
// If this is the first dependent list, just fill in the dependentValues
if (!dependentValuesInitialized) {
dependentValuesInitialized = true;
for (j=0; j<sel.options.length; j++) {
if (sel.optionsїj].selected) {
this.dependentValuesїsel.optionsїj].value] = true;
}
}
}
// Otherwise, add new options for every existing option
else {
var tmpList = new Object();
var newList = new Object();
for (j=0; j<sel.options.length; j++) {
if (sel.optionsїj].selected) {
tmpListїsel.optionsїj].value] = true;
}
}
for (obj in this.dependentValues) {
for (obj2 in tmpList) {
newListїobj + this.delimiter + obj2] = true;
}
}
this.dependentValues = newList;
}
}
var targetSel = theformїthis.target];
// Store the currently-selected values of the target list to maintain them (in case of multiple select lists)
var targetSelected = new Object();
for (i=0; i<targetSel.options.length; i++) {
if (targetSel.optionsїi].selected) {
targetSelectedїtargetSel.optionsїi].value] = true;
}
}
targetSel.options.length = 0; // Clear all target options
for (i in this.dependentValues) {
if (typeof this.optionsїi] == "object") {
var o = this.optionsїi];
for (j=0; j<o.length; j+=2) {
var text = oїj];
var val = oїj+1];
targetSel.optionsїtargetSel.options.length] = new Option(text, val, false, false);
if (typeof this.defaultValuesїi] != "undefined" && this.defaultValuesїi]!=null) {
for (def in this.defaultValuesїi]) {
if (def == val) {
targetSelectedїval] = true;
}
}
}
}
}
}
targetSel.selectedIndex=-1;
// Select the options that were selected before
for (i=0; i<targetSel.options.length; i++) {
if (targetSelectedїtargetSel.optionsїi].value] != null && targetSelectedїtargetSel.optionsїi].value]==true) {
targetSel.optionsїi].selected = true;
}
}
}
Thanks for looking. Is there anything in the script that might be causing the problem? I am no expert on JavaScript, I am actually using it as a library.
First of all, are your options populating properly when you view the page/source of the originating page? If so, the problem isn't with the javascript.
So if that's not it, then I'd suggest doing this at the top of your receiving page:
Thanks for your input. print_r shows that in fact the $_POST variables are populated, including chk_Country and chk_Location, however they are not being recorded to $_SESSION variables like the other $_POST variables. I am using $_SESSION variables to carry over the post data to another following page, and whilst I have checked for consistency in the variable naming, all but chk_Country and chk_Location are being saved in their respective $_SESSION variables.
I will have to look at the code (yet again) to see if I can spot any problems. Thanks Feyd for looking at the JavaScript, I'll post back the problem soon - it's just past midnight here in the UK so I'll pop back tomorrow with the solution.