Jump to content

Return value if isset (form)


tebrown

Recommended Posts

Hey Guys,

 

Im working with a form and need some help.

 

So at the moment i have got it working so that if the user doesn't select the values from the drop down list, an error will occur saying 'Please select all fields'.

 

What i want to do now is make it so that if this error occurs, the value of the drop down list will stay as it is an NOT reset. Could someone help me out?

 

* Note that the code below is currently ok, basically it checks if there is a value, if not it will say "Select Player".

 


<td>Prop</td>
	    <td><select name="prop1" style="width: 150px">
		<option value="<? echo $row['prop1']; ?>"><? if (empty($row['prop1'])) { echo "Select Player"; } else { echo $row['prop1'];  } ?></option>
		 <?php echo $option_str; ?>
	    </select></td>
	    <td>16.</td>
	    <td><select name="r16" style="width: 150px">
		<option value="<? echo $row['r16']; ?>"><? if (empty($row['r16'])) { echo "Select Player"; } else { echo $row['r16'];  } ?></option>
	      <?php echo $option_str; ?>
	    </select></td>

Link to comment
Share on other sites

Thanks for replying, i tried this but doesnt seem to work?

 


<td>Prop</td>
	    <td><select name="prop1" style="width: 150px">
		<option <?php if (isset($_POST['prop1'])) { print ' value="' . $_POST['prop1'] . '"'; } ?>><? if (empty($row['prop1'])) { echo "Select Player"; } else { echo $row['prop1'];  } ?></option>
		 <?php echo $option_str; ?>
	    </select></td>

Link to comment
Share on other sites

The way that I imagine you are doing this is sort of wonky. I think you will end up with two identical items in your dropdown. Don't bother with changing the default option ("Select Player") to the selected option, just change the selected option to actually be selected.

 

The reason I say that is because you have an $option_str variable, which I assume to be a bunch of other options. Unless you are removing the selected one behind-the-scenes, you will end up with two identical ones.

 

With that aside, you didn't read my code properly. I am adding the selected attribute to make that option the default selected option. Instead of doing that logic at the top you should instead do it on each option so that you don't have any duplicates. It would help you out a lot to put your options into an array, then you can loop over them so you only need the logic once.

 

Here's an example:

$options = array(
'john'     => 'John',
'dan'      => 'Dan',
'michelle' => 'Michelle',
'robert'   => 'Robert',
'jennifer' => 'Jennifer'
);

echo '<select name="first_name">';
echo '<option value="">Select Name</option>';

foreach($options as $key => $val)
{
$selected = $_POST['first_name'] == $key ? 'selected="selected"' : null;

echo '<option value="' . $key . '"' . $selected . '>' . $val . '</option>';
}

echo '</select>';

Link to comment
Share on other sites

Thanks for the reply.

 

Im working with multiple drop down lists. That is what the $options_str variable if for.

 


<?php

	$id=$_GET['id'];
        $sql="SELECT * FROM fixtures WHERE id=$id AND club='$club' AND team='$team'";
	$result=mysql_query($sql);
        $row=mysql_fetch_array($result);

	$selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team' ORDER by lname DESC";
	$player=mysql_query($selectplayers);
	$option_str = '';

		while($rowplayer = mysql_fetch_array($player)) {

			$fname=$rowplayer["fname"];
			$lname=$rowplayer["lname"];
					$option_str .= "<OPTION VALUE=\"$fname $lname\">$fname $lname</OPTION>";
			}

	?>

 

And i see what you mean by 'two identical items in your dropdown'. This is currently happening. I will give it a shot and get back to you.

 

Cheers.

Link to comment
Share on other sites

Ok, i tried, but failed. I followed your method but not getting anywhere.

 

Im having no errors, but its just not holding the value when i press submit...

 


<?php

	$id=$_GET['id'];
        $sql="SELECT * FROM fixtures WHERE id=$id AND club='$club' AND team='$team'";
	$result=mysql_query($sql);
        $row=mysql_fetch_array($result);

	$selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team' ORDER by lname DESC";
	$player=mysql_query($selectplayers);
	$option_str = '';

		while($rowplayer = mysql_fetch_array($player)) {

			$fname=$rowplayer["fname"];
			$lname=$rowplayer["lname"];
					$option_str .= "<OPTION VALUE=\"$fname $lname\">$fname $lname</OPTION>";
			}

	?>



	<form id="form" action="lineup.php?id=<? echo $row['id']; ?>" name="lineup" method="post">
	<table class='lineups' width="560" cellpadding="5">
	  <tr>
	    <td colspan="2">Starting Lineup</td>
	    <td colspan="2">On the Bench</td>
	  </tr>
	  <tr>
	    <td width="119"> </td>
	    <td width="160"> </td>
	    <td width="69"> </td>
	    <td width="160"> </td>
	  </tr>
	  <tr>
	    <td>Prop</td>
	    <td><select name="prop1" style="width: 150px">
		<option <?php if(isset($_POST['prop1']) && $_POST['prop1'] == "prop1"){echo 'selected="selected"';}?>><?php echo $option_str; ?></option>

	    </select></td>
	    <td>16.</td>
	    <td><select name="r16" style="width: 150px">
		<option value="<? echo $row['r16']; ?>"><? if (empty($row['r16'])) { echo "Select Player"; } else { echo $row['r16'];  } ?></option>
	      <?php echo $option_str; ?>
	    </select></td>
	  </tr>
	</table><br /><br />
	<input type="submit" name="lineup" value="Save Lineup" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.