1

i have a website done in php, which has a form for user to select a certain data, on selection of the data, after that user has the option to generate random value, so the random value should start with the select box value and some random number, so far i did the following:

  function Random() {
          var rnd = Math.floor(Math.random() * 1000000000);
          document.getElementById('tb').value = rnd;
      }
<select class="form-control" name="customer" aria-label="Default select example">
  <option value="">Select Customer</option>
  <?php
                                                            foreach($listcustomers as $valw){?>
    <option value="<?=$valw->name?>">
      <?=$valw->name?>
    </option>
    <?php }?>

</select>

<input type="button" onclick="Random();" class="btn btn-outline-warning" value="GENERATE">
<input type="text" name="sku" class="form-control" id="tb" placeholder="SKU" value="" required>

this is generating random number on button click, but how do i start the random number with the value of user selected value in selectbox, can anyone please tell me, thanks in advance

4
  • Where is jquery involved? in Random(), get the selected option from your <select> and prepend it to your random number Commented Dec 13, 2021 at 11:09
  • @brombeer can u please show how to do that Commented Dec 13, 2021 at 11:10
  • Does this answer your question? jQuery get selected option value (not the text, but the attribute 'value') Commented Dec 13, 2021 at 11:14
  • What exactly is not working? Is this a Javascript problem, or a PHP problem? Commented Dec 13, 2021 at 11:30

1 Answer 1

1

So what you can do is get the value of the selected option in JavaScript and add it as the start value before your random number. Your Random() function can be like:

function Random() {
    var userValueElement = document.getElementById("userValue");
    var userValue = userValueElement.options[userValueElement.selectedIndex].text;
    var rnd = Math.floor(Math.random() * 1000000000);
    document.getElementById('tb').value = userValue + rnd;
}

Notice that I am getting the select element with the help of id. You can add id to your select element:

<select id="userValue" class="form-control" name="customer" aria-label="Default select example">
...
</select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.