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 9122131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:01:25+00:00 2026-06-17T06:01:25+00:00

The script below works fine, it combine JavaScript and PHP. By pressing the button

  • 0

The script below works fine, it combine JavaScript and PHP.
By pressing the button I get its value (buttonValue = 20) and the existing item list (A,B,C) is being replaced by the new list that stopped when $i <= 10 (1,2,3… 10):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){

    var buttonValue = $(':button').val();
    alert(buttonValue);

    $("#City > option").remove(); // remove all items from list
    <?php $i = 1; while ($i <= 10): ?>
    $('<option><?php echo $i; ?></option>').appendTo('#Country select'); 
    <?php $i++; endwhile; ?>
  });
});
</script>

<body>
    <button value="20">click to start</button>
    <div id="Country">
      <select id="City">
        <option value="">A</option>
        <option value="">B</option>
        <option value="">C</option>
      </select>
    </div>
</body>

I would like to change the PHP loop so that instead $i <= 10, it will show $i <= $the_buttonValue (assign the button value to show numbers 1,2,3… 20).

I’ve tried jQuery AJAX functions: load(), get() or the ajax() method, to send the variable to the server but that doesn’t seems to solve the above request.

I know that the two sides of PHP and JavaScript are communicate differently and that maybe an AJAX request can get the new requirement to work, but I’m not sure how.

Any tip to make it works as requested, would be greatly appreciated.

  • 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-06-17T06:01:26+00:00Added an answer on June 17, 2026 at 6:01 am

    you dont need php for this particular problem but if you must use it for some reason then you can use jquery load() function like:

    your.html

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
      $("button").click(function(){
    
        var buttonValue = $(':button').val();
        alert(buttonValue);
    
        $("#City > option").remove(); // remove all items from list
        $('#Country').load('your.php?value='+buttonValue+' #Country')
      });
    });
    </script>
    
    <body>
        <button value="20">click to start</button>
        <div id="Country">
          <select id="City">
            <option value="">A</option>
            <option value="">B</option>
            <option value="">C</option>
          </select>
        </div>
    </body>
    

    your.php

    <div id="Country">
          <select id="City">
    
    <?php 
        $val = $_GET['value']; //this will be your button value
        $i = 1;
        while (true)
        {
    ?>
            <option value="">
                        <?php echo $i++; if($i==$val) break; ?>
            </option>
    <?php
        }
    ?>
          </select>
    </div>
    

    My first ever stackoverflow answer, hope you will vote up 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing image upload script (below) which works fine but I would
The script below works fine if 3 and 3.2 are not links but i
I tested out the script below in jsfiddle and it works fine, can someone
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
It's my first time dealing with cookies in JavaScript and the below script works
Code below works fine in FF, CH etc. but doesn't get centred in IE.
The below script works fine but only for the first record in the array.
I was just testing this download script below. The downloading works fine but the
the below js script works fine but there is small problem and the problem
The script below, test.php, is intended to be placed in a specific directory of

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.