spryce Posted April 27, 2011 Share Posted April 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234833-use-a-tag-to-post-a-variable-that-can-be-used-in-a-function/ Share on other sites More sharing options...
saurabhx Posted April 27, 2011 Share Posted April 27, 2011 May be you can use ajax to pass the value from your javascript code to php and process it.. Quote Link to comment https://forums.phpfreaks.com/topic/234833-use-a-tag-to-post-a-variable-that-can-be-used-in-a-function/#findComment-1206790 Share on other sites More sharing options...
Muddy_Funster Posted April 27, 2011 Share Posted April 27, 2011 what's the code within your get_query_1() function? Quote Link to comment https://forums.phpfreaks.com/topic/234833-use-a-tag-to-post-a-variable-that-can-be-used-in-a-function/#findComment-1206791 Share on other sites More sharing options...
spryce Posted April 27, 2011 Author Share Posted April 27, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/234833-use-a-tag-to-post-a-variable-that-can-be-used-in-a-function/#findComment-1206806 Share on other sites More sharing options...
spryce Posted April 27, 2011 Author Share Posted April 27, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/234833-use-a-tag-to-post-a-variable-that-can-be-used-in-a-function/#findComment-1206833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.