jQuery append html x-amount of times

Thanks for getting back to me!

So that’s PHP, right?

That’s correct.

Once you have a reference to $num_computers for the individual stores, couldn’t you just use it to create a for loop?

I’ve done that but it is only retrieving the last $num_computers value. Maybe if I assign all of the $row[‘num_computers’] to an array? :nono:

But don’t you have a database record which represents a store and that record contains a reference to the number of computers a store has?
If that’s the case, then there might be something wrong with your PHP.
Could you post it here?

The PHP is correctly returning the right number of computers for each store. At the moment I’m returning 3 stores each having a select menu and the $row[‘num_computers’] value. Store 1’s value is 5, store2’s value is 8 and store3’s value is 12. But the jQuery is only returning the last value, number 12. How would it know which $row[‘num_computers’] to retrieve? I haven’t put a $(this) or the like in front of it.

Could you post it here?

Too messy, too much personal info at the moment.

I think we’re getting our wires crossed :slight_smile:

jQuery and PHP cannot talk to each other.
PHP gets parsed and sends a bunch of HTML to the browser.
jQuery can then manipulate and do things with that HTML.

If you say that the PHP script is outputting what it should, then you just have to store a reference to $num_computers somewhere that jQuery can get at it.
You cannot hand this reference to jQuery via PHP.

Maybe using a data attribute would be the best way?
This is quite independent of your doctype, so would work fine with HTML 4 (as far as I am aware).

Maybe using a data attribute would be the best way?

Ok, I can try that.

Thanks for your help Pullo, I’ll see what I can do.

Cheers.

No probs.
Let me know if the data-attribute thing works.

Hey Pullo,

the data-attribute seems to work, and select menus are being created with the correct numbers of options. Good idea, thanks.

Now the next stage is my favorite part - validation. :wink:

When someone makes a selection from the first menu, an equal number of second menus will appear. Each of these second menus must have a value greater than 0 before the form can be submitted. I’ve used the in_array function for showing error messages which works to some extent. I’m trying to show the messages in the following format:

Please select a value for Mike’s computer1
Please select a value for Mike’s computer4
Please select a value for Jane’s computer2

For each error message I include the specific store name and number. Do you know I can achieve this? At the moment is isn’t printing error messages for all the “affected” menus.

I realize this is PHP but maybe you could quickly look this over?

Thank you very much.

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  if(isset($_POST['computers']) ){
    $com = array();
    foreach($_POST['computers'] as $k => $v){
                foreach($v as $n => $m){
                 $com[] .= $m;
          }
          }

if (in_array("0", $com)) {

$errors['computers'] = "Please select a value for $k";

}
else {     

Got it working, I had to concatenate the error messages.

Thanks.

Good job :slight_smile: