SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Combine 2 forms into 1

    Hello


    On my main homepage, I have 2 forms.

    The first form only consist of a dropdown box.
    When a user selects, New York and hits the "go" button, it will
    redirect to mymainpage.com/newyork.php

    Code HTML4Strict:
    <form name="navList" onsubmit="return submitForm();">		
    <b>Select your state</b></font><font face="Arial"><font size="2"> </font>
    <select name="subMenu">
    <option value="home"> </option>
    <option value="Washington.php">	Washington DC	</option>
    <option value="newyork.php">New York</option>
    <option value="Alabama.php">	Alabama	</option>
    <option value="Alaska.php">	Alaska	</option>
    <option value="Arizona.php">	Arizona	</option>
    <option value="Arkansas.php">	Arkansas	</option>
    <option value="California.php">	California	</option>
     
    </select><font size="2"> </font>
    </font>
     
     
    <input type="submit" value="Go"><font size="2"> </font>
    </form>

    then inside my newyork.php, there are some information about New York.
    and some db stuff such as
    PHP Code:
    $result mysql_query("SELECT * FROM `table` WHERE `city_state` = 'New York' 

    All that was created a month ago.
    However, just recently, I decided to add in a function where user can select the category that they wish to see.

    So, that is why I have also added another form,
    and this form also consist of only a drop down box.


    PHP Code:
    <form method="POST" action="category.php"><b>Category</b>
    <?php
    //create a single array of options
    $opts = array( 'Automobile''Apparel','Careers','Consumer goods','Education','Electronics','Food and Beverage','Furniture','General Business','Society');

    //  Use here to create your form elements

    echo '<select name="option">' PHP_EOL

    foreach ( 
    $opts as $key=>$val )
    echo 
    '<option value="'.$val '">' $val '</option>' PHP_EOL 

    echo 
    '</select>' PHP_EOL;

    ?>
    <input type="submit" name="submit" value="Go">
    </form>

    Both of the forms works perfectly on THEIR OWN!
    They have their separate functions, and separate "submit" buttons.
    They go to their own separate php pages.

    But, I want to combine both of them into just 1 form, and 1 submit button.

    This is so that the user can choose both drop down box value at once.
    Example, the user can select "New york" AND "Apparel".
    Result: It goes to mymainhomepage.com/newyork.php and shows all the post related to apparel.



    How can I do this ?

    Thanks a BILLION for your generous support and kind assistance.
    May everyone here have a blessed week ahead!

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What is the difference between newyork.php and washington.php?

    Why not make one page which deals out all the information, and use plain HTML forms to send info to that one page?

    Make a single file which contains your options.

    settings.php
    PHP Code:
    <?php

    //create a single array of options

    $cats = array( 'Automobile''Apparel','Careers','Consumer goods','Education','Electronics','Food and Beverage','Furniture','General Business','Society');

    $states = array('Alaska','New York','Washington','Right State' );

    ?>

    The first page:

    nav.php
    PHP Code:
    <?php

    // first use of your arrays
    include 'settings.php' ;

    ?>
    <form name="navList" action="item.php" method="GET">      
    <p>Select your state</p>

    <select name="state">
    <option value="home"> </option>

    <?php

    foreach ( $states as $key=>$state )

    echo 
    '<option value="' $state '">' $state '</option>' PHP_EOL 

    echo 
    '</select>' PHP_EOL;

    ?>

    </select> 

    <p>Pick a category</p>

    <select name="cat">
    <?php

    foreach ( $cats as $key=>$cat )

    echo 
    '<option value="' $cat '">' $cat '</option>' PHP_EOL 

    echo 
    '</select>' PHP_EOL;

    ?>
     
    <input type="submit" value="Go"><font size="2"> </font>
    </form>
    then your form handler:

    item.php
    PHP Code:
    // 2nd use of your arrays
    include 'settings.php' ;

    // some debug
    if( isset( $_GET ) {

    var_dump$_GET );

    }

    // using the states array as a white-list of permitted variable values.

    if( isset( $_GET['state'] && in_array$_GET['state'], $states ) ){

    //The state sent was one of the permitted ones.


    That is untested, but the var_dump on the form handler should show you how your variables are being sent across.

    There are many variations of doing this, like generating the array of states from your database, for example, use the POST method instead of GET and avoid urlencodeing problems, but for the level of question you are asking this example should help you understand how you don't need to use JS to send values around.

    I am not saying don't use JS, you could certainly use it to check that a state was picked.

    Sorry for changing the variable names city/state/option etc, alter them to suit your needs of course.
    Last edited by Cups; May 25, 2009 at 05:19. Reason: typos and a link

  3. #3
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups,
    So happy to see you again.
    What is the difference between newyork.php and washington.php?
    In newyork.php, I have some pictures of New York, some little description of it.
    and in washington.php, I have some pictures of washington, and some description of places to visit and etc.

    thus, I don't think its possible to combine the states into 1
    ------------------------------------------------------------------------


    By the way, I tried your idea as above, and it works perfect!
    but ..... looks like I am loosing my newyork.php static stuffs

    but I like your idea, and i think its the best.
    cause, its all in 1 page.
    which means.... I no longer need to have like 20 over state.php files.
    cause with your idea, I just need 1 .php file.
    COOL

    THANKS Cups

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    But is there a pattern in how you keep your static stuff?

    new_york.jpg
    new_york.txt

    If there isn't then how could you create/manage it?

    You might have an SEO concern about losing newyork.php but we can address that later, or read up on mod_rewrite and .htaccess files or search this forum for those terms.

    Turning requests such as:

    item.php?state=Washington&cat=Clothing

    into URLs like:

    /Washington/Clothing

  5. #5
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    But is there a pattern in how you keep your static stuff?

    new_york.jpg
    new_york.txt

    If there isn't then how could you create/manage it?
    ermm nope, there are no patterns.
    I just type some descriptions into the microsoft frontpage or dreamweaver, and copy paste the picture from a file into the frontpage.

    then save the file as .php, and upload it to the server

    yeah, will have to look into mod_rewrite and .htaccess pretty soon.

  6. #6
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PROBLEM SOLVED by using if else statements

    in the nav.php (main homepage)

    if the user selects NOTHING in the first State drop down box, but selects Apparel in the next drop down box

    it will display NOTHING.
    but in actual fact, there are many post regarding Apparel in my database.

    my SQL codes looks like this

    PHP Code:
    $result mysql_query("SELECT * FROM `table` WHERE `city_state` = '" $_GET['states']. "' AND `category1` = '" $_GET['option'] . "'
    ............. 
    the obvious problem is because when the user selects nothing in the city_state, there are no fields which are empty, and thus it will not be able to display.

    So, even if I have 1000 post regarding Apparel, if can't display because it could not find a city_state which is empty.

    I wanna make it flexible, that if the user selects NOTHING in the first drop down box, but still selects "Apparel" in the 2nd drop down box, he will be able to display ALL the Apparels from all over USA.
    Or if he selects New York AND Apparel, it will show all the New york apparel stuff.
    or if my user selects just New York, and leaves the next box blank, it will show all the new york stuffs.


    So, How do I construct a proper SQL statement ?

    Thanks once again
    Last edited by aaron888; May 25, 2009 at 19:44. Reason: SOLVED

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You create a query builder using conditional code

    PHP Code:
    // create the start of your statement
    $sql "SELECT * FROM `table` WHERE ";

    if( isset( 
    $_GET['states'] ){
    // if a state is set, concatenate it to $sql 
    $sql .= "`city_state` = '" $_GET['states']. "' AND ";

    }
    // then concatenate the end of the sql statement
    $sql .= "`category1` = '" $_GET['option'] . "'";


    // take a look at it, make sure its valid before continuing
    echo $sql 
    easy

    If it does not seem to work then var_dump( $_GET) and take a look what your default selection contains ... and use that in your "if" condition.

  8. #8
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your codes sure is shorter and neater.

    mine was a little messy

    PHP Code:
    // if the user selects both fields.
    if( isset( $_GET['cat']) && in_array$_GET['cat'], $cats )  &&  isset( $_GET['state']) && in_array$_GET['state'], $states ) ){

       
    $result mysql_query("SELECT * FROM `table` WHERE `country` = 'United States' AND `category1` = '" $_GET['cat'] . "' AND `city_state` = '" $_GET['state'] . "' ORDER BY `date` ASC");

    }

    //if the user only selects category
    elseif( isset( $_GET['cat']) && in_array$_GET['cat'], $cats ) ){
       
    $result mysql_query("SELECT * FROM `table` WHERE `country` = 'United States' AND `category1` = '" $_GET['cat'] . "' ORDER BY `date` ASC");

    }

    //if the user only selects state
    elseif( isset( $_GET['state']) && in_array$_GET['state'], $states ) ){
    $result mysql_query("SELECT * FROM `date` WHERE `country` = 'United States' AND `city_state` = '" $_GET['state']. "' ORDER BY `date` ASC");

    }
    else {
        echo 
    "PLEASE Select your state or category";



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •