PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How to retrieve and modify textbox values with jQuery inside a php loop

 October 27, 2022     jquery, php     No comments   

Issue

I want to provide a restriction to input fields inside a PHP loop, so that in each of the looped input fields, a user can not insert a value greater than 100. If the value is greater than 100, the user will be alerted that the value is greater than 100 and the field will be reset to empty ("") so that he can write the correct value. My issue is that the code only functions for the first input field and not for the rest of the loop.

Here is what I have tried.

while ($row = mysqli_fetch_array($query)) {
    echo "<label>Name</label>
    <input type='text' class='form-control' name='name' id='name' value='".$row['name']."' />";
    echo "<label>Score</label>
    <input type='text' class='form-control' name='score' id='score' value='' />";
}


$(document).ready(function(){
$("#score").keyup(function(){                
    var score=$(this).val();
    if (score > 100) {
        alert("Score cannot be greater than 100");
        $('#score').val("");
    }
});

});


Solution

Please try this

php:

while ($row = mysqli_fetch_array($query)) {
    echo "<label>Name</label>
    <input type='text' class='form-control' name='name' id='name' value='".$row['name']."' />";
    echo "<label>Score</label>
    <input type='text' class='form-control score' name='score' id='score' value='' />";
}

js:

    $(document).ready(function(){
        $(".score").keyup(function(){                
            var score=$(this).val();
            if (score > 100) {
                alert("Score cannot be greater than 100");
                $(this).val("");
            }
        });
    });


Answered By - Delowar Hossen
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing