0

I have this table:

foreach( //conditionals ){ 
  <td id="changeStatus">
     <input type="text" name="rcStatus" value=""/>
  </td>
}

The <td>s are blank, and when I click on each one, the bgcolor of the cell changes, and also the value of rcStatus.

I do using this code:

<script> 
  $('#table #changeStatus').click(
    function(){
      var cell = $(this);
      state = cell.data('state') || 'first';

    switch(state){
      case 'first':
      cell.addClass('red');
      cell.data('state', 'second');
      this.getElementsByTagName('input')[0].value = "missed";
      break;
      // other cases here 
     }
  });
</script>

My problem now is that I need to store the value of rcStatus in my database. What happens is that I am only able to store the most recently set rcStatus. I'm using PHP.

foreach( //conditionals ){ 
  mysql_query("INSERT INTO `details`(ID, Name, Status) VALUES('NULL','$_POST[name]','$_POST[rcStatus]');");
}

How can I call each individual variable using $_POST even though I'm using the same name/id?

fmodos
  • 4,472
  • 1
  • 16
  • 17
abigail_g
  • 37
  • 9

3 Answers3

0

You could append a number to the name of the input field to identify each input field, see also Multiple inputs with same name through POST in php.

Community
  • 1
  • 1
Smutje
  • 17,733
  • 4
  • 24
  • 41
0

you can use this logic.

$i = 1;
foreach( //conditionals ){ 
  <td id="changeStatus">
     <input type="text" name="rcStatus<?php echo $i; ?>" value=""/>
  </td>
$i++;
}
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
0

Change your <tr> to:

foreach( //conditionals ){ 
  <td id="changeStatus">
     // giving [] to a name attribute makes it an input array, like @Smutje mentioned
     <input type="text" name="rcStatus[]" value=""/>   
  </td>
}

And in your PHP:

foreach($_POST[rcStatus] as $key=>$val){ 
  mysql_query("INSERT INTO `details`(ID, Name, Status) VALUES('NULL','$_POST[name]','$val');");
}

A few notes:

  1. Please migrate to mysqli_ functions as mysql_ is deprecated
  2. You are open to SQL Injection . For the time being, till you migrate, you can use mysql_real_escape_string to escape $_POST values
Community
  • 1
  • 1
AyB
  • 11,609
  • 4
  • 32
  • 47