Web Analytics Made Easy -
StatCounter onSelect, display proper fields in textbox - CodingForum

Announcement

Collapse
No announcement yet.

onSelect, display proper fields in textbox

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • onSelect, display proper fields in textbox

    Hi,

    I perform a search and want to display the results in a listbox or another window, on select of each item in my list, I want to change/populate the correspondant fields...which gets the results from the db. Is this possible in php? Can you guide me to a similar example.
    thanks.

  • #2
    Re: onSelect, display proper fields in textbox

    I perform a search and want to display the results in a listbox or another window,...
    well... listbox or another window?

    ...on select of each item in my list, I want to change/populate the correspondant fields...
    With PHP this can not be done without reloading the page with JavaScript, unless
    you store all the data of the results in JavaScript Variables (arrays, for exaple) and then you can display the contents on selecting an item.
    www.united-scripts.com
    www.codebattles.org

    Comment


    • #3
      Hi,

      Yes, I accept with php/javascript, I need to reload the page.
      I can do that by:
      if(isset($_POST['search'])){
      SearchClick(Field1,Field2);
      }

      function SearchClick(){
      $sql = "select Field1,Field2 from Table where Field1 like '%london%'";
      $result = mysql_query($sql);
      }

      In my results, how do I change the data, depending on what I select from my results.

      Comment


      • #4
        j-iN-uK,

        You need to be more specific. What data are you trying to display?

        This is what I'm gathering: you've got a list-box (is it multiselect?), based on the selection, you refresh the page using javscript onChange event, and you query the database with the selection.

        So what EXACTLY do you want to display? Where?

        Sadiq.

        Comment


        • #5
          Hi,

          This is what I'm trying 2 do, there's 2 things which are not working :
          Please find my code below:
          Onchange of the SchoolName, then that school details must display onchange.
          It displays, but when I change it, it adds into the list instead of displaying the data only.adds

          The other problem I'm having is, I wanna display my results in a text box & not a select box.

          <HTML>
          <title>Dynamic List Boxes in PHP</title>

          <BODY>
          <?PHP

          //connect to the database

          mysql_connect("localhost","adri","") or die("server error");
          @mysql_select_db("links_db") or die("database error");

          //declare the form

          echo "<FORM name=f1 action=['PHP_SELF'] method=post>";


          //read the database
          $sql = "select Name, Street, Address, Town, PostCode from Table where Name like '%london%'";
          $result = mysql_query($sql);

          //write the table

          echo "<CENTER><BR><B>Test</B><BR>";
          echo "<TABLE font style='font-family: verdana; font-size: 12; font-weight:700' border=1>";

          echo "<TR><TD valign=\"center\">Name</TD><TD><SELECT NAME=\"LstSchoolName\" SIZE=\"10\" ONCHANGE=\"Schoolselected(this);\" >\n";

          // write the entry code for the javascript...
          // \n is used to force a new line so the resultant code is more readable


          $sJavaScript = "function Schoolselected(elem)
          {\n for (var i = document.f1.PCode.options.length; i >= 0; i--)
          { \n document.f1.PCode.options[i] = null;\n";


          // loop through the database..

          $SName="";
          while ( $row = mysql_fetch_array($result) )
          {
          // is this a new School?

          If ($SName!=$row["SchoolName"]){

          // if yes, add the entry to the SChool's listbox

          $SName = $row["SchoolName"];
          echo "\n<OPTION VALUE='".$row["SchoolName"]."'>".$SName."</OPTION>";

          // and add a new section to the javascript...

          $sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";
          }

          $sJavaScript = $sJavaScript."document.f1.PCode.options[document.f1.PCode.options.length] = new Option('".$row["PostCode"]."','".$row["PostCode"]."');\n";
          $sJavaScript = $sJavaScript."document.f1.txtStreet.options[document.f1.txtStreet.options.length] = new Option('".$row["Street"]."','".$row["Street"]."');\n";
          $sJavaScript = $sJavaScript."document.f1.txtTown.options[document.f1.txtTown.options.length] = new Option('".$row["Town"]."','".$row["Town"]."');\n";

          }

          // finish the Name's listbox
          echo "</SELECT></TD>";

          echo "\n<TD valign=\"center\">Postal</TD><TD><SELECT NAME=\"PCode\" SIZE=10>";
          echo "\n<TD valign=\"center\">Street</TD><TD><SELECT NAME=\"txtStreet\" SIZE=10>";
          echo "\n<TD valign=\"center\">Town</TD><TD><SELECT NAME=\"txtTown\" SIZE=10>";
          echo "\n<TD valign=\"center\">Locality</TD><TD><SELECT NAME=\"txtLocality\" SIZE=10>";
          echo "\n<TD valign=\"center\">Postal</TD><TD><INPUT TYPE='TEXT' NAME=\"PCode\" SIZE=10></TD>";
          echo "<TD width="17%">Locality</td><TD><INPUT type='text' NAME=\"txtLocality\" SIZE=10></TD>";

          echo "</TABLE>";
          // finish the javascript and write out

          $sJavaScript = $sJavaScript."\n}\n}\n";
          echo "\n<SCRIPT LANGUAGE=\"JavaScript\">";
          echo "\n".$sJavaScript."\n</SCRIPT>\n";

          //close the form

          echo "</FORM></center>";

          Comment


          • #6
            I'm sorry, but your code is too unreadable (dispite your comments!)

            Firstly, you need to put all that code in [ php ] [ /php ] tags (no spaces).

            Secondly you need to get rid of all those echoed html/javascript. I can't follow all the braces and quotes. In general, it's much nicer to use php sparingly in your code. See:


            If you manage to clean that up, post it with the [ php ] [ /php ] tags (no spaces), and I'm sure most people will be willing to give you a hand with it -- and future code submissions!

            Sadiq.

            Comment


            • #7
              Hi, hope this code is much clearer. Basically onchange of a Schoolname, I run the javascript to display the proper school fields. I know the problem is in my For statement,
              for (var i = document.f1.PCode.options.length; i >= 0; i--)
              { \n document.f1.PCode.options[i] = null";
              In that statement, I only reference my Postal code, which changes fine when I echo it, how do I include my other fields in the for statement.

              echo "<TR><TD valign=\"center\">School</TD><TD><SELECT NAME=\"LstSchoolName\" SIZE=\"10\" ONCHANGE=\"Schoolselected(this);\" >\n";

              $sJavaScript = "function Schoolselected(elem)

              {\n for (var i = document.f1.PCode.options.length; i >= 0; i--)
              { \n document.f1.PCode.options[i] = null";

              // loop through the database..

              $SName="";
              while ( $row = mysql_fetch_array($result) )
              {
              If ($SName!=$row["SchoolName"]){
              $SName = $row["SchoolName"];
              echo "\n<OPTION VALUE='".$row["SchoolName"]."'>".$SName."</OPTION>";
              $sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){\n";
              }

              $sJavaScript = $sJavaScript."document.f1.PCode.options[document.f1.PCode.options.length] = new Option('".$row["PostCode"]."','".$row["PostCode"]."');\n";
              $sJavaScript = $sJavaScript."document.f1.txtStreet.options[document.f1.txtStreet.options.length] = new Option('".$row["Street"]."','".$row["Street"]."');\n";
              $sJavaScript = $sJavaScript."document.f1.txtTown.options[document.f1.txtTown.options.length] = new Option('".$row["Town"]."','".$row["Town"]."');\n";

              }

              echo "\n<TD valign=\"center\">Postal</TD><TD><SELECT NAME=\"PCode\" SIZE=10>";
              echo "\n<TD valign=\"center\">Street</TD><TD><SELECT NAME=\"txtStreet\" SIZE=10>";
              echo "\n<TD valign=\"center\">Town</TD><TD><SELECT NAME=\"txtTown\" SIZE=10>";

              Comment


              • #8
                Hi,

                I managed to sort out my for statement so that the values dont get repeated.


                $sJavaScript = "function Schoolselected(elem)
                {\n for (var i = document.f1.length; i >= 0; i--)
                \n document.f1.PCode.options[i] = null\n
                \n document.f1.txtStreet.options[i] = null\n}";

                Thant was the resolution. But now I want to display the data in a normal text field, but can't seem to get that to work except in a Select box:


                echo "\n<TD valign=\"center\">Postal</TD><TD><SELECT NAME=\"PCode\" SIZE=10>";
                echo "\n<TD valign=\"center\">Street</TD><TD><SELECT NAME=\"txtStreet\" SIZE=10>";

                that works, but I want to display them in a text box, when I change input type="text", it does not seem to work.

                Comment


                • #9
                  When you say
                  when I change input type="text", it does not seem to work.
                  Can you elaborate on that a little bit? What does happen? Do you get any textboxes? Do you get the right number of textboxes? Do the textboxes have the right names? Are they blank? Do they have values? Should they have values?

                  You have to be more specific.

                  When you merely change select to input type="text", you have to note the difference between a select box and an input box. Select boxes have options, inputs don't. They have a value attribute. If you want the text box to show a value, you have to set the value attribute.

                  I'm not sure what other problems you've got. But try and be as clear and concise with your problems. Number them, like:
                  problem 1
                  problem 2
                  ...

                  That way it's easy to refer to them and answer them.

                  Also, why have you got those \n characters in your code when they're not inside of quotes? I'm getting confused as to why you're showing code that shouldn't work and claiming your problems are something else altogether.

                  This week doesn't seem to want to end...
                  Sadiq.

                  Comment


                  • #10
                    Ok, I'm really sorry for not being clear in my problems. Let's hope this is better.

                    ---------------------------------------------------------------------------
                    Eg1.
                    This is what I have which works fine

                    $sJavaScript = "function Schoolselected(elem)
                    { for (var i = document.f1.length; i >= 0; i--)
                    document.f1.PCode.options[i] = null
                    document.f1.txtStreet.options[i] = null}";

                    echo "<TD valign=\"center\">Postal</TD><TD><SELECT NAME=\"PCode\" SIZE=10>";
                    echo "<TD valign=\"center\">Street</TD><TD><SELECT NAME=\"txtStreet\" SIZE=10>";


                    ----------------------------------------------------------------------------------
                    Eg2.
                    This is what I want, I want to display the values in text boxes and not a list box,
                    when I change it to this, the text box appears but it's blank and my onchange does not work.

                    $sJavaScript = "function Schoolselected(elem)
                    { for (var i = document.f1.length; i >= 0; i--)
                    document.f1.PCode.value[i] = null
                    document.f1.txtStreet.value[i] = null}";

                    echo "<TD valign=\"center\">Postal</TD><TD><INPUT TYPE='TEXT' NAME=\"PCode\" SIZE=10>";
                    echo "<TD valign=\"center\">Street</TD><TD><INPUT TYPE='TEXT' NAME=\"txtStreet\" SIZE=10>";

                    Comment


                    • #11
                      Like I said in my previous post, you're not setting the value attribute.

                      This:
                      PHP Code:
                      echo "<TD valign=\"center\">Postal</TD><TD><INPUT TYPE='TEXT' NAME=\"PCode\" SIZE=10>"
                      Should look more like this:
                      PHP Code:
                      echo "<TD valign=\"center\">Postal</TD><TD><INPUT TYPE='TEXT' NAME=\"PCode\" SIZE=10 VALUE=\"$PCode\">"
                      You're still not putting your code in [ php ] [ /php ] tags, but whatever. That should fix your problems.

                      Sadiq.

                      Comment


                      • #12
                        What he means is you need tags like this:

                        [php]
                        <?php

                        echo 'blah';

                        ?>
                        [/php]

                        which will then look like this when you post your message.

                        PHP Code:
                        <?php

                        echo 'blah';

                        ?>
                        Last edited by missing-score; Feb 20, 2004, 12:58 PM.
                        PHP Weekly - A PHP Developers Resource
                        PHP 5.1.4 and Ruby on Rails web hosting
                        Moderator of PHP and Work offers and Requests
                        Install Apache/PHP/MySQL
                        (by marek_mar) | TinyPlugin Architecture

                        Comment


                        • #13
                          That line of code still does not work, the reason, I'm displaying two fields is to show you that one populates onchange and the other does not. My PCode value gets populated but the txtStreet value does not and this is because its a textbox. I know u did say I must set the value of txtStreet, which I did, I even declared $txtStreet, but it still does not seem to populate.

                          <?PHP

                          $sJavaScript = "function Schoolselected(elem)

                          { for (var i = document.f1.length; i >= 0; i--)
                          { document.f1.PCode.options[i] = null
                          document.f1.txtStreet.value[i] = null";

                          $SName="";
                          while ( $row = mysql_fetch_array($result) )
                          {
                          If ($SName!=$row["SchoolName"]){
                          $SName = $row["SchoolName"];
                          echo "<OPTION VALUE='".$row["SchoolName"]."'>".$SName."</OPTION>";
                          $sJavaScript = $sJavaScript."}"."if (elem.options[elem.selectedIndex].value=='".$row["SchoolName"]."'){";
                          }

                          $sJavaScript = $sJavaScript."document.f1.PCode.options[document.f1.PCode.options.length] = new Option('".$row["PostCode"]."','".$row["PostCode"]."');";
                          $sJavaScript = $sJavaScript."document.f1.txtStreet.value[document.f1.txtStreet.value.length] = new Option('".$row["Street"]."','".$row["Street"]."');";

                          }
                          echo "</SELECT></TD>";

                          if (!empty($HTTP_POST_VARS["txtStreet"]))
                          {
                          $txtStreet = $HTTP_POST_VARS["txtStreet"];
                          }

                          echo "<TD valign=\"center\">Street</TD><TD><INPUT TYPE='TEXT' NAME=\"txtStreet\" SIZE=10 VALUE=\"$txtStreet\">";
                          echo "<td width=\"83%\"><SELECT NAME=PCode size=1></td>";

                          ?>

                          Comment


                          • #14
                            Try this out for debugging and see what you come up with. It may help you to narrow down your problem.

                            Replace:
                            PHP Code:
                            if (!empty($HTTP_POST_VARS["txtStreet"]))

                            $txtStreet $HTTP_POST_VARS["txtStreet"];

                            With:
                            PHP Code:
                            if (!empty($HTTP_POST_VARS["txtStreet"]))

                            $txtStreet $HTTP_POST_VARS["txtStreet"];
                            }
                            else
                            {
                            $txtStreet "EMPTY!!!";

                            Reading that code may also make you think: should txtStreet be in $HTTP_POST_VARS? What does that imply? Does that mean that txtStreet is not coming from the database? Should it be coming from the database?

                            Also, this bit of code of yours looks kinda funny:
                            PHP Code:
                            $sJavaScript $sJavaScript."document.f1.txtStreet.value[document.f1.txtStreet.value.length] = new Option('".$row["Street"]."','".$row["Street"]."');"
                            The reason being is that you're creating a new Option. Options are only for select boxes -- NOT for text boxes. Also it looks like you're referring to street with $row["Street"] -- so what is $txtStreet? Where did that come from?

                            These questions I'm posing are not for me (ie. I don't care about the answers), but for you. Hopefully they will guide you to solving this problem of yours. Study your code.

                            Hope that helps,
                            Sadiq.

                            Comment


                            • #15
                              hi j-iN-uK

                              PLEASE could you use the [php][/php] formatting tags as shown in my example above.
                              PHP Weekly - A PHP Developers Resource
                              PHP 5.1.4 and Ruby on Rails web hosting
                              Moderator of PHP and Work offers and Requests
                              Install Apache/PHP/MySQL
                              (by marek_mar) | TinyPlugin Architecture

                              Comment

                              Working...
                              X