Dropdown list PHP/Javascript confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CoreLEx
Forum Newbie
Posts: 10
Joined: Sun Jun 01, 2003 5:18 pm

Dropdown list PHP/Javascript confusion

Post by CoreLEx »

Hi everyone

I've been baffled by this problem all day. It's a simple one but the problem is that so am I.

I've got a dropdown list (static) that I want to query a database depending on the option the user selects from the list.

I've got a Javascript as such that dynamically sets the text of the selected option to a variable txt:

Code: Select all

<script language="JavaScript">
function chkForm()
{
	txt=document.formsї0].category.optionsїdocument.formsї0].category.selectedIndex].text;
}
</script>
The Javascript is run when the form containing the dropdown list is submitted:

Code: Select all

<form action="<? echo($PHP_SELF); ?>"onChange="chkForm()"; method="post">
  <select name="category" value="category">
  <option value="All" selected>All</option>
  <option value="General">General</option>
  <option value="Microsoft">Microsoft</option>
  <option value="Programming">Programming</option>
  <option value="Helpline">Helpline</option>
  </select><input type="submit" value="Go!" name="submit"><br><br>
</form>
The problem is that I now don't know how to run a query like the one below depending on the selected text:

Code: Select all

$sql="select id,author,title,joke,category,approved from jokes WHERE category=txt";
I've got the page up at http://www.cyberiapc.com/jokes.php and what I'd like to do is allow visitors to choose to display jokes from only a certain category. I know that Javascript is client-side and PHP is server-side, and so that might be what's confusing me. :(

Thanks for any pointers.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

Well, there are a couple of things.
First, start out by letting the php handle the error issues, and not Java, so eliminate the Java code and check form.
than...

1) You should edit the variables listed below:

Code: Select all

<form action="<? echo($PHP_SELF); "onChange="chkForm()"; method="post"> 
  <select name="category" value="category"> 
  <option value="All" selected>All</option> 
  <option value="General">General</option> 
  <option value="Microsoft">Microsoft</option> 
  <option value="Programming">Programming</option> 
  <option value="Helpline">Helpline</option> 
  </select><input type="submit" value="Go!" name="submit"><br><br> 
</form>
And just relplace with

Code: Select all

<form action="<? echo($PHP_SELF); method="post" ?> 
  <select name="0" value="category"> 
  <option value="1" selected>All</option> 
  <option value="2">General</option> 
  <option value="3">Microsoft</option> 
  <option value="4">Programming</option> 
  <option value="5">Helpline</option> 
  </select>
<input type="submit" value="Go!" name="submit"><br><br> 
</form>
This will simplify your DB call dramtically.

Now, You will need to define the above listings in your .php script as variables.

Code: Select all

if (empty($category)) &#123;
print "Please select a valid choice";// make sure they are chosing from your list
exit;
&#125;

if ($category>0 && $category<="5") &#123; //make sure only the selections are used

if ($category=="1") &#123;
$DB_request=="*";
&#125;
if ($category=="2") &#123;
$DB_request=="general";
&#125;
if ($category=="3") &#123;
$DB_request=="microsoft";
&#125;
if ($category=="4") &#123;
$DB_request=="programming";
&#125;
if ($category=="5") &#123;
$DB_request=="helpline";
&#125;
&#125;
The last part is the SQL and you will see how it all ties together.

Code: Select all

$sql="select '$DB_request' from jokes WHERE category='$DB_request' ";

