combo box / text box php mysql

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phill Long

    combo box / text box php mysql

    this is the the code, now here is the final result.... I get one combo box
    and
    one tex box come up, but they are empty...
    DAMN!!!
    Any ideas on what Im doing wrong please..
    Thanks Again

    <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>

    <?
    function select_cases_pr ice ()
    {
    $qy = "SELECT SellingPrice, Description FROM cases";
    $rs = mysql_query ($qy) or die (mysql_error ());
    while (mysql_fetch_ar ray ($rs))
    { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
    $prices[] = $r{1};
    }
    return array ($select, $prices);
    }
    ?>

    On your web page, call the function.

    <? list ($select, $prices) = select_cases_pr ice (); ?> And a bit of JS:
    <script>
    function showPrice() {
    array prices (<? echo $prices ?>);
    var i = document.form.i tem.selectedInd ex; document.form.S ellingPrice =
    prices[i]; </script>

    <select name="Descripti on" onUpdate="showP rice()"> <? echo $select; ?>
    </select> <input type="text" name="SellingPr ice" value="">


  • Pedro Graca

    #2
    Re: combo box / text box php mysql

    Phill Long wrote:[color=blue]
    ><?
    > function select_cases_pr ice ()
    > {[/color]

    // I like to initialize variables
    $prices = array();
    $select = '';

    [color=blue]
    > $qy = "SELECT SellingPrice, Description FROM cases";
    > $rs = mysql_query ($qy) or die (mysql_error ());
    > while (mysql_fetch_ar ray ($rs))[/color]

    while ($r = mysql_fetch_arr ay ($rs))
    // ____^^^^^______ _______________ ___

    [color=blue]
    > { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
    > $prices[] = $r{1};[/color]

    // ?? __________^_^_ is this ok?
    // shouldn't it be
    $prices[] = $r[1];
    // ... I didn't test it with curly braces

    [color=blue]
    > }
    > return array ($select, $prices);[/color]

    // $prices is an array

    [color=blue]
    > }
    > ?>
    >
    > On your web page, call the function.
    >
    ><? list ($select, $prices) = select_cases_pr ice (); ?> And a bit of JS:[/color]

    // $prices is an array!

    [color=blue]
    ><script>
    > function showPrice() {
    > array prices (<? echo $prices ?>);[/color]

    // same as
    array prices (Array);
    // probably wrong I think :-)

    (snip)



    Happy bug hunting :-)
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Pedro Graca

      #3
      Re: combo box / text box php mysql

      Pedro Graca wrote:[color=blue][color=green]
      >><script>
      >> function showPrice() {
      >> array prices (<? echo $prices ?>);[/color]
      >
      > // same as
      > array prices (Array);
      > // probably wrong I think :-)[/color]

      Maybe imploding the array is enough?

      array prices (<?php echo implode(', ', $prices); ?>);
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Phill Long

        #4
        still stuck

        Did the things requested, but still not working, any ideas??
        I get the combo box to read all the case details, but I just cant get the
        thing to put a price in the little text box.. thats all I ask, please help
        me..
        here is a link to the site..
        :http://djsleepy.servebeer.com/all%5F...jsleepy/_test/

        <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>

        <?
        function select_cases_pr ice ()
        {
        $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
        $rs = mysql_query ($qy) or die (mysql_error ());
        while ($r=mysql_fetch _array ($rs))
        { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
        $prices[] = $r{1};
        }
        return array ($select, $prices);
        }
        ?>

        On your web page, call the function.

        <? list ($select, $prices) = select_cases_pr ice (); ?>

        And a bit of JS:

        <script>
        function showPrice()
        {
        array prices (<? echo $prices ?>);
        var i = document.form.i tem.selectedInd ex; document.form.S ellingPrice =
        prices[i];
        </script>

        <select name="Descripti on" onUpdate="showP rice()"> <? echo $select; ?>
        </select> <input type="text" name="SellingPr ice" value="">


        Comment

        • Pedro Graca

          #5
          Re: still stuck

          Phill Long wrote:[color=blue]
          > Did the things requested, but still not working, any ideas??[/color]
          (snip)[color=blue]
          > And a bit of JS:
          >
          ><script>
          > function showPrice()
          > {
          > array prices (<? echo $prices ?>);[/color]

          Still printing "Array" inside the JS? :)

          See the source of your page and you'll see

          <script>
          function showPrice()
          {
          array prices (Array);

          which, obviously (though I don't know JavaScript), is wrong.

          Try the implode(', ', $prices) I told you on my last post. I expect the
          JS to become

          <script>
          function showPrice()
          {
          array prices (4.5, 126.33, 88, 0.3);
          --
          --= my mail box only accepts =--
          --= Content-Type: text/plain =--
          --= Size below 10001 bytes =--

          Comment

          • Shawn Wilson

            #6
            Re: still stuck

            Phill Long wrote:[color=blue]
            >
            > Did the things requested, but still not working, any ideas??
            > I get the combo box to read all the case details, but I just cant get the
            > thing to put a price in the little text box.. thats all I ask, please help
            > me..
            > here is a link to the site..
            > :http://djsleepy.servebeer.com/all%5F...jsleepy/_test/
            >
            > <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>
            >
            > <?
            > function select_cases_pr ice ()
            > {
            > $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
            > $rs = mysql_query ($qy) or die (mysql_error ());
            > while ($r=mysql_fetch _array ($rs))
            > { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
            > $prices[] = $r{1};
            > }
            > return array ($select, $prices);
            > }
            > ?>[/color]

            There were a few errors in the JS. Use the implode() thing Pedro mentioned.
            The code below is tested on IE5/Win98:

            <script>
            function showPrice()
            {
            var prices = new Array(100, 110, 120, 130, 140,150);
            var i = document.forms[0].Description.se lectedIndex;
            document.forms[0].SellingPrice.v alue = prices[i];
            }
            </script>
            <form>
            <select name="Descripti on" onChange="showP rice()"> <option value='Thermalt ake
            Lanfire Black ATX Case With Side Window No PSU'>Thermaltak e Lanfire Black ATX
            Case With Side Window No PSU</option>
            <option value='Thermalt ake Lanfire Silver ATX Case With Side Window No
            PSU'>Thermaltak e Lanfire Silver ATX Case With Side Window No PSU</option>
            <option value='Thermalt ake Screwless Skull Atx Case in Black No PSU'>Thermaltak e
            Screwless Skull Atx Case in Black No PSU</option>
            <option value='Thermalt ake Xaser III Supertower Black ATX Case With Window No
            PSU'>Thermaltak e Xaser III Supertower Black ATX Case With Window No PSU</option>
            <option value='ATX Midi Tower Case 350w PSU in Black'>ATX Midi Tower Case 350w
            PSU in Black</option>
            <option value='ATX Case In Silver With Front USB/Audio/1394'>ATX Case In Silver
            With Front USB/Audio/1394</option>
            </select>

            <input type="text" name="SellingPr ice" value="">
            </form>

            Notes:
            Use implode or join to print out the array so it looks like the JS code above.
            I changed the onUpdate() handler to onChange.
            "item" in the original should be "Descriptio n".
            You need <FORM> tags if you're going to reference using document.forms. ..etc.
            If this isn't the first form, you'll have to change forms[0] to whatever.
            You might want to put onLoad="showPri ce()" in the body tag to set the intial
            price

            Regards,
            Shawn
            --
            Shawn Wilson
            shawn@glassgian t.com

            Comment

            • Phill Long

              #7
              Stuck Again

              Yet again I have ammended the code to suit what has been suggested, but
              still cant get that ole text box to display the price.. HELP ME PLEASE!

              <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>
              <body onLoad="showPri ce()">
              </body>
              <?
              function select_cases_pr ice ()
              {
              $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
              $rs = mysql_query ($qy) or die (mysql_error ());
              while ($r=mysql_fetch _array ($rs))
              { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
              $prices[] = $r[1];
              }
              return array ($select, $prices);
              }
              ?>

              On your web page, call the function.

              <? list ($select, $prices) = select_cases_pr ice (); ?>

              And a bit of JS:

              <script>
              function showPrice()
              {
              var prices = new Array(100, 110, 120, 130, 140,150);
              var i = document.forms[0].Description.se lectedIndex;
              document.forms[0].SellingPrice.v alue = prices[i];
              }
              </script>


              <select name="Descripti on" onChange="showP rice()"> <? echo $select; ?>
              </select> <input type="text" name="SellingPr ice" value="">


              Comment

              • Rahul Anand

                #8
                Re: Stuck Again

                "Phill Long" <djsleepy@djsle epy.co.uk> wrote in message news:<c0ecm5$6v g$1@hercules.bt internet.com>.. .[color=blue]
                > Yet again I have ammended the code to suit what has been suggested, but
                > still cant get that ole text box to display the price.. HELP ME PLEASE!
                >
                > <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>
                > <body onLoad="showPri ce()">
                > </body>
                > <?
                > function select_cases_pr ice ()
                > {
                > $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
                > $rs = mysql_query ($qy) or die (mysql_error ());
                > while ($r=mysql_fetch _array ($rs))
                > { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
                > $prices[] = $r[1];
                > }
                > return array ($select, $prices);
                > }
                > ?>
                >
                > On your web page, call the function.
                >
                > <? list ($select, $prices) = select_cases_pr ice (); ?>
                >
                > And a bit of JS:
                >
                > <script>
                > function showPrice()
                > {
                > var prices = new Array(100, 110, 120, 130, 140,150);
                > var i = document.forms[0].Description.se lectedIndex;
                > document.forms[0].SellingPrice.v alue = prices[i];
                > }
                > </script>
                >
                >
                > <select name="Descripti on" onChange="showP rice()"> <? echo $select; ?>
                > </select> <input type="text" name="SellingPr ice" value="">[/color]

                I checked your previous mail and the URL you have posted.
                You are not using any *form* tags to enclose your *select-box* and *text-box*

                Use form tags, your page will work fine.

                Hope it will help.

                --
                Cheers,
                Rahul Anand

                Comment

                • Shawn Wilson

                  #9
                  Re: Stuck Again

                  Phill Long wrote:[color=blue]
                  >
                  > Yet again I have ammended the code to suit what has been suggested, but
                  > still cant get that ole text box to display the price.. HELP ME PLEASE!
                  >
                  > <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>
                  > <body onLoad="showPri ce()">
                  > </body>
                  > <?
                  > function select_cases_pr ice ()
                  > {
                  > $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
                  > $rs = mysql_query ($qy) or die (mysql_error ());
                  > while ($r=mysql_fetch _array ($rs))
                  > { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
                  > $prices[] = $r[1];
                  > }
                  > return array ($select, $prices);
                  > }
                  > ?>
                  >
                  > On your web page, call the function.
                  >
                  > <? list ($select, $prices) = select_cases_pr ice (); ?>
                  >
                  > And a bit of JS:
                  >
                  > <script>
                  > function showPrice()
                  > {
                  > var prices = new Array(100, 110, 120, 130, 140,150);
                  > var i = document.forms[0].Description.se lectedIndex;
                  > document.forms[0].SellingPrice.v alue = prices[i];
                  > }
                  > </script>
                  >
                  > <select name="Descripti on" onChange="showP rice()"> <? echo $select; ?>
                  > </select> <input type="text" name="SellingPr ice" value="">[/color]

                  Yes, what Rahul said. And when you're working with Javascript, always put it in
                  a proper HTML document. That means, put in <HTML> tags, put your select and
                  option tags within <FORM> tags, and don't close your <BODY> before you start
                  your form. If you insist on taking these kind of shortcuts you're only going to
                  end up frustrating yourself.
                  --
                  Shawn Wilson
                  shawn@glassgian t.com

                  Comment

                  • Phill Long

                    #10
                    Working!!! *But Not Quite

                    Ok I did all that re-coding and the page works fine apart from I want the
                    array of prices to come from the mysql table. I dont want to have to enter
                    them. as I am going to use this for quite a few products.. can this be
                    done..

                    Thanks in Advance Guys, u've been brilliant so far!


                    "Shawn Wilson" <shawn@glassgia nt.com> wrote in message
                    news:402B887A.A 027C8F0@glassgi ant.com...[color=blue]
                    > Phill Long wrote:[color=green]
                    > >
                    > > Yet again I have ammended the code to suit what has been suggested, but
                    > > still cant get that ole text box to display the price.. HELP ME PLEASE!
                    > >
                    > > <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p');[/color][/color]
                    ?>[color=blue][color=green]
                    > > <body onLoad="showPri ce()">
                    > > </body>
                    > > <?
                    > > function select_cases_pr ice ()
                    > > {
                    > > $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
                    > > $rs = mysql_query ($qy) or die (mysql_error ());
                    > > while ($r=mysql_fetch _array ($rs))
                    > > { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
                    > > $prices[] = $r[1];
                    > > }
                    > > return array ($select, $prices);
                    > > }
                    > > ?>
                    > >
                    > > On your web page, call the function.
                    > >
                    > > <? list ($select, $prices) = select_cases_pr ice (); ?>
                    > >
                    > > And a bit of JS:
                    > >
                    > > <script>
                    > > function showPrice()
                    > > {
                    > > var prices = new Array(100, 110, 120, 130, 140,150);
                    > > var i = document.forms[0].Description.se lectedIndex;
                    > > document.forms[0].SellingPrice.v alue = prices[i];
                    > > }
                    > > </script>
                    > >
                    > > <select name="Descripti on" onChange="showP rice()"> <? echo $select; ?>
                    > > </select> <input type="text" name="SellingPr ice" value="">[/color]
                    >
                    > Yes, what Rahul said. And when you're working with Javascript, always put[/color]
                    it in[color=blue]
                    > a proper HTML document. That means, put in <HTML> tags, put your select[/color]
                    and[color=blue]
                    > option tags within <FORM> tags, and don't close your <BODY> before you[/color]
                    start[color=blue]
                    > your form. If you insist on taking these kind of shortcuts you're only[/color]
                    going to[color=blue]
                    > end up frustrating yourself.
                    > --
                    > Shawn Wilson
                    > shawn@glassgian t.com
                    > http://www.glassgiant.com[/color]


                    Comment

                    • Phill Long

                      #11
                      &quot;THE CODE&quot;

                      <?php require_once('. .\library_datab ase\Connections \onlinequote.ph p'); ?>

                      <html>
                      <body onLoad="showPri ce()">

                      <?
                      function select_cases_pr ice ()
                      {
                      $qy = "SELECT Description, SellingPrice FROM onlinequote.cas es";
                      $rs = mysql_query ($qy) or die (mysql_error ());
                      while ($r=mysql_fetch _array ($rs))
                      { $select .= "<option value='{$r[0]}'>{$r[0]}</option>\n";
                      $prices[] = $r[1];
                      }
                      return array ($select, $prices);
                      }
                      ?>

                      On your web page, call the function.

                      <? list ($select, $prices) = select_cases_pr ice (); ?>

                      And a bit of JS:

                      <script>
                      function showPrice()
                      {
                      var prices = new Array(120,130,1 10,120,120,110) ;
                      var i = document.forms[0].Description.se lectedIndex;
                      document.forms[0].SellingPrice.v alue = prices[i];
                      }
                      </script>

                      <form>
                      <select name="Descripti on" onChange="showP rice()"><? echo $select;
                      ?></select>
                      <input type="text" name="SellingPr ice" value="">
                      </form>

                      </body>
                      </html>


                      Comment

                      • Pedro Graca

                        #12
                        Re: &quot;THE CODE&quot;

                        Phill Long wrote:[color=blue]
                        > <script>
                        > function showPrice()
                        > {
                        > var prices = new Array(120,130,1 10,120,120,110) ;[/color]

                        You once almost had this right!
                        The prices come from the DB, they are not constants.

                        You had something like
                        var prices = new Array(<?php echo $prices; ?>);

                        and, as $prices is an array that didn't work.

                        You have to transform the array of prices into a string of values.

                        [code working="false"]
                        <?php echo $prices; ?>
                        [/code]
                        outputs "Array"

                        [code working="false" better="true"]
                        <?php
                        foreach ($prices as $value) {
                        echo "$value,";
                        }
                        ?>
                        [/code]
                        outputs "120,130,110,12 0,120,110," with the final extra comma

                        but
                        [code working="true"]
                        <?php echo implode(",", $prices); ?>
                        [/code]
                        outputs "120,130,110,12 0,120,110" right the way you want :)
                        --
                        --= my mail box only accepts =--
                        --= Content-Type: text/plain =--
                        --= Size below 10001 bytes =--

                        Comment

                        • Phill Long

                          #13
                          Re: &quot;THE CODE&quot;

                          nearly right.. Sorry

                          this displays the price as 16.2, if the price is 16.20... But is there a way
                          to display the whole value (e.g 16.20) ??????
                          "Pedro Graca" <hexkid@hotpop. com> wrote in message
                          news:c0ght6$16t bl1$1@ID-203069.news.uni-berlin.de...[color=blue]
                          > Phill Long wrote:[color=green]
                          > > <script>
                          > > function showPrice()
                          > > {
                          > > var prices = new Array(120,130,1 10,120,120,110) ;[/color]
                          >
                          > You once almost had this right!
                          > The prices come from the DB, they are not constants.
                          >
                          > You had something like
                          > var prices = new Array(<?php echo $prices; ?>);
                          >
                          > and, as $prices is an array that didn't work.
                          >
                          > You have to transform the array of prices into a string of values.
                          >
                          > [code working="false"]
                          > <?php echo $prices; ?>
                          > [/code]
                          > outputs "Array"
                          >
                          > [code working="false" better="true"]
                          > <?php
                          > foreach ($prices as $value) {
                          > echo "$value,";
                          > }
                          > ?>
                          > [/code]
                          > outputs "120,130,110,12 0,120,110," with the final extra comma
                          >
                          > but
                          > [code working="true"]
                          > <?php echo implode(",", $prices); ?>
                          > [/code]
                          > outputs "120,130,110,12 0,120,110" right the way you want :)
                          > --
                          > --= my mail box only accepts =--
                          > --= Content-Type: text/plain =--
                          > --= Size below 10001 bytes =--[/color]


                          Comment

                          • Pedro Graca

                            #14
                            Re: &quot;THE CODE&quot;

                            Phill Long wrote:[color=blue]
                            > nearly right.. Sorry
                            >
                            > this displays the price as 16.2, if the price is 16.20... But is there a way
                            > to display the whole value (e.g 16.20) ??????[/color]

                            You're printing the price in JavaScript. I don't know that.

                            If it were PHP

                            <?php echo number_format(1 6.2, 2); ?>

                            outputs 16.20.

                            You might try to replace the implode() thing with another method that
                            writes the JavaScript array as an array of strings
                            formatted the way you want -- left as an exercise

                            or

                            .... do it in JavaScript
                            --
                            --= my mail box only accepts =--
                            --= Content-Type: text/plain =--
                            --= Size below 10001 bytes =--

                            Comment

                            • Phill Long

                              #15
                              Re: &quot;THE CODE&quot;

                              but
                              [code working="true"]
                              <?php echo implode(",", $prices); ?>
                              [/code]
                              outputs "120,130,110,12 0,120,110" right the way you want :)

                              I mean the price from the mysql database is 16.20, and the webpage displays
                              16.2 from this code... So how do I alter this code to make it display 16.20
                              on the page


                              Comment

                              Working...