Page 1 of 1

SELECT data not being posted

Posted: Thu May 20, 2004 4:10 pm
by mjseaden
Dear All

The following bit of HTML/PHP/JavaScript:

Code: Select all

<STRONG>Country</STRONG> <select NAME="chk_Country" onChange="JavaScript:listB.populate();">

            <OPTION VALUE="Spain">Spain</OPTION>            </select>
            <STRONG>Location</STRONG> <select NAME="chk_Location" onChange="">
            	<SCRIPT LANGUAGE="JavaScript">listB.printOptions()</SCRIPT>
			</select>
            </FONT></TD>
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).

What is causing this?

Many thanks

Mark

Posted: Thu May 20, 2004 4:29 pm
by mjseaden
Would any more information be helpful?

Posted: Thu May 20, 2004 4:31 pm
by feyd
I think it would.

Posted: Thu May 20, 2004 4:35 pm
by mjseaden
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?

Posted: Thu May 20, 2004 4:39 pm
by feyd
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.

Posted: Thu May 20, 2004 5:22 pm
by mjseaden
Hi

The following is a cut down version of the real form:

Code: Select all

<FORM METHOD="post" NAME="myForm" ACTION="XXX/admin/property/add/photos" STYLE="margin: 0px">
    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
        <TR>
        	<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
        </TR>
	</TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
        <TR>
            <TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
			<STRONG>No. of Bathrooms</STRONG>
            </FONT></TD>
            <TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
            <?php
                echo '<INPUT TYPE="text" LENGTH="5" SIZE="3" NAME="txt_Bathrooms" VALUE="'.$_SESSION['bathrooms'].'"/>';
                echo '  e.g. 2';
            ?>
            </FONT></TD>
        </TR>
    </TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="8" WIDTH="734" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
        </TR>
    </TABLE>
    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
        </TR>
    </TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
            <STRONG>Price</STRONG>
            </FONT></TD>
            <TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
            <?php
                echo '<INPUT TYPE="text" LENGTH="100" SIZE="15" NAME="txt_Price" VALUE="'.$_SESSION['property_price'].'"/>';
                echo '  Euros';
            ?>
            </TD>
        </TR>
    </TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
        </TR>
    </TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="20" WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
            <STRONG>Place</STRONG>
            </FONT></TD>
            <TD HEIGHT="20" WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT FACE="Tahoma" SIZE="2" COLOR="#FFFFFF">
            <STRONG>Country</STRONG> <SELECT NAME="chk_Country" onChange="JavaScript:listB.populate();">
            <?php

            // Populate the options again so start off the dynamic updating
            $result = mysql_query("SELECT CountryTitle FROM Countries");

            if(!$result)
            {
            	do_html_message('Error: Could not carry out MySQL query: '.mysql_error());
                exit();
            }

            while( $row = mysql_fetch_array( $result ) )
            {
            	echo '<OPTION VALUE="'.$row['CountryTitle'].'">'.$row['CountryTitle'];
            }

            ?>
            </SELECT>
            <STRONG>Location</STRONG> <SELECT NAME="chk_Location" onChange="">
            	<SCRIPT LANGUAGE="JavaScript">listB.printOptions()</SCRIPT>
			</SELECT>
            </FONT></TD>
        </TR>
        <TR>
        	<TD WIDTH="114" STYLE="padding-left:16px" BGCOLOR="#000066"></TD>
            <TD WIDTH="604" STYLE="padding-right:16px" BGCOLOR="#000066"><FONT COLOR="#FFFFFF" FACE="Tahoma" SIZE="2">
            Location not listed? Click <A HREF="#"><FONT COLOR="#FFFFFF">here</FONT></A> to add a new location (currently disabled).
            </FONT></TD>
        </TR>
    </TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="8" WIDTH="750" BGCOLOR="#000066"></TD>
        </TR>
    </TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD WIDTH="419" STYLE="padding-left:16px" BGCOLOR="#000066" ALIGN="LEFT">
            <INPUT TYPE="reset" VALUE="Clear" NAME="reset"/>
            </TD>
        	<TD WIDTH="300" STYLE="padding-right:15px" BGCOLOR="#000066" ALIGN="RIGHT">
            <INPUT TYPE="submit" VALUE="Next >>" NAME="next"/>
            </TD>
        </TR>
	</TABLE>

    <TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0">
    	<TR>
        	<TD HEIGHT="5" WIDTH="750" BGCOLOR="#000066">
            </TD>
		</TR>
    </TABLE>

    </TABLE>

    </FORM>
