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

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.