Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6197443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:49:19+00:00 2026-05-24T03:49:19+00:00

I have a php for each loop which outputs a form for every product

  • 0

I have a php for each loop which outputs a form for every product in my database.

What I need help with is this bit of jquery

    <script type="text/javascript">
        // When the document is ready

        $(function() {


            $('#foo').change(function() {

              var form = $(this).parents('form');

                // Size is whatever the value the user has selected
                var size = $(this).val();

                var price,
                    itemId;

                // Determine the correct price and item ID based on the selected size
                switch (size) {
                <?php 
                    $var = 1;
                    foreach($subitem['sizes'] as $size) {
                        echo "\n";
                        echo "\t\t\t\t\t\tcase '".$size."':\n";
                        echo "\t\t\t\t\t\t\tprice = '".$subitem['price']."';\n";
                        echo "\t\t\t\t\t\t\titemId = '".$subitem['prodid']."-".$var."';\n";
                        echo "\t\t\t\t\t\t\tbreak;\n\n";

                    $var++;
                    }
                    echo "\t\t\t\t\t}";
                ?>

                form.find('.price').text(price);

                form.find('[name=my-item-price]').val(price);

              // Update the item ID
              form.find('[name=my-item-id]').val(itemId);


            });
        });
    </script>

This is output the same time as the form and is essentially part of the form. What it does is when a user selects an option from a select list, it changes a hidden form element containing the price.

What i need to do is instead of hard coding the prices, have the prices taken from the database.

Becuase it loops through 4 records to populate the select list, it’s impossible to just echo the price in the switch statement. What i thought about doing was to have some global variables which is populated with the price from the database and then just use that global variable in the switch statement

I thought i could initialise the global variables here

//Create field for item name based upon record count    
if(count($subitem['sizes']) > 1)
{

My code with the php and jquery

foreach($items AS $subitem)
{
    list($prodid, $item ,$size, $description, $price) = $subitem;

    //Create field for item name based upon record count    
    if(count($subitem['sizes']) > 1)
    {
        $item_name_field = "<ul><li><select name=\"my-item-name\" id=\"foo\">\n";
        foreach($subitem['sizes'] as $size)
        {
            $item_name_field .= "<option value=\"{$size}\">{$size}</option>\n";
        }
        $item_name_field .= "</select></li></ul>\n";
    }
    else
    {
        $item_name_field = "<input type=\"hidden\" name=\"my-item-name\" value=\"{$subitem['item']}\" />";
    }

    //Creat the form
    if ($count % NUMCOLS == 0) { echo "<tr>"; } //new row
    echo "<td>\n"; 
    echo "<form method=\"post\" action=\"\" class=\"jcart\">\n";
    echo "    <fieldset>\n";
    echo "        <input type=\"hidden\" name=\"jcartToken\" value=\"{$_SESSION['jcartToken']}\" />\n";
    echo "        <input type=\"hidden\" name=\"my-item-id\" value=\"{$subitem['prodid']}\" />\n";
    echo "        <input type=\"hidden\" name=\"my-item-price\" value=\"{$subitem['price']}\" />\n";
    echo "        <input type=\"hidden\" name=\"my-item-url\" value=\"http://yahoo.com\" />\n";
    echo "        {$item_name_field}\n";
    echo "        <ul>\n";
    echo "          <li>Price: $<span class=\"price\">{$subitem['price']}</span></li>\n";
    echo "          <li><label>Qty: <input type=\"text\" name=\"my-item-qty\" value=\"1\" size=\"3\" /></label></li>\n";
    echo "        </ul>\n";
    echo "        <input type=\"submit\" name=\"my-add-button\" value=\"add to cart\" class=\"button\" />\n";
    echo "    </fieldset>\n";
    echo "</form>\n";
    echo "</td>\n";
    ?>

    <script type="text/javascript">
        // When the document is ready
        $(function() {


            $('#foo').change(function() {

              var form = $(this).parents('form');

                // Size is whatever the value the user has selected
                var size = $(this).val();

                var price,
                    itemId;

                // Determine the correct price and item ID based on the selected size
                switch (size) {
                case 'Small':
                    price = '10.00';
                    itemId = '1-a';
                    break;
                case 'Medium':
                    price = '20.00';
                    itemId = '1-b';
                    break;
                case 'Large':
                    price = '30.00';
                    itemId = '1-c';
                    break;
                case 'X-Large':
                    price = '30.00';
                    itemId = '1-c';
                    break;
                }

                form.find('.price').text(price);

                form.find('[name=my-item-price]').val(price);

              // Update the item ID
              form.find('[name=my-item-id]').val(itemId);


            });
        });
    </script>
    <?php 
    $count++;
    if ($count % NUMCOLS == 0) { echo "</tr>\n"; } # end row
    //$counter++;  //Doesn't appear this is used
}

EDIT:

I’ve managed to get it working, sort of, but the prices are all the same.

What i need to do is assign the price of each size to the size.

<script type="text/javascript">
        // When the document is ready

        $(function() {


            $('#foo').change(function() {

              var form = $(this).parents('form');

                // Size is whatever the value the user has selected
                var size = $(this).val();

                var price,
                    itemId;

                // Determine the correct price and item ID based on the selected size
                switch (size) {
                <?php 
                    $var = 1;
                    foreach($subitem['sizes'] as $size) {
                        echo "\n";
                        echo "\t\t\t\t\t\tcase '".$size."':\n";
                        echo "\t\t\t\t\t\t\tprice = '".$subitem['price']."';\n";
                        echo "\t\t\t\t\t\t\titemId = '".$subitem['prodid']."-".$var."';\n";
                        echo "\t\t\t\t\t\t\tbreak;\n\n";

                    $var++;
                    }
                    echo "\t\t\t\t\t}";
                ?>

                form.find('.price').text(price);

                form.find('[name=my-item-price]').val(price);

              // Update the item ID
              form.find('[name=my-item-id]').val(itemId);


            });
        });
    </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:49:20+00:00Added an answer on May 24, 2026 at 3:49 am

    You can render a json object form php code along with the markup and use it in the js as below

        var item_prices_by_size = { "Small": { "Price": "10.00", "ItemId": "1-a" }, 
                                     "Medium": { "Price": "30.00", "ItemId": "1-b" } 
                                  };
    
                $(function() {
    
    
                    $('#foo').change(function() {
    
                      var form = $(this).parents('form');
    
                        // Size is whatever the value the user has selected
                        var size = $(this).val();
    
    // Determine the correct price and item ID based on the selected size
                        var price = item_prices_by_size[size].Price,
                            itemId = item_prices_by_size[size].ItemId;
    
                        form.find('.price').text(price);
    
                        form.find('[name=my-item-price]').val(price);
    
                      // Update the item ID
                      form.find('[name=my-item-id]').val(itemId);
    
    
                    });
                });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which will include template.php file from inside each of these
I have a PHP 2D array, many keys, each with one value, I need
I have created an form validation using ajax/php. Each text box is validated using
(This is greasemonkey with jquery) I have a string of data which is retrieved
I have a form with set of dropdown fields, each of which has a
I have some PHP code which runs a loop, spitting out a button and
I have a php function getContactList() : $res = getContactList(trim($_POST['username']), trim($_POST['password'])); which returns this
i have two files:(localhost/template/) index.php template.php each time when i create an article(an article
I want the following functionality using php I have a csv file. Each file
I have a command-line PHP script that runs a wget request using each member

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.