Jump to content

drop down list display help


php_begins

Recommended Posts

I have a drop down list which retrieves site name acronym and the url from the database. I need help in 2 things.

1.I do not want the url to be displayed in the drop down list.

2. When I hit submit I need to echo the sitename acronym as well as the url in a page called display.php.

Here is my code so far:

<form action="display.php" method ="post">
<tr>
   <td>Category</td>
   <td>
   <select name="new_id">
   <option value="">=============</option>
   <?php foreach($acronym as $key=>$value){ ?>
   <option value="<?php echo $value['site_id']; ?>"><?php echo $value['acronym']; ?></option>
   <option value="<?php echo $value['site_id']; ?>"><?php echo $value['url']; ?></option>
   <?php }?>
   </td>
</tr>
<input type="submit" name="submit" value="submit" />
</form>

 

 

Link to comment
https://forums.phpfreaks.com/topic/248046-drop-down-list-display-help/
Share on other sites

If you don't want the URL to show in the list, don't echo it in <option></option> tags. Use the site_id value from the $_POST array to query the database and retrieve the information you want displayed on the page after the form is submitted.

Use JavaScript.

 

<?php 

$acronym = array(
array( 'site_id' => '1', 'acronym' => 'HelloWorld', 'url' => 'http://www.helloworld.com/' ),
array( 'site_id' => '2', 'acronym' => 'FooBar', 'url' => 'http://www.foobar.com/' ),
array( 'site_id' => '3', 'acronym' => 'HerpDerp', 'url' => 'http://www.herpderp.com/' )
);

if( $_POST ) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}

?>
<script type="text/javascript">
function doSubmit() {
	var form = document.getElementById('thisForm');
	var select = document.getElementById('thisFormSelect');
	var acronym = {
		<?php $comma = FALSE; foreach ( $acronym as $o ) {
			echo ($comma ? ',':'') .
				"'{$o['site_id']}':{'acronym':'{$o['acronym']}','url':'{$o['url']}'}";
			$comma = TRUE; } ?>
	};
	if( select.value in acronym ) {
		var url = document.createElement( 'input' );
		url.name = 'url'; url.type = 'hidden'; url.value = acronym[ select.value ]['url'];
		var acro = document.createElement( 'input' );
		acro.name = 'name'; acro.type = 'hidden'; acro.value = acronym[ select.value ]['acronym'];
		form.appendChild( url );
		form.appendChild( acro );
		form.submit();
	}
}
</script>
<form id="thisForm" action="#" method="post">
<div id="boobies">
	Category:
	<select id="thisFormSelect" name="id">
		<?php foreach( $acronym as $option )
		echo '<option value="'.$option['site_id'].'">'.$option['acronym'].'</option>'; ?>
	</select><br>
	<input type="button" value="Submit" onclick="doSubmit()">
</div>
</form>

Archived

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

×
×
  • 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.