1

I'm trying to get a while loop to display data in <span> field with the a id, each time it loops. But it only display the data on the first loop. What I'm I doing Wrong?

<? php

$i = 0;
while($i < 5){
$i++;
echo "<input type = 'hidden' id = 'minute' value = ''>
<input type = 'hidden' id = 'second' value = ''>
<br><br>
<span id = 'selectmin' value = '0'></span>:<span id = 'selectsec' value = 
'0'></span>
<br><br>";
echo "<script>document.getElementById('selectmin').innerHTML = 'Hello ';
</script>";
?>
5
  • 4
    There's no closing } on your while loop. Commented May 20, 2017 at 19:16
  • 2
    @AluanHaddad document.getElementById('selectmin').innerHTML = 'Hello '; Commented May 20, 2017 at 19:20
  • 1
    @AluanHaddad <?Javascript echo '<script>alert(phpinfo());</script>'; ?> //confused Commented May 20, 2017 at 19:25
  • Wow O_O. I'm confused. phpinfo would need to be a global JavaScript variable to be used there Commented May 20, 2017 at 19:27
  • @YazanWYusuf Can you be more clear on your edit descriptions? 'briefly edit' doesn't tell why you edited the post, nor what the poster should do to fix it in the future. Commented May 21, 2017 at 1:28

2 Answers 2

2

This piece of code is not valid in php.

Either use short tags like <? or just use qualified PHP tags to avoid such mistakes. There is no space between <? and php.

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

1 Comment

If it worked for u please accept it by ticking it as helpful. Thanks.
0

id='selectmin' you are creating 5 identical HTML block with the same id? ID are unique and only gonna select the first match that's why it only show once.

Use 'selectmin$i' so it create a unique id for each loop:

<?php

$i = 0;
while($i < 5){
$i++;
echo "<input type = 'hidden' id = 'minute$i' value = ''>
<input type = 'hidden' id = 'second' value = ''>
<br><br>
<span id = 'selectmin$i' value = '0'></span>:<span id = 'selectsec$i' value = '0'></span>
<br><br>";
echo "<script>document.getElementById('selectmin$i').innerHTML = 'Hello '; </script>";
}
?>

Also close your while loop with } and use <?php or <? to start your php code.

The output HTML is:

<input type='hidden' id='minute1' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin1' value='0'></span>:<span id='selectsec1' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin1').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute2' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin2' value='0'></span>:<span id='selectsec2' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin2').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute3' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin3' value='0'></span>:<span id='selectsec3' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin3').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute4' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin4' value='0'></span>:<span id='selectsec4' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin4').innerHTML = 'Hello ';
</script>
<input type='hidden' id='minute5' value=''>
<input type='hidden' id='second' value=''>
<br>
<br>
<span id='selectmin5' value='0'></span>:<span id='selectsec5' value='0'></span>
<br>
<br>
<script>
  document.getElementById('selectmin5').innerHTML = 'Hello ';
</script>

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.