NOTE: I do not know how you have your DB set up, so there is a potential problem when trying to match from the table 'Jokes' to a table that is selected from the drop down such as "microsoft", because if you have a table named microsoft, and than have a table named category, this would be considered a redundent SQL storage issue. (meaning that just having the category table would be enough if you store the jokes in a SQL form, and have them individually within their own category.
CoreLEx
Forum Newbie
Posts: 10
Joined: Sun Jun 01, 2003 5:18 pm

Post by CoreLEx »

All credit to you Michael 01! It's now working as expected.

What I found I had to also do was to append the $category selection to the url and then read it off when the page loads.

Two quick questions though:

1. Is there any way of having a single sql query as such:

Code: Select all

SELECT id,author,title,joke,category,approved FROM jokes WHERE category='$DB_request'
to work when DB_request is either a category value or *. Apparently, the latter does not seem to query properly (even though it executes) and I'm having to include a second one:

Code: Select all

if($DB_request=="*") &#123;
		$sql="SELECT id,author,title,joke,category,approved FROM jokes";
	&#125;
	else &#123;
		$sql="SELECT id,author,title,joke,category,approved FROM jokes WHERE category='$DB_request'";
	&#125;
2. When handling the form on submit, I've got the following code:

Code: Select all

if($_POST&#1111;'submit'])
&#123;
	$url=$PHP_SELF .'?category=' .$category;
	print "<html><head></head></html>";
&#125;
Does that look right? For some reason I feel like the print "..." line looks odd, but it won't work without it.

Thanks again.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

First off, I am glad its working for you. :) It is a little tid bit of code I use for quite a few things, and having the error checking for the variable values this way (1 to 5) is quite a bit more secure than the text method.

If you wanted to, you could keep the category out of the link (URL), and have a added "hidden" input statement in your FORM statment.

Code: Select all

<input type="hidden" name="category"> (something like that...)

Secondly, you could add another drop down list into your HTML page (either rite next to your other drop down, or rite underneath it for only design purpostes) for the first part of your query. Basically, take everything that was posted in above, but make it like this:

Code: Select all

<!--this is your first select area now modified-->

<table summary="" border="0" bgcolor="blue" width="41%">
     <tr>
        <td valign="top">
          <form action="<? echo($PHP_SELF); method="post" ?> 
             <select name="0" value="category">
              <option value="" value="">Select Joke Topic:</option>
                  <option value="" value="">----------------</option> 
                     <option value="1">All</option> 
                      <option value="2">General</option> 
                   <option value="3">Microsoft</option> 
              <option value="4">Programming</option> 
          <option value="5">Helpline</option> 
       </select> 
</form> 
</td>
<!--end first select area-->
<!--Start-just a spacer/add a picture--> 
  <td bgcolor="f1f1f1" width="20%">
           </td>
<!--end spacer-->
<!--start second search by form-->               
              <td width="33%" valign="top">
                    <form name="sorted"> 
                         <select name="0" value="by"> 
                            <option value="" value="">Select By:</option>
                               <option value="" value="">------------</option>
                                      <option value="1">All</option> 
                                       <option value="2">Author</option> 
                                     <option value="3">Title</option>
                                       <option value="4">Joke</option>  
                               
                           </select> </form>
                       </td>
	</tr>
    </table>
<table summary="" border="0" bgcolor="00362" width="41%">
     <tr>
         <td align="center">
             <input type="submit" value="Select A Joke!" name="submit">
                 </td>
                      </tr>
                        </table>
Now, you can see with the second form, I have added a variable "sorted". This will be similar to category of your old code. So, we need to do the same things as we did for category in your .php script. with the sorted area.

Code: Select all

if (empty($sorted)) &#123; 
print "Please select a valid choice";// make sure they are chosing from your list 
exit; 
&#125; 

if ($sorted>0 && $sorted<="4") &#123; //make sure only the selections are used 

if ($sorted=="1") &#123; 
$DB_Sort=="*"; 
&#125; 
if ($sorted=="2") &#123; 
$DB_Sort=="author"; 
&#125; 
if ($sorted=="3") &#123; 
$DB_Sort=="title"; 
&#125; 
if ($sorted=="4") &#123; 
$DB_Sort=="joke"; 
&#125; 
 
&#125;
Than again, the SQL will have to be modified now for the new variables:

Code: Select all

$sql="select '$DB_Sort' from jokes WHERE category='$DB_request' ";
Be sure to read the next post first.....

Try this, and than get back to me. :)
Last edited by Michael 01 on Sun Feb 08, 2004 6:53 pm, edited 2 times in total.
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

Now, i just seen what the problem was with the "all" statment. Take it out of the first select drop down, and your php script for category. Drop the number from 5, to 4 now instead because of the now removed "*" option.

The second drop down select form will take care of the problem. You have to SELECT *......it will not WHERE * and that was the problem. ;)

so, with the second form we have now attached to this code, and by taking out what I just described, it will work perfect.

The option of added hidden input areas is up to you, if you wish to add them. I did not add them in, so you will have to add that to your link just like category.
CoreLEx
Forum Newbie
Posts: 10
Joined: Sun Jun 01, 2003 5:18 pm

Post by CoreLEx »

Just tried it locally and it works beautifully. The SQL query line is also like I wanted it.

Really appreciate the help Michael 01!

take care
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

No problem. Glad it worked for you, and I am glad to help. :)
Post Reply