Page 1 of 2

Form not passing field value

Posted: Fri Feb 24, 2012 8:59 am
by eblackwell
This should be so basic; I've done it thousands of times. I have a form, the user selects the value, and I grab that value on the next page with POST. But it's blank, and I must be staring at this for too long because I can't see why.

It's a form that fills the first select box from a state array; the second dropdown pulls from a database based on the state. When I look at the output from the code, it's fine. Here it is in action: http://youradminpartners.com/state-selection/. It's not pretty yet, because I'm focusing on function. Form code:

Code: Select all

<form action="http://youradminpartners.com/bcompliant/" method="post">
	  <?php

	    $state=$_GET['state'];

	  	function showOptionsDrop($array,$state)
	  	{
	  	  $string = '';
	  	  foreach($array as $k => $v)
	  	  {
	  	    // check to see if state has already been selected
	  	    // k equals the state abbreviation code

			if(isset($state) and strlen($state) > 0)
			{
			  if ($state==$k)
			  {
			    $string .= '<option selected="selected" value="'.$k.'">'.$v.'</option>'."\n";
			  }
			} else {
			  $string .= '<option value="'.$k.'">'.$v.'</option>'."\n";
            }
	  	  }
	  	return $string;
	  	}
	?>

	<select name="state" onchange="reload(this.form)">
	  <option value="0">Select state</option>
	  <?php echo showOptionsDrop($states_arr,$state); ?>
	</select>

    <br /><br /><br />

     <p>Please select your employer's name:</p>

	<select name="employer">
	  <option value="">Select employer</option>
		<?php
		  // set database server access variables:
		  $host = "localhost";
		  $user = "youradmi";
		  $pass = "Admin8Partners";
		  $db = "youradmi_demographics";

		  // open connection
		  $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

		  // select database
		  mysql_select_db($db) or die ("Unable to select database!");

		   if(isset($state) and strlen($state) > 0)
		  {
			// create query
           $query="SELECT DISTINCT name FROM employer_state where state='$state' order by name";
		  }
		else
		  {
			// create query
           $query="SELECT DISTINCT name FROM employer_state order by name";
	    }

         // execute query
		$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

		// see if any rows were returned
		if (mysql_num_rows($result) > 0)
		{
		  // yes
		  // printing the list box select command
		  while($nt=mysql_fetch_array($result))
		  {
		    //Array or records stored in $nt
		    echo "<option value=\"$nt[name]\">$nt[name]</option>";
		    /* Option values are added by looping through the array */
		  }
		}
		else
		{
		  // no
		  echo "<option value=''>No employers in that state</option>";
		}
	 // close connection
	mysql_close($connection);

     ?>
	 </select>
	<br /><br />
    <input type="submit" name="mysubmit" value="Continue to Public Portal" /> <button type="button" style="width:65;height:65" onClick="window.location='http://youradminpartners.com/state-selection/'">Clear State</button>
</form>
When I get to the next page, I echo the variable to see if it's there, but it's blank.

Thanks for your help!

Re: Form not passing field value

Posted: Fri Feb 24, 2012 9:23 am
by temidayo
Post the code you are using to collect the POST data on http://youradminpartners.com/bcompliant/

Re: Form not passing field value

Posted: Fri Feb 24, 2012 9:49 am
by eblackwell

Code: Select all

<?php

echo $_POST['employer'];
?>

Re: Form not passing field value

Posted: Sat Feb 25, 2012 1:54 am
by temidayo
Something is seriously wrong. I check your form with firefox tamper addon and the form posted correctly. It has to do with
point where you are collecting the data. Try this:
<?php
echo print_r($_POST);
?>

Re: Form not passing field value

Posted: Sun Feb 26, 2012 11:09 am
by eblackwell
Thanks for helping!

I get this: Array ( ) 1

Re: Form not passing field value

Posted: Sun Feb 26, 2012 12:24 pm
by eblackwell
This gets even more weird.

I took all the PHP and SQL out of it to troubleshoot where the problem is happening. Now it's a straight form and it's still isn't posting the variable!

Code: Select all

