0

How can I get the values of each input field (both hidden and visible) using jQuery?

<?php 
     $date   = date('m-d-Y');
     $ts     = strtotime($date);
     $year   = date('o', $ts);
     $week   = date('W', $ts);

     for($i = 1; $i <= 7; $i++) {
     ?><td><?php
     $ts = strtotime($year.'W'.$week.$i);
     ?><input type="text" name="days" id="days" />
      <input type="hidden" name="date_now" id="date_now" value="<?php print date("Y-m-d", $ts); ?>" />
    </td><?php
    } 
?> 
d-_-b
  • 21,536
  • 40
  • 150
  • 256
webghost
  • 25
  • 2
  • 9

2 Answers2

2

Are all the inputs produced by the loop intended to be submitted as part of one form? Why not collate them into an array?

Change the names from days to days[] and from date_now to date_now[], respectively.

When embedding php control structures directly into html, consider using the alternative syntax for more clarity.

If you embed the current loop counter to the id of each input, you'll be able select each one individually.

If you add a class attribute to each input, you can select them as a group.

You can eliminate the extra assignment to $ts in each loop iteration by passing the result of strtotime() directly to the second parameter of date().

Since it's not totally clear what your ultimate intent was, here is your initial code sample modified with all of my suggestions applied:

<?php 
$date = date('m-d-Y');
$ts = strtotime($date);
$year = date('o', $ts);
$week = date('W', $ts);

for($i = 1; $i <= 7; $i++): 
$dt = date("Y-m-d", strtotime($year.'W'.$week.$i));
?>
<td>
<input type="text" name="days[]" id="days<?=$i?>" class="days" />
<input type="hidden" name="date_now[]" id="date_now<?=$i>" value="<?=$dt?>" class="date_now" />
</td>
<?php endfor; ?>

From the above code, you can either select one of the inputs individually like this:

$("#days3").val();

Or, you can select all of the hidden inputs like this:

var dates_now = [];
$(".date_now").each(function(i, input) {
    dates_now.push(input.val());
});
Community
  • 1
  • 1
Daniel Miladinov
  • 1,582
  • 9
  • 20
0

changing the id='days' and id='date_now' to classes (class='days' and class='date_now') assuming you want all the values, we can put them into an array like this:

var days_array     = [];
var date_now_array = [];

$('.days').each(function() {
    days_array.push($(this).val());
});

$('.date_now').each(function() {
    date_now_array.push($(this).val());
});

now the variables days_array and date_now_array contain all the values.

kennypu
  • 5,950
  • 2
  • 22
  • 28
  • 3
    The problem is that the `for` loop makes multiple elements with the same `id` attribute. You won't be able to properly select them all like this. – Blender Nov 28 '12 at 03:03
  • ahh i see, that's kind of bad practice to have duplicate id's (should be using classes instead), but i'll change my answer – kennypu Nov 28 '12 at 03:05
  • will i am not use something like .each function because when i am surfing the net,.,.i just encounter the .each function – webghost Nov 28 '12 at 03:05
  • can you give me an example anyway,.,.thankz because i want to get the values of the text field and hidden field and save it to my db,.,as duration and date_reported – webghost Nov 28 '12 at 03:06
  • @webghost: You won't be able to use `.each()` until you fix the `id` issue. The `id` attribute should be unique (one id per element and one element per id), so you'd have to change `id` to `class`. – Blender Nov 28 '12 at 03:07
  • 1
    easiest way, would be to change the id to class instead. would you want me to make an example of that? – kennypu Nov 28 '12 at 03:07
  • i would expand my question if you dont mind,,.how can i pass this values to something like $.post(blabla..) and get the pass values using php..,thankz – webghost Nov 28 '12 at 03:27
  • that would be an entirely different question. but you just use those values for the object you would pass to $.post – kennypu Nov 28 '12 at 03:30