Author Topic: can i use a drop down list without a submit button  (Read 1186 times)

dmcb

  • New Member
  • *
  • Posts: 16
  • Karma: 0
    • View Profile
can i use a drop down list without a submit button
« on: March 07, 2012, 04:40:23 PM »
Hi i am using a dropdown list to run different queries. the user selects an option from the list and then hits a submit button. i was just wondering if i could get the dropdown list to work without having to use the submit button? is there an easy to edit my code to acheive this ?

this is the some of the code i am using



<center><form action="films6.php" method="POST">
<center>Browes Films: <select name="category"></center>
<option value="Action" selected="selected">Action</option>
<option value="Comedy">Comedy</option>
<option value="Drama">Drama</option>
<option value="Thiller">Thiller</option>
<option value="Added">New Releases</option>
<option value="ScFi">ScFi</option>
</select>
<input type="text" name="search_name" size="10" />
<input type="Submit" value="Search" name="search" />
</form></center>



PHP Code: [Select]

if( isset( $_POST['search'] ) ){

   
$dropdownValue $_POST['category'];
	
	

   switch( 
$dropdownValue ){

case 
"Action" $query "SELECT name,description,image FROM movies WHERE genre ='Action'   ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Comedy" $query "SELECT name,description,image FROM movies WHERE genre ='Comedy' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Drama" :  $query "SELECT name,description,image FROM movies WHERE genre ='Drama' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Thiller"$query "SELECT name,description,image FROM movies WHERE genre ='Thiller' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Added" :  $query "SELECT name,description,image FROM movies ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"ScFi" :   $query "SELECT name,description,image FROM movies WHERE genre ='SC-FI' ORDER BY id DESC LIMIT $start,$per_page"; break;
default : die( 
"You have entered something that isn't an option in my dropdown" );

        }

        }
	
	

	
	

	
	

	
	
$query_run mysql_query($query);


ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1864
  • Karma: 32
    • View Profile
Re: can i use a drop down list without a submit button
« Reply #1 on: March 07, 2012, 05:29:55 PM »
Sure, you can add onchange=categorychanged();  to the SELECT and then set up a Javascript FUNCTION to handle whatever you want it to.

  The issue is that normally with PHP you POST a form to a second page which handles the data that was changed or entered in the FORM page.  So, without a submit button, you are not "posting" your entries in the form to a PHP program.  Some people try to use on-form PHP code and just post to yourself.  But, this causes all kinds of problems due to PHP really being SERVER-SIDE only software.  Not designed for CLIENT-SIDE.  (It can work, but, I do not suggest it.  Unless you are using Javascript to LOAD PHP pages into iFrames and then using the data.  This works well because the PHP will run SERVER-SIDE and fill data to be accessed by Javascript routines.)

  In Javascript, it is very easy to set up a function to use the drop-downs and read the data in it and do whatever you wish.  Remember the PHP code you showed runs server-side and can not be run in the browser the way you have it listed.  It is run on the server then, rendered to the browser.  You should look at Javascript for this.  Ask any questions and we can help.  (I'm gone for a few hours...)

dmcb

  • New Member
  • *
  • Posts: 16
  • Karma: 0
    • View Profile
Re: can i use a drop down list without a submit button
« Reply #2 on: March 07, 2012, 06:05:52 PM »
Thanks for reply, i will try the javascript way myself, but  do you have  a simple example i could go use or can you edit my code , i'm just not to sure how to go about it. whenever you have some spare time there's no rush thanks

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1864
  • Karma: 32
    • View Profile
Re: can i use a drop down list without a submit button
« Reply #3 on: March 07, 2012, 06:10:04 PM »
Sorry, I am leaving now for a few hours...  Here is a link that might help.
Select the DEMONSTRATION to see how it works.  It is not a drop-down, but does the same thing.
It allows a CHANGE and then makes something happen based on it.  It gives a nice effect.
I have it working in a drop-down somewhere and can help tomorrow or late tonight.

Good luck...    http://remysharp.com/2007/09/18/auto-populate-multiple-select-boxes/

dmcb

  • New Member
  • *
  • Posts: 16
  • Karma: 0
    • View Profile
Re: can i use a drop down list without a submit button
« Reply #4 on: March 07, 2012, 07:36:00 PM »
great thanks for help, i'll try tomorrow.

dmcb

  • New Member
  • *
  • Posts: 16
  • Karma: 0
    • View Profile
Re: can i use a drop down list without a submit button
« Reply #5 on: March 08, 2012, 03:24:34 PM »
Got it working very simple actually i just added the javascript onchange function to the select tag. as follows

Code: [Select]
<center><form action="films5.php" method="POST">
<center>Browes Films: <select name="category"[color=red]onchange='this.form.submit()'[/color] ></center>
<option selected="selected">Choose a Genre...</option>
<option value="Added">All</option>
<option value="Action">Action</option>
<option value="Comedy">Comedy</option>
<option value="Drama">Drama</option>
<option value="Thiller">Thiller</option>
<option value="ScFi">ScFi</option>
</select>
</form>
<form action="films3.php" method="POST">
<input type="text" name="search_name" size="10" />
<input type="Submit" value="Search" name="search" />
</form></center>

Then i changed the isset function to check the select tag, (name="catergory") as folllows

PHP Code: [Select]


[color=red]if( isset( $_POST['category'] )[/color] ){

$dropdownValue $_POST['category'];
	
	

switch( 
$dropdownValue ){

case 
"Action" $query "SELECT name,description,image FROM movies WHERE genre ='Action' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Comedy" $query "SELECT name,description,image FROM movies WHERE genre ='Comedy' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Drama" :  $query "SELECT name,description,image FROM movies WHERE genre ='Drama' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Thiller"$query "SELECT name,description,image FROM movies WHERE genre ='Thiller' ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"Added" :  $query "SELECT name,description,image FROM movies ORDER BY id DESC LIMIT $start,$per_page"; break;
case 
"ScFi" :   $query "SELECT name,description,image FROM movies WHERE genre ='SC-FI' ORDER BY id DESC LIMIT $start,$per_page"; break;
        default : die( 
"You have entered something that isn't an option in my dropdown" );

        }

        }

ErnieAlex

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 1864
  • Karma: 32
    • View Profile
Re: can i use a drop down list without a submit button
« Reply #6 on: March 08, 2012, 05:58:21 PM »
Great!  Always nice to complete a project...