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 14 of 14
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post

    data from database into js array

    Hello:

    I was wondering if I can get some guidance on how to pull data from mysql and inserted in js array in the following manner

    PHP Code:
    var option_array = new Array(num_of_cats);

    option_array[0] = new Array("You need to select a category\"); // This is the first (blank) category. Don't mess with it.
    option_array[1] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[2] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[3] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[4] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[5] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[6] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[7] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[8] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[9] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[10] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[11] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[12] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[13] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[14] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[15] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[16] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[17] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[18] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[19] = new Array(\"-- Select One --\",\"\",\"\");


    var text_array = new Array(num_of_cats);

    text_array[0] = new Array(\"Please Select a Merchandise\"); // These are general instructions. Change them if you want, or keep them if you don't.
    text_array[1] = new Array(\"Butterfly Paddle Blade\");
    text_array[2] = new Array(\"Newgy Robot Balls by Dozen\");
    text_array[3] = new Array(\"Mini Table For Students training\");
    text_array[4] = new Array(\"Nice Cold Drink of H2O\");
    text_array[5] = new Array(\"Replenish that thirst with this cold drink of Gatorate\");
    text_array[6] = new Array(\"Flextra Butterfly paddle combination\");
    text_array[7] = new Array(\"Membership Renewal\");
    text_array[8] = new Array(\"Individual Private Lesson for Adults with our Coach Jack by the hour\");
    text_array[9] = new Array(\"Individual Private Lesson for Children with our Coachby the hour\");
    text_array[10] = new Array(\"Group Lesson for Adults. For this class, a Min 3 and Max 6 persons by the hour\");
    text_array[11] = new Array(\"Group Lesson for Children. For this class, a Min 3 and Max 6 persons by the hour\");
    text_array[12] = new Array(\"Membership -- Couples (2)\");
    text_array[13] = new Array(\"Membership -- Family (Max 4)\");
    text_array[14] = new Array(\"Robot Usage -- 30 Minutes Increments\");
    text_array[15] = new Array(\"Gift Certificate -- Please provide certificate receiver's name below\");
    text_array[16] = new Array(\"Adding Value to an Existing Gift Certificate\");
    text_array[17] = new Array(\"RTTC Shirt -- Custom Stiched Shirt\");
    text_array[18] = new Array(\"other\");
    text_array[19] = new Array(\"Give Credit to a member or player\");



    var text_array2 = new Array(num_of_cats);
    text_array2[1] = new Array(\"160.00\");
    text_array2[2] = new Array(\"6.00\");
    text_array2[3] = new Array(\"175.00\");
    text_array2[4] = new Array(\"1.00\");
    text_array2[5] = new Array(\"2.00\");
    text_array2[6] = new Array(\"60.00\");
    text_array2[7] = new Array(\"30.00\");
    text_array2[8] = new Array(\"40.00\");
    text_array2[9] = new Array(\"35.00\");
    text_array2[10] = new Array(\"25.00\");
    text_array2[11] = new Array(\"20.00\");
    text_array2[12] = new Array(\"50.00\");
    text_array2[13] = new Array(\"70.00\");
    text_array2[14] = new Array(\"5.00\");
    text_array2[15] = new Array(\"0.00\");
    text_array2[16] = new Array(\"0.00\");
    text_array2[17] = new Array(\"25.00\");
    text_array2[18] = new Array(\"0.00\");
    text_array2[19] = new Array(\"0.00\");


    var text_array3 = new Array(num_of_cats);
    text_array3[1] = new Array(\"\");
    text_array3[2] = new Array(\"\");
    text_array3[3] = new Array(\"\");
    text_array3[4] = new Array(\"\");
    text_array3[5] = new Array(\"\");
    text_array3[6] = new Array(\"\");
    text_array3[7] = new Array(\"\");
    text_array3[8] = new Array(\"\");
    text_array3[9] = new Array(\"\");
    text_array3[10] = new Array(\"\");
    text_array3[11] = new Array(\"\");
    text_array3[12] = new Array(\"\");
    text_array3[13] = new Array(\"\");
    text_array3[14] = new Array(\"\");
    text_array3[15] = new Array(\"Important Notice For Gift Card Below!\");
    text_array3[16] = new Array(\"\");
    text_array3[17] = new Array(\"\");
    text_array3[18] = new Array(\"\");
    text_array3[19] = new Array(\"\");

    var text_array4 = new Array(num_of_cats);
    text_array4[1] = new Array(\"1\");
    text_array4[2] = new Array(\"1\");
    text_array4[3] = new Array(\"1\");
    text_array4[4] = new Array(\"1\");
    text_array4[5] = new Array(\"1\");
    text_array4[6] = new Array(\"1\");
    text_array4[7] = new Array(\"1\");
    text_array4[8] = new Array(\"1\");
    text_array4[9] = new Array(\"1\");
    text_array4[10] = new Array(\"1\");
    text_array4[11] = new Array(\"1\");
    text_array4[12] = new Array(\"1\");
    text_array4[13] = new Array(\"1\");
    text_array4[14] = new Array(\"1\");
    text_array4[15] = new Array(\"1\");
    text_array4[16] = new Array(\"1\");
    text_array4[17] = new Array(\"1\");
    text_array4[18] = new Array(\"1\");
    text_array4[19] = new Array(\"1\"); 
    thanks!,
    Mossa

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    STEP 1:

    Get it all working with the data you have currently hard-coded.
    You have to make sure everything works properly ... then let us know.

    I see some syntax problems ... so try my script below ...
    (not that I don't have problems ... I wasn't able to test it) ...

    PHP Code:

    <?php
    // PHP will output the javascripting ...

    echo"
    var option_array = new Array(num_of_cats);

    option_array[0] = new Array(\"You need to select a category\"); // This is the first (blank) category. Don't mess with it.
    option_array[1] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[2] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[3] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[4] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[5] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[6] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[7] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[8] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[9] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[10] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[11] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[12] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[13] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[14] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[15] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[16] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[17] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[18] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[19] = new Array(\"-- Select One --\",\"\",\"\");


    var text_array = new Array(num_of_cats);

    text_array[0] = new Array(\"Please Select a Merchandise\"); // These are general instructions. Change them if you want, or keep them if you don't.
    text_array[1] = new Array(\"Butterfly Paddle Blade\");
    text_array[2] = new Array(\"Newgy Robot Balls by Dozen\");
    text_array[3] = new Array(\"Mini Table For Students training\");
    text_array[4] = new Array(\"Nice Cold Drink of H2O\");
    text_array[5] = new Array(\"Replenish that thirst with this cold drink of Gatorate\");
    text_array[6] = new Array(\"Flextra Butterfly paddle combination\");
    text_array[7] = new Array(\"Membership Renewal\");
    text_array[8] = new Array(\"Individual Private Lesson for Adults with our Coach Jack by the hour\");
    text_array[9] = new Array(\"Individual Private Lesson for Children with our Coachby the hour\");
    text_array[10] = new Array(\"Group Lesson for Adults. For this class, a Min 3 and Max 6 persons by the hour\");
    text_array[11] = new Array(\"Group Lesson for Children. For this class, a Min 3 and Max 6 persons by the hour\");
    text_array[12] = new Array(\"Membership -- Couples (2)\");
    text_array[13] = new Array(\"Membership -- Family (Max 4)\");
    text_array[14] = new Array(\"Robot Usage -- 30 Minutes Increments\");
    text_array[15] = new Array(\"Gift Certificate -- Please provide certificate receiver's name below\");
    text_array[16] = new Array(\"Adding Value to an Existing Gift Certificate\");
    text_array[17] = new Array(\"RTTC Shirt -- Custom Stiched Shirt\");
    text_array[18] = new Array(\"other\");
    text_array[19] = new Array(\"Give Credit to a member or player\");

    var text_array2 = new Array(num_of_cats);
    text_array2[1] = new Array(\"160.00\");
    text_array2[2] = new Array(\"6.00\");
    text_array2[3] = new Array(\"175.00\");
    text_array2[4] = new Array(\"1.00\");
    text_array2[5] = new Array(\"2.00\");
    text_array2[6] = new Array(\"60.00\");
    text_array2[7] = new Array(\"30.00\");
    text_array2[8] = new Array(\"40.00\");
    text_array2[9] = new Array(\"35.00\");
    text_array2[10] = new Array(\"25.00\");
    text_array2[11] = new Array(\"20.00\");
    text_array2[12] = new Array(\"50.00\");
    text_array2[13] = new Array(\"70.00\");
    text_array2[14] = new Array(\"5.00\");
    text_array2[15] = new Array(\"0.00\");
    text_array2[16] = new Array(\"0.00\");
    text_array2[17] = new Array(\"25.00\");
    text_array2[18] = new Array(\"0.00\");
    text_array2[19] = new Array(\"0.00\");

    var text_array3 = new Array(num_of_cats);
    text_array3[1] = new Array(\"\");
    text_array3[2] = new Array(\"\");
    text_array3[3] = new Array(\"\");
    text_array3[4] = new Array(\"\");
    text_array3[5] = new Array(\"\");
    text_array3[6] = new Array(\"\");
    text_array3[7] = new Array(\"\");
    text_array3[8] = new Array(\"\");
    text_array3[9] = new Array(\"\");
    text_array3[10] = new Array(\"\");
    text_array3[11] = new Array(\"\");
    text_array3[12] = new Array(\"\");
    text_array3[13] = new Array(\"\");
    text_array3[14] = new Array(\"\");
    text_array3[15] = new Array(\"Important Notice For Gift Card Below!\");
    text_array3[16] = new Array(\"\");
    text_array3[17] = new Array(\"\");
    text_array3[18] = new Array(\"\");
    text_array3[19] = new Array(\"\");

    var text_array4 = new Array(num_of_cats);
    text_array4[1] = new Array(\"1\");
    text_array4[2] = new Array(\"1\");
    text_array4[3] = new Array(\"1\");
    text_array4[4] = new Array(\"1\");
    text_array4[5] = new Array(\"1\");
    text_array4[6] = new Array(\"1\");
    text_array4[7] = new Array(\"1\");
    text_array4[8] = new Array(\"1\");
    text_array4[9] = new Array(\"1\");
    text_array4[10] = new Array(\"1\");
    text_array4[11] = new Array(\"1\");
    text_array4[12] = new Array(\"1\");
    text_array4[13] = new Array(\"1\");
    text_array4[14] = new Array(\"1\");
    text_array4[15] = new Array(\"1\");
    text_array4[16] = new Array(\"1\");
    text_array4[17] = new Array(\"1\");
    text_array4[18] = new Array(\"1\");
    text_array4[19] = new Array(\"1\");  

    "
    ;

    ?>
    Let the PHP script insert that javascripting into your web page.
    I assume your page is .php instead of .html?

    PHP will be processed on the webhost server before serving the file,
    so the javascripting will be in place dynamically. Make sure what
    you have now works. Let me know.

    When the current script works properly, you can then do some queries
    to output each item dynamically ... PHP will duplicate what you've manually done.



    .

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    mlseim, good to hear from you again, its been awhile!

    My initial hard-coded data works fine -as does your revised one. It was within a php file inside js in the following manner
    PHP Code:
    $display_block .="
    <form name='PaymentForm' onsubmit='return ValidatePaymentForm() && PrintMeSubmitMe(this)' action='update_generalinvoice.php' method='post'>
    <script language='JavaScript'>
    var num_of_cats = 17; // This is the number of categories, including the first, blank, category.
    var open_in_newwindow=1; //Set 1 to open links in new window, 0 for no.
    var option_array = new Array(num_of_cats);
    .......// rest of the code 
    Mossa--

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    So now, you already have a MySQL database in place, and there are things stored in that?

    If so, what are the table name(s), and column names?

    Write a simple PHP script that connects to your database,
    and you can do a simple query without errors. I'm not sure
    how much scripting you have right now ... so I'm taking this
    in steps.

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    db table: products
    columns:
    id
    items
    products_desc
    quantity
    unit_cost
    other

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Do you know how to connect to your MySQL database?
    And do you have some knowledge of doing a query?

    And which one of those columns is used for "category"?

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    I have the following thus far:

    PHP Code:
    <?php 
    include '../datalogin.php';
    error_reporting(0);
    error_reporting(E_ERROR E_WARNING E_PARSE);
    //ini_set('display_errors',1);
    //error_reporting(E_ALL|E_STRICT);


    //Connect to mysql server
        
    $link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
        if(!
    $link) {
            die(
    'Failed to connect to server: ' mysql_error());
        }
        
    //Select database
        
    $db mysql_select_db(DB_DATABASE);
        if(!
    $db) {
            die(
    "Unable to select database");
        }
        
    $result mysql_query("SELECT * FROM products");

    while(
    $row mysql_fetch_array($result))
      {
      
    //$new[id] = $row['playerid'];

      
    echo $row['items'];
      echo 
    "<br />";
    }
        
    ?>
    so far so good!

    the items column would represent the categories

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Let's give this one a shot and see what happens ...

    PHP Code:

    <?php
    include '../datalogin.php';
    error_reporting(0);
    error_reporting(E_ERROR E_WARNING E_PARSE);
    //ini_set('display_errors',1);
    //error_reporting(E_ALL|E_STRICT);


    //Connect to mysql server
        
    $link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
        if(!
    $link) {
            die(
    'Failed to connect to server: ' mysql_error());
        }
        
    //Select database
        
    $db mysql_select_db(DB_DATABASE);
        if(!
    $db) {
            die(
    "Unable to select database");
        }
        
    $result mysql_query("SELECT * FROM products");

        
    // PHP will output the javascripting ...

    echo"
    var option_array = new Array(num_of_cats);

    option_array[0] = new Array(\"You need to select a category\"); // This is the first (blank) category. Don't mess with it.
    option_array[1] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[2] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[3] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[4] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[5] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[6] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[7] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[8] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[9] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[10] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[11] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[12] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[13] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[14] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[15] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[16] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[17] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[18] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[19] = new Array(\"-- Select One --\",\"\",\"\");


    var text_array = new Array(num_of_cats);
    "
    ;

    $count=1;
    echo
    "
    text_array[0] = new Array(\"Please Select a Merchandise\"); // These are general instructions. Change them if you want, or keep them if you don't.
    "
    ;
    while(
    $row mysql_fetch_array($result))
      {
    echo 
    "text_array[".$count."] = new Array(\"".$row['items']."\")";
    $count++;
    }

    echo
    "
    var text_array2 = new Array(num_of_cats);
    text_array2[1] = new Array(\"160.00\");
    text_array2[2] = new Array(\"6.00\");
    text_array2[3] = new Array(\"175.00\");
    text_array2[4] = new Array(\"1.00\");
    text_array2[5] = new Array(\"2.00\");
    text_array2[6] = new Array(\"60.00\");
    text_array2[7] = new Array(\"30.00\");
    text_array2[8] = new Array(\"40.00\");
    text_array2[9] = new Array(\"35.00\");
    text_array2[10] = new Array(\"25.00\");
    text_array2[11] = new Array(\"20.00\");
    text_array2[12] = new Array(\"50.00\");
    text_array2[13] = new Array(\"70.00\");
    text_array2[14] = new Array(\"5.00\");
    text_array2[15] = new Array(\"0.00\");
    text_array2[16] = new Array(\"0.00\");
    text_array2[17] = new Array(\"25.00\");
    text_array2[18] = new Array(\"0.00\");
    text_array2[19] = new Array(\"0.00\");

    var text_array3 = new Array(num_of_cats);
    text_array3[1] = new Array(\"\");
    text_array3[2] = new Array(\"\");
    text_array3[3] = new Array(\"\");
    text_array3[4] = new Array(\"\");
    text_array3[5] = new Array(\"\");
    text_array3[6] = new Array(\"\");
    text_array3[7] = new Array(\"\");
    text_array3[8] = new Array(\"\");
    text_array3[9] = new Array(\"\");
    text_array3[10] = new Array(\"\");
    text_array3[11] = new Array(\"\");
    text_array3[12] = new Array(\"\");
    text_array3[13] = new Array(\"\");
    text_array3[14] = new Array(\"\");
    text_array3[15] = new Array(\"Important Notice For Gift Card Below!\");
    text_array3[16] = new Array(\"\");
    text_array3[17] = new Array(\"\");
    text_array3[18] = new Array(\"\");
    text_array3[19] = new Array(\"\");

    var text_array4 = new Array(num_of_cats);
    text_array4[1] = new Array(\"1\");
    text_array4[2] = new Array(\"1\");
    text_array4[3] = new Array(\"1\");
    text_array4[4] = new Array(\"1\");
    text_array4[5] = new Array(\"1\");
    text_array4[6] = new Array(\"1\");
    text_array4[7] = new Array(\"1\");
    text_array4[8] = new Array(\"1\");
    text_array4[9] = new Array(\"1\");
    text_array4[10] = new Array(\"1\");
    text_array4[11] = new Array(\"1\");
    text_array4[12] = new Array(\"1\");
    text_array4[13] = new Array(\"1\");
    text_array4[14] = new Array(\"1\");
    text_array4[15] = new Array(\"1\");
    text_array4[16] = new Array(\"1\");
    text_array4[17] = new Array(\"1\");
    text_array4[18] = new Array(\"1\");
    text_array4[19] = new Array(\"1\");  

    "
    ;

    ?>

    .

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    so far so good! it is pulling "items" and making them into arrays.

    revised code
    PHP Code:
    <?php
    include '../datalogin.php';
    error_reporting(0);
    error_reporting(E_ERROR E_WARNING E_PARSE);
    //ini_set('display_errors',1);
    //error_reporting(E_ALL|E_STRICT);


    //Connect to mysql server
        
    $link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
        if(!
    $link) {
            die(
    'Failed to connect to server: ' mysql_error());
        }
        
    //Select database
        
    $db mysql_select_db(DB_DATABASE);
        if(!
    $db) {
            die(
    "Unable to select database");
        }
        
    $result mysql_query("SELECT * FROM products");

        
    // PHP will output the javascripting ...
    //var text_array = new Array(num_of_cats);
    //var text_array3 = new Array(num_of_cats);
    //var text_array4 = new Array(num_of_cats);
    echo"
    var option_array = new Array(num_of_cats);
    "
    ;

    $count=1;
    echo
    "
    option_array[0] = new Array(\"Please Select a Merchandise\");<br />"
    ;
    while(
    $row mysql_fetch_array($result))
      {
    echo 
    "option_array[".$count."] = new Array(\"".$row['items']."\");<br />";
    $count++;

    }
    ?>
    output:

    var option_array = new Array(num_of_cats); option_array[0] = new Array("Please Select a Merchandise");
    option_array[1] = new Array("Butterfly Paddle");
    option_array[2] = new Array("Balls");
    option_array[3] = new Array("TT Tables-Mini");
    option_array[4] = new Array("Water");
    option_array[5] = new Array("TT Tables -- Reg");
    option_array[6] = new Array("Gatorate");
    now to output the other arrays?
    Last edited by mbarandao; 04-11-2012 at 09:40 PM.

  • #10
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Here's some more ...

    I'm not sure if I'm picking the correct columns, but you should be able
    to see what I'm doing ... and figure out the rest of it.

    PHP Code:

    <?php
    include '../datalogin.php';
    error_reporting(0);
    error_reporting(E_ERROR E_WARNING E_PARSE);
    //ini_set('display_errors',1);
    //error_reporting(E_ALL|E_STRICT);


    //Connect to mysql server
        
    $link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
        if(!
    $link) {
            die(
    'Failed to connect to server: ' mysql_error());
        }
        
    //Select database
        
    $db mysql_select_db(DB_DATABASE);
        if(!
    $db) {
            die(
    "Unable to select database");
        }
        
    $result mysql_query("SELECT * FROM products");

        
    // PHP will output the javascripting ...

    echo"
    var option_array = new Array(num_of_cats);

    option_array[0] = new Array(\"You need to select a category\"); // This is the first (blank) category. Don't mess with it.
    option_array[1] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[2] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[3] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[4] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[5] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[6] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[7] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[8] = new Array(\"-- Select One --\",\"\",\"\",\"\");
    option_array[9] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[10] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[11] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[12] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[13] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[14] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[15] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[16] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[17] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[18] = new Array(\"-- Select One --\",\"\",\"\");
    option_array[19] = new Array(\"-- Select One --\",\"\",\"\");


    var text_array = new Array(num_of_cats);
    "
    ;

    $count=1;
    echo
    "
    text_array[0] = new Array(\"Please Select a Merchandise\"); // These are general instructions. Change them if you want, or keep them if you don't.
    "
    ;
    while(
    $row mysql_fetch_array($result))
      {
    echo 
    "text_array[".$count."] = new Array(\"".$row['items']."\")";
    $count++;
    }

    echo
    "
    var text_array2 = new Array(num_of_cats);
    "
    ;
    $count=1;
    while(
    $row1 mysql_fetch_array($result))
      {
    echo 
    "text_array2[".$count."] = new Array(\"".$row1['cost']."\")";
    $count++;
    }

    echo
    "
    var text_array3 = new Array(num_of_cats);
    "
    ;
    $count=1;
    while(
    $row2 mysql_fetch_array($result))
      {
    echo 
    "text_array3[".$count."] = new Array(\"".$row2['quantity']."\")";
    $count++;
    }

    echo
    "
    var text_array4 = new Array(num_of_cats);
    text_array4[1] = new Array(\"1\");
    text_array4[2] = new Array(\"1\");
    text_array4[3] = new Array(\"1\");
    text_array4[4] = new Array(\"1\");
    text_array4[5] = new Array(\"1\");
    text_array4[6] = new Array(\"1\");
    text_array4[7] = new Array(\"1\");
    text_array4[8] = new Array(\"1\");
    text_array4[9] = new Array(\"1\");
    text_array4[10] = new Array(\"1\");
    text_array4[11] = new Array(\"1\");
    text_array4[12] = new Array(\"1\");
    text_array4[13] = new Array(\"1\");
    text_array4[14] = new Array(\"1\");
    text_array4[15] = new Array(\"1\");
    text_array4[16] = new Array(\"1\");
    text_array4[17] = new Array(\"1\");
    text_array4[18] = new Array(\"1\");
    text_array4[19] = new Array(\"1\");  

    "
    ;

    ?>


    .

  • #11
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    now, i'm running into some js errors. here's what have so faronly the js portion with php sql queries embeds)

    PHP Code:
    $display_block .="
    <script language='JavaScript'>
    var num_of_cats = 17; // This is the number of categories, including the first, blank, category.
    var open_in_newwindow=1; //Set 1 to open links in new window, 0 for no.

    "
    ;
     
    $result mysql_query("SELECT * FROM products");

    $display_block .="var option_array = new Array(num_of_cats);";

    $count=1;
    $display_block .="option_array[0] = new Array(\"Please Select a Merchandise\");";
    while(
    $row mysql_fetch_array($result))
      {
    //$display_block .=" option_array[".$count."] = new Array(\"".$row['item']."\");";
    $display_block .="option_array[".$count."] = new Array(\"--select One--\",\"\",\"\");";
    $count++;

    }

       
    $result2 mysql_query("SELECT * FROM products");

    //$display_block .="<br /><br />";
    $display_block .="var text_array = new Array(num_of_cats);";

    $count=1;
    $display_block .="text_array[0] = new Array(\"Please Select a Merchandise\");";
    while(
    $row_1 mysql_fetch_array($result2))
      {
    $display_block .=" text_array[".$count."] = new Array(\"".$row_1['product_desc']."\");";
    $count++;

    }

    $result3 mysql_query("SELECT * FROM products");

    //$display_block .="<br /><br />";
    $display_block .="var text_array2 = new Array(num_of_cats);";

    $count=1;
    $display_block .="text_array2[0] = new Array(\"Please Select a Merchandise\");";
    while(
    $row_2 mysql_fetch_array($result3))
      {
    $display_block .="text_array2[".$count."] = new Array(\"".$row_2['unit_cost']."\");";
    $count++;

    }

    $result4 mysql_query("SELECT * FROM products");

    //$display_block .="<br /><br />";
    $display_block .="var text_array3 = new Array(num_of_cats);";

    $count=1;
    $display_block .="text_array3[0] = new Array(\"Please Select a Merchandise\");";
    while(
    $row_3 mysql_fetch_array($result4))
      {
    $display_block .="text_array3[".$count."] = new Array(\"".$row_3['other']."\");";
    $count++;

    }

    $display_block .="

    function switch_select()

    {
      for (loop = window.document.PaymentForm.select_2.options.length-1; loop > 0; loop--)
      {
        window.document.PaymentForm.select_2.options[loop] = null;
      }
      for (loop = 0; loop < option_array[window.document.PaymentForm.select_1.selectedIndex].length; loop++)
      {
        window.document.PaymentForm.select_2.options[loop] = new Option(option_array[window.document.PaymentForm.select_1.selectedIndex][loop]);
      }
        window.document.PaymentForm.select_2.selectedIndex = 0;
    }
      
    function switch_text()
    {
      window.document.PaymentForm.textarea_1.value = text_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
      window.document.PaymentForm.cost.value = text_array2[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
      window.document.PaymentForm.gift_card.value = text_array3[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
      //window.document.PaymentForm.qty.value = text_array4[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];

      }
    function box()
    {
      if (window.document.PaymentForm.select_2.selectedIndex == 0)
      {
        alert(\"Sorry, you have to select an item\");
      } else {
        if (open_in_newwindow==1)
        window.open(url_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex],\"_blank\");
        else
        window.location=url_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex]
      }
    }
    function set_orig()
    {
      window.document.PaymentForm.select_1.selectedIndex = 0;
      window.document.PaymentForm.select_2.selectedIndex = 0;
    }
    window.onload=set_orig
    </script> 
    The above script is the js that is inside a php file. I'm able to pull the data and the arrays are created -- I'm to see this from webpage source code.

    now, inside the html part of the file, I'm pulling the item list from the db and passing it to list box. the code:

    PHP Code:
    $get_products "select id as id_num, items as display_name2 from products order by id_num"
       
    $get_products_res mysql_query($get_products) or die (mysql_error()); 

    if (
    mysql_num_rows($get_products_res) < 1
      { 
           
    // no records 
           
    $display_block .="<p><em>Sorry, no records to select</em></p>"
        } 
        
      else 
       { 
        
    $display_block .="
       
    <td valign='top' style='border-left: 0px solid #cccccc;' width='15%'> <form name='form_1' onSubmit='return false;'>
          <select name=\"select_1\" style='background:url(../images/product-icon.png) #FFFFFF no-repeat 30% 5%; border-width:0px; width:200px; color:#003399; text-align:center; font-size:1em' onChange=\"switch_select(); switch_text();\"> 
          <option value=\"\">-- Select an Item --</option>"

     while (
    $recs2 mysql_fetch_array($get_products_res)) 
          { 
              
    $id_num $recs2['id_num']; 
              
    $display_name2 stripslashes($recs2['display_name2']); 
              
     
    $display_block .= "<option value=\"$id_num\">
                   $display_name2</option>"


                         } 
          } 
    the output is:

    PHP Code:
    <select name="select_1" style='background:url(../images/product-icon.png) #FFFFFF no-repeat 30% 5%; border-width:0px; width:200px; color:#003399; text-align:center; font-size:1em' onChange="switch_select(); switch_text();"
          <
    option value="">-- Select an Item --</option><option value="1">
                   
    5 A Series Blades</option><option value="2">
                   
    Balls</option><option value="3">
                   
    TT Tables-Mini</option><option value="4">
                   
    Bottle Water</option><option value="5">
                   
    TT Tables -- Reg</option><option value="6">
                   
    Gatorate</option><option value="7">
                   
    Blade combo</option><option value="8">
                   
    Membership Renewal</option><option value="9">
                   
    1 hr Private Lesson (Adlt)</option><option value="10">
                   
    1 hr Private Lesson (Chld)</option><option value="11">
                   
    Group Lesson (Adlt)</option><option value="12">
                   
    Group Lesson (Chlidren)</option><option value="13">
                   
    Membership (Couples-2)</option><option value="14">
                   
    Membership (Family -Up 4)</option><option value="15">
                   
    Robot Play</option><option value="16">
                   
    Gift Certificate</option><option value="17">
                   
    Gift Card Add Value</option><option value="18">
                   
    Club T-Shirts</option><option value="19">
                   
    Member Credit</option><option value="20">
                   
    Other</option><option value="21">
                   
    Rubber 1615 PIPS </option><option value="22">
                   
    Rubber -Volant 3</option><option value="23">
                   
    6 A Series Blades</option><option value="24">
                   
    Blades Only -China QI</option><option value="25">
                   
    Blades Only -CQ 1</option><option value="26">
                   
    Ross-Action Blade</option><option value="27">
                   
    Ross-Classic Blade</option
    I'm getting these errors pertain to the js:
    uncaught TypeError: Cannot set property 'selectedIndex' of undefined
    Uncaught TypeError: Cannot read property 'options' of undefined


    here is the function which purpose is to pass additional information to textbox and texarea box on selection from the list.
    PHP Code:
    function switch_text()
    {
      
    window.document.PaymentForm.textarea_1.value text_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
      
    window.document.PaymentForm.cost.value text_array2[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
      
    window.document.PaymentForm.gift_card.value text_array3[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];
      
    //window.document.PaymentForm.qty.value = text_array4[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex];

      
    }
    function 
    box()
    {
      if (
    window.document.PaymentForm.select_2.selectedIndex == 0)
      {
        
    alert("Sorry, you have to select an item\");
      } else {
        if (open_in_newwindow==1)
        window.open(url_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex],\"_blank\");
        else
        window.location=url_array[window.document.PaymentForm.select_1.selectedIndex][window.document.PaymentForm.select_2.selectedIndex]
      }
    }
    function set_orig()
    {
      window.document.PaymentForm.select_1.selectedIndex = 0;
      window.document.PaymentForm.select_2.selectedIndex = 0;
    }
    window.onload=set_orig 
    My apologies, for the lengthy code!
    Last edited by mbarandao; 04-12-2012 at 12:59 PM.

  • #12
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    This is strange ...

    All of them have this same line ...

    $display_block .="var option_array = new Array(num_of_cats);";


    I don't think I'm going to be able to figure-out this without being able to
    see the actual files and try out things. There's no way for me to test anything.
    It looks like something is named wrong in the javascripting.

    You also have a form named "PaymentForm", with a box called "qty" ...
    I have no idea where that form is, or what that form is. A whole bunch
    of script is missing, or doesn't exist?

  • #13
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    yes,
    PHP Code:
    $display_block .="var option_array = new Array(num_of_cats);"
    appears multiple times, but with a different variable

    PHP Code:
    var option_array
    var text_array
    Var text_array2
    var text_array3 
    I decided not to create an array of the content of "qty" box.

    If you like I can PM you the entire file. The problem seems to be centered around my list box; whereas, initially, the values where hard-coded into the box and now populated dymanically. This appears to be producing the following errors:

    PHP Code:
    uncaught TypeErrorCannot set property 'selectedIndex' of undefined
    Uncaught TypeError
    Cannot read property 'options' of undefined 
    for testing purpose, if hard-code the list box the error disappears.

    listbox code
    hard-coding:

    PHP Code:
    <td valign='top' style='border: 0px solid #cccccc;' width='15%'> <form name='form_1' onSubmit='return true;'>
      <
    select name='select_1' onChange="switch_select(); switch_text();\">
        <option>-- Select an Item --</option><option>1 hr Private Lesson (Adlt)</option><option>1 hr Private Lesson (Chld)</option><option>5 A Series Blades</option><option>6 A Series Blades</option><option>Balls</option><option>Blade combo</option><option>Blades Only -China QI</option><option>Blades Only -CQ 1</option><option>Bottle Water</option><option>Club T-Shirts</option><option>Gatorate</option><option>Gift Card Add Value</option><option>Gift Certificate</option><option>Group Lesson (Adlt)</option><option>Group Lesson (Chlidren)</option><option>Member Credit</option><option>Membership (Couples-2)</option><option>Membership (Family -Up 4)</option><option>Membership Renewal</option><option>Other</option><option>Robot Play</option><option>Ross-Action Blade</option><option>Ross-Classic Blade</option><option>Rubber - 1615 PIPS </option><option>Rubber -Volant 3</option><option>TT Tables -- Reg</option><option>TT Tables-Mini</option> 

      </select> 
    Dynamic data:

    PHP Code:
    $get_products "select id as id_num, items as display_name2 from products order by id_num"
       
    $get_products_res mysql_query($get_products) or die (mysql_error()); 

    if (
    mysql_num_rows($get_products_res) < 1
      { 
           
    // no records 
           
    $display_block .="<p><em>Sorry, no records to select</em></p>"
        } 
        
      else 
       { 
        
    $display_block .=" <td valign='top' style='border: 0px solid #cccccc;' width='15%'> <form name='form_1' onSubmit='return false;'>
          <select name='select_1' style='background:url(../images/product-icon.png) #FFFFFF no-repeat 30% 5%; border-width:0px; width:200px; color:#003399; text-align:center; font-size:1em' onChange=\"switch_select(); switch_text();\"> 
          <option value=''>-- Select an Item --</option>"

     while (
    $recs2 mysql_fetch_array($get_products_res)) 
          { 
              
    $id_num $recs2['id_num']; 
              
    $display_name2 stripslashes($recs2['display_name2']); 
              
     
    $display_block .= "<option value=\"$id_num\">$display_name2</option>"
     
                         } 
          } 
    Your thoughts!

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    341
    Thanks
    22
    Thanked 1 Time in 1 Post
    mlseim, found the issue. It was a missing closing select tag for the dynamically populated listbox. Applied the fix and voila, issue resolved!

    Listen, thank you so much for working through with me. Much oblige!

    Best,
    Mossa


  •  

    Posting Permissions

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