Jump to content

select option


jagguy

Recommended Posts

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";

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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'];

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.