www.webdeveloper.com
Results 1 to 7 of 7

Thread: Newbie Question.... Help Please!!

  1. #1
    Join Date
    Sep 2009
    Posts
    5

    Newbie Question.... Help Please!!

    I am new to PHP and am working on a personal community website in which i have everything working perfectly fine except for my search script. Which i am pretty sure most of you are going to rag on me about what i am about to ask but im going for it anyways.....

    I am trying to set up my search so that people come in and select from multiple drop down boxes for what they want to search for. I want to use the $_Get method to do this but i can not for the life of me figure out how to set it up so that what they select from the form gets passed onto the script and also how to set up the script to get the data i want....

    So basically im stumped and need serious help..... Anyone !?!?!?

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    In HTML you name all form fields with a name. If you submit using the GET method, each name in PHP ends up as the key name in the associative array $_GET, i.e. as $_GET['name']

    Simple example:

    HTML Code:
    <select name="search_item1">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="2">Three</option>
    </select>
    $_GET['search_item1'] = whatever value user selected on submit (i.e. "1")

    -jim

  3. #3
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    As a followup, a common way to handle PHP form submission like this, pretend you created page.php as follows:

    PHP Code:
    <form action="page.php">
    ...form elements...
    ...submit button with name="submit"...
    </form>

    <?php

    if (isset($_GET['submit'])) {

    ... 
    process form data stored in $_GET ...

    }

    ?>
    So once the user submits, the action loads the same page (you can use page name or $PHP_SELF among other things) and just test to see if the submit button was pressed and process the form if true.

    There are a million ways to do this, i.e. some folks put the form inside an else condition. Some folks like to use a hidden form field to test for submit and add a little security (although there is none in GET vs. POST) and of course you can put the form at the bottom and submit at the top if you wish. The above is just a simple, simple example to demo the concept.

    -jim

  4. #4
    Join Date
    Sep 2009
    Posts
    5
    OK that sounds easy enough, But can i ask another stupid question... What about using multiple drop downs? how do i set up the array for that... Also do you know of any tutorials that would help me out with this??

    Code:
    $_GET['search_item1'] = 1
    $_GET['search_item2'] = 1

  5. #5
    Join Date
    Sep 2009
    Posts
    5
    Quote Originally Posted by SrWebDeveloper View Post
    As a followup, a common way to handle PHP form submission like this, pretend you created page.php as follows:

    PHP Code:
    <form action="page.php">
    ...form elements...
    ...submit button with name="submit"...
    </form>

    <?php

    if (isset($_GET['submit'])) {

    ... 
    process form data stored in $_GET ...

    }

    ?>
    So once the user submits, the action loads the same page (you can use page name or $PHP_SELF among other things) and just test to see if the submit button was pressed and process the form if true.

    There are a million ways to do this, i.e. some folks put the form inside an else condition. Some folks like to use a hidden form field to test for submit and add a little security (although there is none in GET vs. POST) and of course you can put the form at the bottom and submit at the top if you wish. The above is just a simple, simple example to demo the concept.

    -jim

    So your saying that it would be more secure if i used the POST method vs GET????

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    A few key differences between POST and GET are generally:

    POST

    • Form data sent inside the message body
    • Virtually no restriction on length
    • Not usually cached by any server or by any web browser

    GET

    • Form data encoded into the URL
    • Maximum URL length of around 2000 characters
    • Usually cached by web browsers

    Clearly those are not the only differences.

    Other advantages/disadvantages outline in this nice blog someone wrote.

    With search engines it's very common to use GET due to the short fields typical in search forms versus large data fields used for message content or whatever. One thing I like to pass on about GET is the form action cannot include any URL arguments, i.e. "/page.php" ok, but "/page.php?x=2" not, so if you purposefully want the URL to be updated with a custom argument/value pair not part of the form shown to the user, simply use a hidden field to add that pair, i.e.

    <input type="hidden" name="x" value="2">

    You might want to do this to add formatting options which your search engine RESULT PAGE would need, for example, but you don't want the user to control with a form field they can see. That's the concept. In POST you can put the pair in the form action OR create a hidden field, either one.

    Obviously with POST the browser URL doesn't show all the fieldnames and their values, and if you want real security with form submission you use SSL data encryption or similar.

    I am only mentioning the basics here.

    As to your other question PHP and select (dropdown):

    1) If the user can only select ONE item - use the method I discussed earlier, the form name in the select tag becomes the key name in $_GET and the value comes from the option listing.

    2) If you allow multi-select, that's different. In the select tag, append [] to the name attribute, i.e. name="dropdown_choice[]"

    In PHP, any array element may contain a value OR another array. So when the user submits the form, using the above example, it ends up as $_GET['dropdown_choice'] and you can access the array like this:

    PHP Code:
        $choices=$_POST['dropdown_choice'];
        if (
    $choices){
         foreach (
    $choices as $mychoice){echo 'You selected ',$mychoice,'<br />';}
        } 
    There are many ways to walk through arrays, the above is a simple example using foreach, which is pretty easy and common in PHP.
    Have fun!

    -jim

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    POST should be used if the request is going to cause some effect: change a database value, place an order for a product, send an email, etc. GET should be used if there is no such effect and instead just affects what gets displayed: a site search, selecting which inventory item to display, view a blog post, etc. One exception for GET would be if you need the request to contain more data than can be reasonably expected to be contained in a URL query string*, or if you need to submit non-ASCII characters in the form data, in which case you'll need to use POST.

    See http://www.w3.org/TR/html401/interac...#submit-format for more info.
    ____________
    * There is no official size limit. Most newer browsers support URLs of 2kb or more, but you cannot count on all users having that large a capability.
    Last edited by NogDog; 09-03-2009 at 01:24 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles