Reading values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Godefroy

    Reading values

    Hi there...

    I want to read fieldvalues from out of a dynamicaly created table (php). But
    this has to be done by JavaScript. Every row of the table has a select-box,
    inputbox and a order-button. So if I press on "order" at row 8, I just want
    to popup "Product 8 - Price - Quantity" using alert()correspo nding to the
    rowvalues from where the button was pressed.

    Any help appreciated.

    Roger Godefroy
    Q-Websolutions.co m


  • Zac Hester

    #2
    Re: Reading values

    "Roger Godefroy" <godefroy@(no spam)home.nl> wrote in message
    news:bfo84v$lvg $1@news3.tilbu1 .nb.home.nl...[color=blue]
    > Hi there...
    >
    > I want to read fieldvalues from out of a dynamicaly created table (php).[/color]
    But[color=blue]
    > this has to be done by JavaScript. Every row of the table has a[/color]
    select-box,[color=blue]
    > inputbox and a order-button. So if I press on "order" at row 8, I just[/color]
    want[color=blue]
    > to popup "Product 8 - Price - Quantity" using alert()correspo nding to the
    > rowvalues from where the button was pressed.
    >
    > Any help appreciated.
    >
    > Roger Godefroy
    > Q-Websolutions.co m
    >[/color]

    Reading values from non-form elements on a web page is possible in newer
    browsers (look into .innerText, .innerHTML, and .nodeValue to use these
    "newer" features).

    However, I would recommend just coding it into a simpler JavaScript at the
    head of the document:

    ....
    <html>
    ....
    <head>
    <script type="text/javascript">

    //Set up the base array for a 2D array.
    var tabledata = new Array();

    //Array indexes are generated inside PHP loop using loop index.
    tabledata[0] = new Array('Product One','Price One');
    tabledata[1] = new Array('Product Two','Price Two');
    tabledata[2] = new Array('Product Three','Price Three');

    //Pop up an alert box with order information.
    function popup(row_num) {

    //Get quantity from select box.
    var quantity = document.getEle mentById('slctb x'+row_num).val ue;

    //Tell the user what they're ordering:
    alert("Order Details:\n\nPro duct: "+tabledata[row_num][0]
    +"\nPrice: "+tabledata[row_num][1]+"\nQuantity : "
    +quantity);
    }
    </script>
    </head>
    <body>
    <!-- Numbers in form fields are generated
    inside PHP loop using loop index. -->
    ....
    <input type="checkbox" name="chkbx0"
    value="true" onclick="popup( 0);" />
    ....
    <select name="slctbx0"> ...</select>
    ....
    <input type="checkbox" name="chkbx1"
    value="true" onclick="popup( 1);" />
    ....
    <select name="slctbx1"> ...</select>
    ....
    <input type="checkbox" name="chkbx2"
    value="true" onclick="popup( 2);" />
    ....
    <select name="slctbx2"> ...</select>
    ....
    </body>
    </html>


    PHP (and any other server-side execution environment) gives you the ability
    to generate dynamic JavaScript. It can save you a lot of hassles that you
    encounter when you're trying to put the burden on the visitor's UA (such as
    backwards compatibility). Try to keep your JavaScripts very simple (or
    non-existent) and leverage PHP's ability to always run the same way no
    matter what browser is accessing your page.

    HTH,
    Zac


    Comment

    Working...