Form not passing field value

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!

Moderator: General Moderators

eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Form not passing field value

Post 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!
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Form not passing field value

Post by temidayo »

Post the code you are using to collect the POST data on http://youradminpartners.com/bcompliant/
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post by eblackwell »

Code: Select all

<?php

echo $_POST['employer'];
?>
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Form not passing field value

Post 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);
?>
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post by eblackwell »

Thanks for helping!

I get this: Array ( ) 1
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post 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>
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post 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?
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Form not passing field value

Post by litebearer »

what is the path/name of the file to which you are attempting to post
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post 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.
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post 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?
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Form not passing field value

Post 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.
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post 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/
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Form not passing field value

Post 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.
eblackwell
Forum Newbie
Posts: 19
Joined: Mon Oct 10, 2011 3:19 pm

Re: Form not passing field value

Post 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.
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Form not passing field value

Post 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.
Post Reply