#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Australia
    Posts
    33
    Rep Power
    13

    php,javascript + logic


    what i want to do is create a dropdown box with fields from mysql and when the selection is changed the price get auto added to the field called price

    it looks like this

    PHP Code:
    <?
    $sql 
    mysql_query("Select * from item") or die("error in sql " mysql_error());
    ?>

    <form>
    <select name="products" onchange="selectprice(this.form)">
    <option name="0">select a product</option>
    <?
    while ($row mysql_fetch_array($sql))
    {
    $price $row["products_costprice"];
    ?>
    <script>
    function selectprice(theForm)
    {

        theForm.price.value = '<? print $price?>';

    }
    </script>
    <option name="<?=$row["id"]?>"><?=$row["itemcode"]?></option>
    <?
    }
    ?>
    </select><br><br>
    <input type="text" name="price">
    </form>
    this all work fine but it displays the wrong price i believe this is because i need know what product id has been selected from the item list.

    looking at it ,it looks like i need to echo out the option name but how?... (theForm.products.option.name ???)

    thx
    nexe

    oh also is there is a cleaner way of doing like a funtion or class thx
  2. #2
  3. No Profile Picture
    cr-creations.co.uk
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Manchester, England
    Posts
    229
    Rep Power
    13
    im not sure if this will work but from looking at it, its my best bet

    PHP Code:
    <?
    $sql 
    mysql_query("Select * from item") or die("error in sql " mysql_error());
    ?>
    <script language="javascript">
    function selectprice(theForm)
    {

        theForm.price.value = theForm.selected.value;

    }
    </script>
    <form>
    <select name="products" onchange="selectprice(this.form)">
    <option name="0">select a product</option>
    <?
    while ($row mysql_fetch_array($sql))
    {

    ?>
    <option name="<?=$row["products_costprice"]?>"><?=$row["itemcode"]?></option>
    <?
    }
    ?>
    </select><br><br>
    <input type="text" name="price">
    </form>
    it would work but it depends if u need the $row['id'] for something else. ....

    hope it helps
    regards
    Last edited by destravation; February 9th, 2003 at 07:27 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    Australia
    Posts
    33
    Rep Power
    13
    didn't work is there a site where can view javascript form objects?

    maybe this is a more javascript related topic ill try it in the javascript forum

IMN logo majestic logo threadwatch logo seochat tools logo