jagguy Posted March 31, 2007 Share Posted March 31, 2007 How do i click on an option and then automatically sort a mysql query by that value? I can get a set of option to appear and can do a query but not combine them? echo "<select name='alName'>"; echo "<option value='one' selected='selected'>two</option>"; echo "<option>three</option>"; echo "<option>four</option>"; echo "<option>five</option>"; echo "</select>"; $sql="select a.products_name ,b.products_price from products_description a, products b where a.products_id= b.products_id order by SELECTED VALUE"; Quote Link to comment Share on other sites More sharing options...
Heero Posted March 31, 2007 Share Posted March 31, 2007 Hello, I am not entirely sure what you are trying to do, but you want to be able to sort a list of items based on the chosen value? If that is the case, you can't do it instantly because you would have to resubmit the form (at least that's what I think...I haven't touched PHP in like 2 years). Quote Link to comment Share on other sites More sharing options...
jagguy Posted March 31, 2007 Author Share Posted March 31, 2007 Yes I want to be able to sort a list of items based on the chosen value. Yes you can do this because i see it everywhere. It does help if you reply to a php forum ,that you actually know php to some degree, but thanks anyway. Quote Link to comment Share on other sites More sharing options...
Jove Posted March 31, 2007 Share Posted March 31, 2007 with php you can't do it instantly; you have to use javascript Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 You would have to make the select menu auto post when you click but you would do as you did with a few alterations echo "<select name='alName'>"; echo "<option selected='selected'>one</option>"; echo "<option >two</option>"; echo "<option>three</option>"; echo "<option>four</option>"; echo "</select>"; $selection=$_POST['alName']; $sql="select a.products_name ,b.products_price from products_description a, products b where a.products_id= b.products_id order by $selection"; Your selection menu would need to be something like Price, Rating, Name, Brand and stuff liek that? if that is what you after? so each select value is actually a field name.. An other way without refreshing is using AJAX regards Liam Quote Link to comment Share on other sites More sharing options...
DeathStar Posted March 31, 2007 Share Posted March 31, 2007 what about functions and onclick event? Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 <script language="JavaScript"> function autoSubmit() { var formObject = document.forms['theForm']; formObject.submit(); } </script> <form name="theForm" method="get"> <?php echo "<select name='province' onChange='autoSubmit();'> echo "<select name='alName'>"; echo "<option selected='selected'>one</option>"; echo "<option >two</option>"; echo "<option>three</option>"; echo "<option>four</option>"; echo "</select>"; ?> </form> That should do the job Liam Quote Link to comment Share on other sites More sharing options...
jagguy Posted March 31, 2007 Author Share Posted March 31, 2007 Hi, How does the JS work when called <script language="JavaScript"> function autoSubmit() { var formObject = document.forms['theForm']; formObject.submit(); } </script> where i would get a new option value to use on mysql? This follows on from your prev post I take it. thanks for this. Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 31, 2007 Share Posted March 31, 2007 Well it submits the form so the data is passed via $_POST Quote Link to comment Share on other sites More sharing options...
Heero Posted March 31, 2007 Share Posted March 31, 2007 It does help if you reply to a php forum ,that you actually know php to some degree, but thanks anyway. And was I wrong about what I said? PHP works on the server not the client side so to do something with it you have to send and retrieve data from the server. If want it to refresh immediately and not sent something to the server then PHP is not your choice. What you were seeing everywhere else was probably AJAX. Is that a BETTER answer for you? Quote Link to comment Share on other sites More sharing options...
jagguy Posted April 2, 2007 Author Share Posted April 2, 2007 The program works fine to call a simple page with an option choice but it won't work if the page to call has got parameters. How do you call a page to submit to if the page has the url http://localhost/ecommerce/zen/index.php?main_page=index&cPath=3_10 This is complicated to call because I get the page http://localhost/ecommerce/zen/index.php php --- $mypath2="index.php?main_page=index&cPath="; echo " <form name='theForm' method='get' onChange='autoSubmit();' >"; echo "<input type='hidden' name='$mypath' value='$bpath'>"; echo "<br><br><br><select name='$alName'>"; echo "<option selected='selected'>products_price a-z</option>"; echo "<option value='products_price desc'>products_price high to low</option>"; echo "<option>a.products_name</option>"; echo "<option value='a.products_name desc'>a.products_name z-a</option>"; echo "</select></form><br>"; ...... JS -- <script language="JavaScript"> function autoSubmit() { var formObject = document.forms['theForm']; formObject.submit(); } </script> Quote Link to comment Share on other sites More sharing options...
jitesh Posted April 2, 2007 Share Posted April 2, 2007 echo "<select name='alName' onchage="javascript:window.location.href='currentfile.php?field='+this.value;">"; echo "<option value='fieldname_of_table_1' selected='selected'>two</option>"; echo "<option value='fieldname_of_table_2'>three</option>"; echo "<option value='fieldname_of_table_3'>four</option>"; echo "<option value='fieldname_of_table_4'>five</option>"; echo "</select>"; $sql="select a.products_name ,b.products_price from products_description a, products b where a.products_id= b.products_id order by ".$_GET['field']; Quote Link to comment Share on other sites More sharing options...
jagguy Posted April 2, 2007 Author Share Posted April 2, 2007 I solved it by using hidden fields with required values . This added on to the original url. Your way looks good to. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted April 2, 2007 Share Posted April 2, 2007 please click solved Quote Link to comment 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.