<form action="http://youradminpartners.com/bcompliant/" method="post">

	<select name="state">
			<option value="0">Select state</option>
			<option value="AL">Alabama</option>
			<option value="AK">Alaska</option>
			<option value="AZ">Arizona</option>
			<option value="AR">Arkansas</option>
			<option value="CA">California</option>
			<option value="CO">Colorado</option>
			<option value="CT">Connecticut</option>
			<option value="DE">Delaware</option>
			<option value="DC">District of Columbia</option>
			<option value="FL">Florida</option>
			<option value="GA">Georgia</option>
			<option value="HI">Hawaii</option>
			<option value="ID">Idaho</option>
			<option value="IL">Illinois</option>
			<option value="IN">Indiana</option>
			<option value="IA">Iowa</option>
			<option value="KS">Kansas</option>
			<option value="KY">Kentucky</option>
			<option value="LA">Louisiana</option>
			<option value="ME">Maine</option>
			<option value="MD">Maryland</option>
			<option value="MA">Massachusetts</option>
			<option value="MI">Michigan</option>
			<option value="MN">Minnesota</option>
			<option value="MS">Mississippi</option>
			<option value="MO">Missouri</option>
			<option value="MT">Montana</option>
			<option value="NE">Nebraska</option>
			<option value="NV">Nevada</option>
			<option value="NH">New Hampshire</option>
			<option value="NJ">New Jersey</option>
			<option value="NM">New Mexico</option>
			<option value="NY">New York</option>
			<option value="NC">North Carolina</option>
			<option value="ND">North Dakota</option>
			<option value="OH">Ohio</option>
			<option value="OK">Oklahoma</option>
			<option value="OR">Oregon</option>
			<option value="PA">Pennsylvania</option>
			<option value="RI">Rhode Island</option>
			<option value="SC">South Carolina</option>
			<option value="SD">South Dakota</option>
			<option value="TN">Tennessee</option>
			<option value="TX">Texas</option>
			<option value="UT">Utah</option>
			<option value="VT">Vermont</option>
			<option value="VA">Virginia</option>
			<option value="WA">Washington</option>
			<option value="WV">West Virginia</option>
			<option value="WI">Wisconsin</option>
			<option value="WY">Wyoming</option>
	</select>

    <br /><br /><br />

     <p>Please select your employer's name:</p>

	<select name="employer">
		<option value="0">Select employer</option>
		<option value="Alleghany County Public Schools">Alleghany County Public Schools</option><option value="Ameilia County Public Schools">Ameilia County Public Schools</option><option value="Appomattox County School Board">Appomattox County School Board</option><option value="Bath County School Board">Bath County School Board</option><option value="Bedford County Public Schools">Bedford County Public Schools</option><option value="Bland County School Board">Bland County School Board</option><option value="Botetourt County Public Schools">Botetourt County Public Schools</option><option value="Bristol Virginia Public Schools">Bristol Virginia Public Schools</option><option value="Buchanan County Public School">Buchanan County Public School</option><option value="Buckingham County School Board">Buckingham County School Board</option><option value="Buena Vista City Public Schools">Buena Vista City Public Schools</option>
		</select>
    <input type="submit" name="mysubmit" value="Continue to Public Portal" /> <button type="button" style="width:65;height:65" onClick="window.location='http://youradminpartners.com/state-selection/'">Clear State</button>
</form>

Re: Form not passing field value

Posted: Sun Feb 26, 2012 2:39 pm
by eblackwell
I figured out it's not the form. I redirected to a different page and the POST works fine. So it's the page after the form, which makes no sense either. Here is what I have:

Code: Select all

<?php /* Template Name: bCompliant Public Pages */  ?>

<?php
echo $_POST['employer'];
?>[/syntax

I have also tried _REQUEST as well the echo print_r($_POST); suggestion above.  What could possible prevent something as simple as this?

Re: Form not passing field value

Posted: Sun Feb 26, 2012 2:45 pm
by litebearer
what is the path/name of the file to which you are attempting to post

Re: Form not passing field value

Posted: Sun Feb 26, 2012 4:20 pm
by eblackwell
http://youradminpartners.com/bcompliant/

I changed it to a different file to testing -- http://youradminpartners.com/data-entry-menu/ -- which worked fine. But the bcompliant still does not, even with the same code.

Re: Form not passing field value

Posted: Sun Feb 26, 2012 5:46 pm
by eblackwell
All right, I found out the problem. It's the GET with the state to fill in the Employer dropdown. If I take out that reload & GET call on the state, I get the POST variables with no problem on the next page.

NOW I have to figure out how to fill in the second dropdown when the user chooses the state without reloading with the GET. I wonder if I can do POST for that?

Re: Form not passing field value

Posted: Mon Feb 27, 2012 5:11 am
by temidayo
It might be that when you reload the page, you affect the html form method. I wanted to confirm that but I see that
you've changed your pages. Wish you success in getting it right.

Re: Form not passing field value

Posted: Wed Feb 29, 2012 9:06 am
by eblackwell
temidayo wrote:It might be that when you reload the page, you affect the html form method. I wanted to confirm that but I see that
you've changed your pages.
I'll put it back if you want because this isn't selecting the second select box correctly from the database. Here is the page the way it was: http://youradminpartners.com/state-employer-selection/

Re: Form not passing field value

Posted: Wed Feb 29, 2012 12:21 pm
by temidayo
Are you using some sort of framework? If yes, what framework are you using?

If it won't be too much of a problem, why don't you separate the selection of state form from
selection of employer.

Just make it like a step 1 and step 2 stuff since you are not using ajax and you are posting
the form back anyway.

Re: Form not passing field value

Posted: Wed Feb 29, 2012 3:32 pm
by eblackwell
Sorry to be dense, but not sure what you mean about framework. It's in WordPress - the original developers set that up - if that helps.

I thought of separating state and employer, but it means for clicks for the user going to 2 forms.
temidayo wrote:Are you using some sort of framework? If yes, what framework are you using?

If it won't be too much of a problem, why don't you separate the selection of state form from
selection of employer.

Re: Form not passing field value

Posted: Wed Feb 29, 2012 3:40 pm
by temidayo
yeah, you answered my question about the framework.
eblackwell wrote: I thought of separating state and employer, but it means for clicks for the user going to 2 forms.
The user as it is is already going to the same form twice.