Jump to content

Use a <select> tag to post a variable that can be used in a function?


spryce

Recommended Posts

I am currently using a javascript to redirect the page using a select tag and the onchange property. Each page calls a different function.

 

I want to be able to pass the <select> value to a variable that I can use within a php condition statement on the same page. This will generate the required output based on the <select> value. This way I dont require a seperate page for each query.

 

So onchange.... $select_value == this.value?

 

Then within my page:

if ($select_value == 'alpha') {
    get_query_alpha();
} 
if ($select_value == 'bravo') {
    get_query_bravo();
} 

 

This is my current set up...

	<p>Order by: 
	<select name="query" id="query" onchange="gotourl(this.value)">
		<option value="query1.php" >Sort by A</option>
		<option value="query2.php" >Sort by B</option>
		<option value="query3.php" >Sort by C</option>
		<option value="query4.php" >Sort by D</option>
	</select></p>

	<table> <?php get_query_1();?> </table>

 

and js...

function gotourl(url){ 
	window.location= url;
}
function selectsubject(){
	alert("Please select a subject!");
}

 

Is there a simple way to do this? Thanks.

Link to comment
Share on other sites

get_query_1() just calls a specific sql query on the db.

 

I just tried this.

 

<?php 
$query = get_query();

function get_query() {
	global $query;
	if (!$_POST['query']) {     // giving me an undefined index error here
	$query = ' ';
	} 
	else {
	$query = $_POST['query'];
	}
}

function return_incident_data($query ) {
	if ($query === ' ') {
		get_query1();
	}
	if ($query === 'query1') {
		get_query1();
	}
	if ($query === 'query2') {
		get_query2();
	}
	if ($query === 'query3') {
		get_query3();
	}
}	
?>
<form method="post" action="thispage.php">
	<p>Order by: 
	<select name="query" id="query" onchange="this.form.submit()">

		<option value="query1" >Sort by A</option>
		<option value="query2" >Sort by B</option>
		<option value="query3" >Sort by C</option>
		<option value="query4" >Sort by D</option>
	</select></p>

               <table><?php return_incident_data($query); ?> </table>

 

But I get the undefined index error....

Link to comment
Share on other sites

This worked! yay me....

 

<?php 
echo get_query();

function get_query() {
	global $query;
	if (!isset($_POST['query'])) {
	$query = ' ';
	} 
	else {
	$query = $_POST['query'];
	}
}

function return_incident_data($query ) {
	if ($query === ' ') {
		get_query1();
	}
	if ($query === 'query1') {
		get_query1();
	}
	if ($query === 'query2') {
		get_query2();
	}
	if ($query === 'query3') {
		get_query3();
	}
}	
?>
<form name="form1" id="form1" method="post" action="thispage.php">
	<p>Order by: 
	<select name="query" id="query" onchange="document.form1.submit()">

		<option value="query1" >Sort by A</option>
		<option value="query2" >Sort by B</option>
		<option value="query3" >Sort by C</option>
		<option value="query4" >Sort by D</option>
	</select></p>

               <table><?php return_incident_data($query); ?> </table>

Link to comment
Share on other sites

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.