Many thanks
Mark

Posted: Thu May 20, 2004 5:28 pm
by feyd
what about the javascript?

Posted: Thu May 20, 2004 5:54 pm
by feyd
I found this, since the information I was looking for wasn't in your original post, which has disappeared..

Code: Select all

function DynamicOptionList_populate() &#123;
	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++) &#123;
		var sel = theform&#1111;this.dependencies&#1111;i]];
		var selName = sel.name;
		// If this is the first dependent list, just fill in the dependentValues
		if (!dependentValuesInitialized) &#123;
			dependentValuesInitialized = true;
			for (j=0; j<sel.options.length; j++) &#123;
				if (sel.options&#1111;j].selected) &#123;
					this.dependentValues&#1111;sel.options&#1111;j].value] = true;
					&#125;
				&#125;
			&#125;
		// Otherwise, add new options for every existing option
		else &#123;
			var tmpList = new Object();
			var newList = new Object();
			for (j=0; j<sel.options.length; j++) &#123;
				if (sel.options&#1111;j].selected) &#123;
					tmpList&#1111;sel.options&#1111;j].value] = true;
					&#125;
				&#125;
			for (obj in this.dependentValues) &#123;
				for (obj2 in tmpList) &#123;
					newList&#1111;obj + this.delimiter + obj2] = true;
					&#125;
				&#125;
			this.dependentValues = newList;
			&#125;
		&#125;

	var targetSel = theform&#1111;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++) &#123;
		if (targetSel.options&#1111;i].selected) &#123;
			targetSelected&#1111;targetSel.options&#1111;i].value] = true;
			&#125;
		&#125;

	targetSel.options.length = 0; // Clear all target options
		
	for (i in this.dependentValues) &#123;
		if (typeof this.options&#1111;i] == "object") &#123;
			var o = this.options&#1111;i];
			for (j=0; j<o.length; j+=2) &#123;
				var text = o&#1111;j];
				var val = o&#1111;j+1];
				targetSel.options&#1111;targetSel.options.length] = new Option(text, val, false, false);
				if (typeof this.defaultValues&#1111;i] != "undefined" && this.defaultValues&#1111;i]!=null) &#123;
					for (def in this.defaultValues&#1111;i]) &#123;
						if (def == val) &#123;
							targetSelected&#1111;val] = true;
							&#125;
						&#125;
					&#125;
				&#125;
			&#125;
		&#125;
	targetSel.selectedIndex=-1;
	
	// Select the options that were selected before
	for (i=0; i<targetSel.options.length; i++) &#123;
		if (targetSelected&#1111;targetSel.options&#1111;i].value] != null && targetSelected&#1111;targetSel.options&#1111;i].value]==true) &#123;
			targetSel.options&#1111;i].selected = true;
			&#125;
		&#125;
	&#125;

Posted: Thu May 20, 2004 5:56 pm
by mjseaden
Hi Feyd,

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.

Cheers
Mark

Posted: Thu May 20, 2004 6:03 pm
by Unipus
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:

Code: Select all

die(print_r($_POST));
See what those select variables really look like when they're being sent over.

Posted: Thu May 20, 2004 6:17 pm
by mjseaden
Hi Unipus

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.

Many thanks

Mark