Forms and PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hugh Janus

    #1

    Forms and PHP

    Hello, I have a huge list of selectives in a database. What I want to do is
    have 3 lists <selectfuncti on in a form. 1 list will have countrys, another
    list will have citys.

    When a country is selected, I want the list of citys to show that is in that
    country, if there is no citys i want it to go straight to the 3rd list. Is
    this possible in php? if so do you have any examples.

    Please ask if a part didnt make sense.

    Thanks
    BW


  • Chenky

    #2
    Re: Forms and PHP

    Hi Hugh,

    My simple answer is no. What you want for this is dynamic script ie
    JavaScript which is relatively simple to do. PHP is server-side and
    doesn't interact with the user unlike JavaScript.

    Now my guess is to use an array for all the cities in one country when
    u select the country and then write that array into the innerHTML of
    the select tag. I'm not too qualified in JavaScript but I dabble and
    that would be my method. I'm sure someone else would be able ot provide
    a simpler solution though...


    Regards,
    Josh

    Comment

    • Chenky

      #3
      Re: Forms and PHP

      Sorry, on second thought you should be able to utilise the database
      cache of cities (if that's what you mean) but I have never connected to
      a database using javascript and I am unsure if that is possible.

      If not, you'll have to cnnect to the database using PHP (if u don't
      know how I can show you) and print the data into an array or variable
      of some kind. Then continue with my previous suggestion.


      Regards,
      Josh

      Comment

      • Chenky

        #4
        Re: Forms and PHP

        Hi Hugh,

        My simple answer is no. What you want for this is dynamic script ie
        JavaScript which is relatively simple to do. PHP is server-side and
        doesn't interact with the user unlike JavaScript.

        Now my guess is to use an array for all the cities in one country when
        u select the country and then write that array into the innerHTML of
        the select tag. I'm not too qualified in JavaScript but I dabble and
        that would be my method. I'm sure someone else would be able ot provide
        a simpler solution though...


        Regards,
        Josh

        Comment

        • Colin Fine

          #5
          Re: Forms and PHP

          Chenky wrote:
          Sorry, on second thought you should be able to utilise the database
          cache of cities (if that's what you mean) but I have never connected to
          a database using javascript and I am unsure if that is possible.
          >
          If not, you'll have to cnnect to the database using PHP (if u don't
          know how I can show you) and print the data into an array or variable
          of some kind. Then continue with my previous suggestion.
          >
          >
          Regards,
          Josh
          >
          Not unless you use AJAX. Javascript is running on the user's machine,
          and knows nothing of the database.

          Colin

          Comment

          • Hugh Janus

            #6
            Re: Forms and PHP


            "Colin Fine" <news@kindness. demon.co.ukwrot e in message
            news:egam40$lfp $3$8302bc10@new s.demon.co.uk.. .
            Chenky wrote:
            >Sorry, on second thought you should be able to utilise the database
            >cache of cities (if that's what you mean) but I have never connected to
            >a database using javascript and I am unsure if that is possible.
            >>
            >If not, you'll have to cnnect to the database using PHP (if u don't
            >know how I can show you) and print the data into an array or variable
            >of some kind. Then continue with my previous suggestion.
            >>
            >>
            >Regards,
            >Josh
            >>
            >
            Not unless you use AJAX. Javascript is running on the user's machine, and
            knows nothing of the database.
            >
            Colin
            Hey, do you have any examples I can look at for AJAX and PHP in a dropdown
            list?

            Thanks
            BW


            Comment

            • Chenky

              #7
              Re: Forms and PHP

              I haven't used AJAX (or heard of it) so Colin's probably more
              knowledgeable at that one.

              Assuming you're using MySQL, a simple way to achieve what you need is
              something like this...


              <select name="selectedc ity">
              <?
              // Next line is name of database
              $db = "mydatabase ";

              // In next line replace localhost with IP address of database server,
              but alot of them are just localhost, and replace username and password
              with the obvious

              $link = @mysql_connect( "localhost" , "username", "password") ;

              if (! $link) {
              die("Error: Couldn't connect to MySQL Database");
              }
              mysql_select_db ($db) or die("Error: Couldn't open MySQL Database");
              // Replace name with the column heading containing the city name
              $query = mysql_query("SE LECT * FROM cities where name!="");

              while ($city = mysql_fetch_arr ay($query)) {
              echo "<option value=" . $city['name'] . ">" . $city['name'];
              }
              ?>
              </select>

              This will print a new...row or thing lol in the drop down list for
              every city listed in the database. Any other help just ask ;)

              Josh

              Comment

              • universalbitmapper

                #8
                Re: Forms and PHP


                Hugh Janus wrote:
                "Colin Fine" <news@kindness. demon.co.ukwrot e in message
                news:egam40$lfp $3$8302bc10@new s.demon.co.uk.. .
                Chenky wrote:
                Sorry, on second thought you should be able to utilise the database
                cache of cities (if that's what you mean) but I have never connected to
                a database using javascript and I am unsure if that is possible.
                >
                If not, you'll have to cnnect to the database using PHP (if u don't
                know how I can show you) and print the data into an array or variable
                of some kind. Then continue with my previous suggestion.
                >
                >
                Regards,
                Josh
                >
                Not unless you use AJAX. Javascript is running on the user's machine, and
                knows nothing of the database.

                Colin
                >
                Hey, do you have any examples I can look at for AJAX and PHP in a dropdown
                list?
                >
                Thanks
                BW
                There is this kind of hierarchy in web programming:

                XHTML->php/mysql -javascript->DOM->XMLHttpReque st

                What you want to do is keep the user on the same page even if he has to
                use the third drop list. That means you have to keep the data available
                to javascript/DOM/XMLHttpRequest.
                AJAX is just a buzzword that encompasses dynamically refreshing pages
                techniques.
                Google search with these keywords + howto, and get going! :-)

                Comment

                • Manuel Lemos

                  #9
                  Re: Forms and PHP

                  Hello,

                  on 10/07/2006 10:37 PM Hugh Janus said the following:
                  Hello, I have a huge list of selectives in a database. What I want to do is
                  have 3 lists <selectfuncti on in a form. 1 list will have countrys, another
                  list will have citys.
                  >
                  When a country is selected, I want the list of citys to show that is in that
                  country, if there is no citys i want it to go straight to the 3rd list. Is
                  this possible in php? if so do you have any examples.
                  What you want can be done using this forms generation class. It comes
                  with a linked select custom input that can switch the options of select
                  input when another input changes.

                  It can switch options from static arrays or retrieve dynamically from a
                  database using AJAX:

                  http://www.phpclasses.org/formsgeneration

                  Here is a working example of the static version:

                  http://www.phpclasses.org/browse/file/9879.html

                  --

                  Regards,
                  Manuel Lemos

                  Metastorage - Data object relational mapping layer generator


                  PHP Classes - Free ready to use OOP components written in PHP
                  http://www.phpclasses.org/

                  Comment

                  Working...