1

I have developed a PHP loop that creates a register form everyday for each user in my database. Each form has one div which contains a variety of dropdowns and this is placed directly over one which shows has what has been submitted that day. When one of the dropdown boxes is selected I have used jQuery to make the top div disappear and show the answer on the div below. If a mistake is made then there is a reset button which puts an x into the database.

I have also created a PHP query that finds the most recent input for each user every day (whether it is an x or what they were registered that day). This is then echoed into a div on to the top div (I use CSS to hide it).

What I want to have happen is that when somebody loads the page that it will show the people that have already been registered that day. I want to do this by having a function that hides the top div for anybody who's string length is >2. I have tried multiple ways to do this but can't get any to work. Is there a way to create unique id's and then transfer these to jQuery?

I have attached a simplified version of the code below that shows what I have so far. Any help would be really appreciated.

I have tried using a counter but I think I have implemented it wrong. Can anybody see how to fix it so that I can call unique ids? Here is the script I have tried.

<script>
$(document).ready(function() {
if ($("#needed<?= $i ?>").text().length > 3) {
$("#needed<?= $i ?>").parentsUntil(".submitted").addClass("hidediv");
$("#needed<?= $i ?>").parentsUntil(".submitted").removeClass("showdiv");
}
});

<!DOCTYPE html>

<html>

<head>
<?php include 'dbconnection.php'; ?>


<link href="hidphptest.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>



<script>
$(document).ready(function(){
$(".cars").change(function(){
   $(this).parentsUntil(".submitted").addClass("hidediv");
    $(this).parentsUntil(".submitted").removeClass("showdiv");
});
});



</script>



<script>
$(document).ready(function(){
$(".go45").click(function(){
        $(this).siblings().addClass("showdiv");
    $(this).siblings().removeClass("hidediv");

});
});


</script>

<script>
$(document).ready(function() {
if ($("#needed<?= $i ?>").text().length > 3) {
$("#needed<?= $i ?>").parentsUntil(".submitted").addClass("hidediv");
$("#needed<?= $i ?>").parentsUntil(".submitted").removeClass("showdiv");
}
});
</script>
<body>

<br>


<form  method="post" action="testplace3.php">
<select name="classu" >
<option  disabled="disabled" selected="selected">Select Class</option>
    <?php
$selectclass=$connect->query("SELECT DISTINCT class FROM `firstnametest` ");
while($rows=$selectclass->fetch_array())
{
 ?>
<option value="<?php echo $rows['class']; ?>"><?php echo $rows['class']; ?>            
</option>
<?php
}
?>



<input type="submit" value="Add Group" style="width: 317px; height: 45px"/>    

</select>
</form> 

<?php




$query=mysqli_query($connect,"SELECT * from firstnametest where class =     
'group1'" );
// Initiate  counter variable
$i = 1;
while ($row=mysqli_fetch_array($query))
{
?>






<div class="submitted ">
<button id="butest" type="button"   class="go45 btn btn-info " value="<?php          
echo$row["id"];   ?>">Go Back</button>
<div class="todo "><br><span id="needed<?= $i ?>"><?php      
 echo$row["surname"];   ?></span><br>




<select  class="cars" >
<option  disabled="disabled" selected="selected">Group </option>
<option value="Group 1">Group 1</option>
<option value="Group 2">Group 2</option>
<option value="Group 3">Group 3</option>
<option value="Group 4">Group 4</option>
</select>




<br>  
<div >

</div> 
</div>
</div>
<!-- Counter increment -->

<?php
$i++;
}

?>

</body>

</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bevan7
  • 63
  • 1
  • 2
  • 11
  • 2
    Where is `$i` being set or updated? – Peter Featherstone Jul 25 '17 at 17:14
  • 1
    Why do you think you have implemented it wrong? What behavior are you observing as opposed to the desired behavior? – coderodour Jul 25 '17 at 17:15
  • You're never setting `$groupder` to anything, and if you are, you're likely vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jul 25 '17 at 17:22
  • On loading the page I want it to hide the first div if the text string is longer than 3 characters. I have put a comment where I have started the counter - // Initiate counter variable $i = 1; and where it increments – bevan7 Jul 25 '17 at 17:22
  • Hi thanks for the advice about SQL injection. I should point out I'm planning to do that with the full version but trying to show the least amount of code as possible in the question to make it easier to decipher. All I'm trying to figure out at the moment is the way to make the divs hide on start up – bevan7 Jul 25 '17 at 17:25
  • 2
    Instead of trying to use different ids and looping over those, just assign each of those elements the same class and then in javascript you can do something like `$(".someclass").each(function(){ if($(this).text().length > 3) { ...add/remove class... }});` – Jonathan Kuhn Jul 25 '17 at 17:34
  • Hi. I tried the class thing but that wouldn't work either. It was explained to me that because 'if' is not a function then it wouldn't understand the 'this'. I could only get it to work with an individual ID that wasn't inside a php loop. Any idea how to get individual IDs? – bevan7 Jul 25 '17 at 17:56
  • You can set the classes via PHP before you serve the page, or you can set the classes via Javascript on the client side after the page has been received, but mixing them just obfuscates what's happening. In either case you don't need a unique "individual" `id="neededX"` on each element; PHP can do it w/o an ID, JS could do it with a common class. `$(document).ready(function() { $('.length_tested').each(function() { /*if len>3 add another class*/ }); });` – Stephen P Jul 25 '17 at 19:05
  • There's also a lot wrong... it's not `` it's `
    ` or `
    ` — your `
    ` is nested incorrectly - yours is `
    ` (your select is closed _after_ the form is closed) etc. Improper structure of the HTML leads to the browser trying to "fix" it and the resulting DOM structure is unpredictable, so your CSS and jQuery selectors may not do what you intend.
    – Stephen P Jul 25 '17 at 22:54
  • Thanks for your help Stephen. I've gone through and edited the code. Are you able to create a working script? I dont fully understand the .length_tested part. – bevan7 Jul 25 '17 at 23:46
  • @bevan7 - In my comment the `.length_tested` is a jquery selector used to find DOM elements that have `class="length_tested"` on them. That is, instead of individually & uniquely identifying `` (saying "this is one of my length-tested items") then the selector gets you all matching spans, and runs a function for each one (or attach a click handler to each: `$(".length_tested").click(function(event) { ... });`) – Stephen P Jul 26 '17 at 21:37

1 Answers1

0

If I am reading it right, your script is the problem.

<script>
$(document).ready(function() {
  if ($("#needed<?= $i ?>").text().length > 3) {
    $("#needed<?= $i ?>").parentsUntil(".submitted").addClass("hidediv");
    $("#needed<?= $i ?>").parentsUntil(".submitted").removeClass("showdiv");
}
});
</script>

I don't know how the $(document).ready(function() bit works but the code you are creating has a #needed, as $i is null when the page is generated by the PHP. So I guess that it is working only on the #needed div. You may need to loop through all your divs and hide them all in the function().

Mark B
  • 649
  • 5
  • 11
  • $i=1 when the script starts and then increments for each iteration – bevan7 Jul 26 '17 at 00:02
  • I know that is what you intended but this bit of code is only written once when the page is generated, php only gets run when the page is created. Look at your HTML source code once it has been loaded. – Mark B Jul 26 '17 at 06:26