Jump to content

Select Options Highlighted With $_post Data


son

Recommended Posts

I have a drop-down which highlights the current options for a product as:

<?php	$q2 = 'SELECT ageing_id, ageing_name FROM ageing ORDER BY ageing_id';	$r2 = mysqli_query($dbc, $q2);		if (mysqli_num_rows($r2) > 0)		{?><div style="float:left; width:130px;"><p><label for="ageing"><?php echo check_error('ageing', 'Ageing'); ?></label><br /><select name="ageing[]" id="ageing[]" multiple="multiple" size="3"><option value="0">Not applicable</option><?php			while (list($ageing_id, $ageing_name) = mysqli_fetch_array($r2, MYSQLI_NUM)) 			{			echo "<option value=\"$ageing_id\"";			// check if there are ageing option assigned to product			$q2a = "SELECT age_id FROM productAge WHERE age_id = $ageing_id AND product_id = " . $pid . "";			$r2a = mysqli_query($dbc, $q2a);				if (mysqli_num_rows($r2a) > 0)				{				echo ' selected="selected"';				}				echo ">" . $ageing_name . "</option>\n";				}			echo "</select></p>\n";?>

That is all working, but now my problem. Upon form submission there are several error checks and user might end up having to fill out or amend entries. If he has de-selected or selected options of ageing drop-down before submission then he will be faced again with entries from database. How can I amend my checks somehow, so it will take data from db if there is no $_POST data data, but otherwise select what is transfered in $_POST. If user has not changed anything then it should be the same anyway. I tried to have a check on the hiddden value on form called submitted, but somehow I got this wrong and it would always go back to the database entries...Son

Link to comment
Share on other sites

For starters to see all your $_POST data:

echo "<pre>";print_r($_POST);echo "</pre>";

The <pre> tags are just so you can view the POST data easier.So to check if the hidden value is sent with the POST data, you can do an if statement like so:

if(isset($_POST['submitted'])){  //Form was sent, execute code in these brackets.  //eg. set variables to $_POST data.}else{  //Form was not sent  // set variables to database values.}

(If you aren't already, just name the submit button, and check the name. ($_POST['submitName'])Forgive me I understood this wrong, but basically if the form was not sent you want to pull information from a database, but if the form is sent you want to use $_POST information?If so, try this:

if(isset($_POST['ageing[0]')){  $info = $_POST['ageing']; //swap the form name for the quoted writing.}else{  $info = //some database entry}

If this isn't what you wanted please explain more.

Link to comment
Share on other sites

For starters to see all your $_POST data:
while (list($ageing_id, $ageing_name) = mysqli_fetch_array($r2, MYSQLI_NUM)) 			{			echo "<option value=\"$ageing_id\"";			if(isset($_POST['ageing[0]']))			{			echo ' selected="selected"';			}			else			{			// check if there are ageing option assigned to product  			$q2a = "SELECT age_id FROM productAge WHERE age_id = $ageing_id AND product_id = " . $pid . "";			$r2a = mysqli_query($dbc, $q2a);				if (mysqli_num_rows($r2a) > 0)				{				echo ' selected="selected"';				}			}				echo ">" . $ageing_name . "</option>\n";				}

but this does not work. Taking out the if the code in else clause (obviously then without else) works fine. My problem is

if(isset($_POST['ageing[0]']))			{			echo ' selected="selected"';			}

How do I check for all values in $_POST array for ageing and make them highlighted if relevant?Son

Link to comment
Share on other sites

Finally figured it out. The following is working (for anyone needing the same sort of thing):

echo "<option value=\"$ageing_id\"";				if(isset($_POST['submitted'])) 				{					if($ageing != 0 && in_array("$ageing_id", $_POST['ageing']))					{					echo ' selected="selected"';					}				}				else				{				// check if there are ageing option assigned to product  				$q2a = "SELECT age_id FROM productAge WHERE age_id = $ageing_id AND product_id = " . $pid . "";				$r2a = mysqli_query($dbc, $q2a);					if (mysqli_num_rows($r2a) > 0)					{					echo ' selected="selected"';					}				}			echo ">" . $ageing_name . "</option>\n";

Son

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...