1

How about, make this example, where we put an initial number and final number. Example We insert the Initial and Final Number:

Initial Number = 1 Final Number = 4

    Result = 1 2 3 4 

The result is thrown when we press the SEND button.

What I want is that I throw my result without having to press the SEND button. That the FOR cycle is performed and I throw the result without pressing the button. That the result is automatic.

CODE:

<form action="" method="post">
  <input type="text" name="inivalue" id="inivalue" placeholder="initial value"/>
  <input type="text" name="finvalue" id="finvalue" placeholder="final value"/>
  <input type="submit" name="submit" vale="submit" />
</form>


<?php
if(isset($_POST['submit']))
{
$num = (int)$_POST['inivalue'];
$numfin = (int)$_POST['finvalue'];
for($num=$num; $num <= $numfin; $num++)
{
echo $num;
}
}
?>
2
  • 3
    If you want this to be done automatically I would recommend javascript. Using PHP would be making it more complicated than it needs to be. Commented Feb 14, 2019 at 19:56
  • 1
    so you want to do oncomplete then Commented Feb 14, 2019 at 19:56

2 Answers 2

0

// Get your input elements using getElementById()
const initialValue = document.getElementById("inivalue");
const finalValue = document.getElementById("finvalue");
const result = document.getElementById("result");
let initialVal = "";
let finalVal = "";

// Every time you change the value in your <input> element
// save that value into the initialVal, finalVal variables.

initialValue.addEventListener("change", function(){
    initialVal = this.value;
    autoArray(initialVal,finalVal);
});

finalValue.addEventListener("change", function(){
    finalVal= this.value;
    autoArray(initialVal,finalVal);
});

// Loop using initialVal and finalVal
function autoArray(ini,fin){
  numArray = [];
  if (ini!= "" && fin != "") {
    for(i = ini; i <= fin; i++){
      numArray.push(i);
    }
  }
  // Change the value of the result <input> element
  result.value = numArray;
}
<input type="text" name="inivalue" id="inivalue" placeholder="initial value"/>
  <input type="text" name="finvalue" id="finvalue" placeholder="final value"/>
  <input type="text" id="result"/>

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to add another condition to check if both the inputs are numbers.
0

One way this can be done is using the onChange event.

set it in your final number field:

<input onchange = "rangefinder()" type="text" name="finvalue" id="finvalue" placeholder="final value"/>

then in your javascript function rangefinder():

function rangefinder(){
    //get the value of both the invalue and finalvalue fields
    //make sure they're both integers - just return if they're not.
    //use a for loop to make a string of numbers from invalue to finalvalue
    //insert this string where ever you want it.
}

I'll leave the actual JS up to you.

3 Comments

array() has special meaning in JS. You might want to change the name of your function.
Good catch. Fixed!
For the js lines have a look here: stackoverflow.com/questions/8069315/…

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.