Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts

    price not populating in text field when selecting product in drop down

    i have the below code which is populating my drop down with the product names stored in tblproduct. the price of the products are stored in another table called tblretprod. the common field between the tables is the prod_id.
    when i am selecting the product name, the price related to the product is not being displayed.
    how can i update the price in tblretprod for each product as well when i change the price displayed to another one?

    here is the code populating the select drop down.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Update product</title>

    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var val=form.prod_name.options[form.prod_name.options.selectedIndex].value;
    //var description1=form.description.value;
    self.location='upd_prod.php?prod_name=' + val;
    //+'&description=' +description1;
    }

    </script>

    <link href="CSS/update.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <?php

    include('db_connect.php');

    @
    $prod_name=$_GET['prod_name'];

    $query=mysql_query("select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id");

    ?>

    <div id="stylized" class="myform">

    <form id="form" name="upd_prod" method="post" action="updprod.php">

    <h2 align="center"><b>- Update Product -</b></h2>

    <table width="1000" border="0">
      <tr>
        <td><div align="right">Product Name</div></td>
        <td>
        <?PHP 
            
             
    echo "<select name='prod_name' onchange=\"reload(this.form)\"><option value=''>select one</option>";
    while(
    $row1 mysql_fetch_array ($query)) { 
    if(
    $row1['0']==@$prod_name){echo "<option selected value='".$row1['0']."'>".$row1['0']."</option>";}
    else{echo  
    "<option value=\"".$row1['0']."\">".$row1['0']."</option>";}
    }
    echo 
    "</select>";
            
    ?>
        
        </td>
      </tr>
      <tr>
        <td><div align="right">Product Price (MRU)</div></td>
        <td><input type="text" name="prod_price" id="prod_price" value = "<?PHP print $row['prod_price'?>"/></td>
      </tr>
     </table>

    <p align="center">
      <input type="submit" class="button" name="update" id="update" value="<-- Update product -->" />
    </p>

    </form>

    </div>

    </body>
    </html>
    error message received is

    Notice: Undefined variable: row in C:\wamp\www\buysmart_site\upd_prod.php on line 61

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You don't have a $row variable, so you cannot find an associative offset for prod_price in it. You do have a $row1 variable though, but if you access it outside of the loop it will contain only the last value in it. Which happens to be false, so you still won't be able to dereference an associative offset from it.
    I don't understand what you are doing here though. Your if/else branch is the same, so why bother checking on it at all?
    As for the price, you can access that within the while loop under $row1[1] or $row1['prod_price'] since you've used a fetch_array so both the numerical and named entries are available.

    Edit:
    Oh wait, I see now the difference. There's a selected there. You should used selected="selected" though.
    Since its *mostly* the same, I'd suggest assembling it instead:
    PHP Code:
    while($row1 mysql_fetch_array ($query))
    {  
        
    $selected $row1[0] == $prod_name ' selected="selected"' '';
        
    printf('<option value="%s"%s>%s - %s</option>'$row[0], $selected$row[0], $row[1]);

    Also, I think what you want to do here is use JS to populate the price onchange. To do so, you'll either need to fetch all records and prices and generate the necessary javascript, or use ajax to lookup the value on request. PHP is incapable of responding to client events such as onchange.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hello, i tried what you said but it did not work. What i did is change the code itself to the one below, but i am having no dropdown and am having an undefined variable selected_product as well

    PHP Code:
    <!-- you beginning HTML -->
    <?php


    include('db_connect.php');

    $prod_name = isset($_GET['prod_name'])?$_GET['prod_name']:"";

    $sql "select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id";

    $result mysql_query($sql);

    if(
    mysql_num_rows($result) < 1) {} //No records, do something (exit, die, return, whatever)

    ?>
    <!-- Continue your HTML -->
    <?php 

    while($row mysql_fetch_array($result){
    if(
    $row['prod_name'] == $prod_name) {
    $selected_product $row;
    echo 
    "<option selected value='".$row['0']."'>".$row['0']." </option>";
    } else {
    echo 
    "<option value=\"".$row['0']."\">".$row['0']."</option>";
    }
    }
    ?>
    <!-- Continue your HTML -->
    <td><input type="text" name="prod_price" id="prod_price"

    value = "<?php print $selected_product['prod_price'?>"/></td> 
    <!-- Rest of HTML -->
    please help out as am stuck with this for a few days now and my deadline for project is approaching fast.

    thank you

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You don't have a select tag here, nor most of your other required tags, so you won't have a dropdown.
    Selected product can only exist if a record matches $prod_name. If there is no match, than there is no $selected_product variable. You'll need to use isset() to check if a variable / offset exists prior to printing it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    here is the full code. please help me sort this out.

    PHP Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Update product</title>

    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var val=form.prod_name.options[form.prod_name.options.selectedIndex].value;
    //var description1=form.description.value;
    self.location='upd_prod.php?prod_name=' + val;
    //+'&description=' +description1;
    }

    </script>

    <link href="CSS/update.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <?php
    include('db_connect.php');
    $prod_name = isset($_GET['prod_name'])?$_GET['prod_name']:"";
    $sql "select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id";
    $result mysql_query($sql);
    if(
    mysql_num_rows($result) < 1) {} //No records, do something (exit, die, return, whatever)
    ?>

    <div id="stylized" class="myform">

    <form id="form" name="upd_prod" method="post" action="updprod.php">

    <h2 align="center"><b>- Update Product -</b></h2>

    <table width="1000" border="0">
      <tr>
        <td><div align="right">Product Name</div></td>
        <td>
        <?php 
    while($row mysql_fetch_array($result)){
    if(
    $row['prod_name'] == $prod_name) {
    $selected_product $row;
    echo 
    "<option selected value='".$row['0']."'>".$row['0']." </option>";
    } else {
    echo 
    "<option value=\"".$row['0']."\">".$row['0']."</option>";
    }
    }
    ?>
        </td>
      </tr>
      <tr>
        <td><div align="right">Product Price (MRU)</div></td>
        <td><td><input type="text" name="prod_price" id="prod_price"
    value = "<?php print $selected_product['prod_price'?>"/></td> </td>
      </tr>
      </table>

    <p align="center">
      <input type="submit" class="button" name="update" id="update" value="<-- Update product -->" />
    </p>

    </form>

    </div>

    </body>
    </html>

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You still don't have the select tags. Just copy the tags out from your first post and add that to the existing code.
    Ultimately, this is the only real change you've made: $selected_product = $row; which is actually sufficient. The problem was that later when you accessed the $row['prod_price'] (which should have been $row1 presumably), it would always be false. By now assigning $selected_product, this variable will not be destroyed during teardown of the loop.
    The only other thing to change is the access to prevent an E_NOTICE: <?php print isset($selected_product['prod_price']) ? $selected_product['prod_price'] : 0; ?> should do it.
    I assume your JS is good. Unfortunately my JS is mediocre at best.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    hance (07-03-2013)

  • #7
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    hey thnx buddy, that solved out the issue. find the below the full code

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Update product</title>

    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var val=form.prod_name.options[form.prod_name.options.selectedIndex].value;
    //var description1=form.description.value;
    self.location='upd_prod.php?prod_name=' + val;
    //+'&description=' +description1;
    }

    </script>

    <link href="CSS/update.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <?php
    include('db_connect.php');
    $prod_name = isset($_GET['prod_name'])?$_GET['prod_name']:"";
    $sql "select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id";
    $result mysql_query($sql);
    if(
    mysql_num_rows($result) < 1) {} //No records, do something (exit, die, return, whatever)
    ?>

    <div id="stylized" class="myform">

    <form id="form" name="upd_prod" method="post" action="updprod.php">

    <h2 align="center"><b>- Update Product -</b></h2>

    <table width="1000" border="0">
      <tr>
        <td><div align="right">Product Name</div></td>
        <td>
        <?php 
        
    echo "<select name='prod_name' onchange=\"reload(this.form)\"><option value=''>select one</option>";
    while(
    $row mysql_fetch_array($result)){
    if(
    $row['prod_name'] == $prod_name) {
    $selected_product $row;
    echo 
    "<option selected value='".$row['0']."'>".$row['0']." </option>";
    } else {
    echo 
    "<option value=\"".$row['0']."\">".$row['0']."</option>";
    }
    }
    echo 
    "</select>";
    ?>
        </td>
      </tr>
      <tr>
        <td><div align="right">Product Price (MRU)</div></td>
        <td><td><input type="text" name="prod_price" id="prod_price"
    value = "<?php print isset($selected_product['prod_price']) ? $selected_product['prod_price'] : 0?>"/></td> </td>
      </tr>
      
    </table>

    <p align="center">
      <input type="submit" class="button" name="update" id="update" value="<-- Update product -->" />
    </p>

    </form>

    </div>

    </body>
    </html>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You bet.
    Now I'd suggest compacting it as much as you can without moving to functions. In this case, use a ternary operator (?: syntax) instead of the if/else like I showed above. That way you have the same <option> tag without needing to modify two locations if you want to change it.
    One thing that always bugs me, but this is a personal preference, is the use of empty options to denote selection. My personal approach is to use optgroup labels for that, but these are not selectable. So I always provide either a default value selected, or select the first option if none are selected. You would need to reflect that accordingly if you do though so you can show it later in the prod_price input.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    hance (07-04-2013)

  • #9
    New Coder
    Join Date
    Jun 2013
    Posts
    30
    Thanks
    6
    Thanked 0 Times in 0 Posts
    thanks for the advice my friend but i dnt have much time to do all that. i have to submit my project in a week. sorry but i'll do that for the coming ones as am going to learn the new php.


  •  

    Posting Permissions

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