views:

44

answers:

3

I basically need to check if there is an easier way to do this before I rewrite all the code. I have a fairly large form that I have each input named with []'s on the end so I can loop through via php for easy insertion.

            <input type="hidden" name="currentdate[]" value="<?php echo date('mdY'); ?>">
                <td><input style="width: 50px" type="text" name="jackname[]" /></td>
                <td><input style="width: 20px" type="text" name="jackkey[]" /></td>
                <td><input style="width: 50px" type="text" name="jackbeg[]" /></td>
                <td><input style="width: 50px" type="text" name="jackend[]" /></td>
                <td><input style="width: 50px" type="text" name="jackbegveh" /></td>
                <td><input style="width: 50px" type="text" name="jackbegmon[]" /></td>
                <td><input style="width: 50px" type="text" name="jackendveh" /></td>
                <td><input style="width: 50px" type="text" name="jackendmon[]" /></td>
                <td><input style="width: 50px" type="text" name="jacktx" disabled /></td>

There are quite a few more fields but you get the idea. I then use

foreach ($_POST['jackname'] as $row=>$name) 
{ 
    $jackname = $name;
    $date = $_POST['currentdate'][$row];
    $jackkey = $_POST['jackkey'][$row];
    $jackbeg = $_POST['jackbeg'][$row];
    $jackend = $_POST['jackend'][$row];
    $jackbegveh = $_POST['jackbegveh'][$row];
    $jackbegmon = $_POST['jackbegmon'][$row];
    $jackendveh = $_POST['jackendveh'][$row];
    $jackendmon = $_POST['jackendmon'][$row];
    $jacktx = $_POST['jacktx'][$row];
    if ($jacktx == '') { 
        $jacktx = '0'; 
    }

    if (empty($jackkey)) {
        echo 'Skipped empty! <br />';

    } else {
        mysql_query("INSERT INTO `ticket_counts_jackson` VALUES('', '" . $date . "', '" . $jackname . "', '" . $jackkey . "', '" . $jackbeg . "', '" . $jackend . "', '" . $jackbegveh . "', '" . $jackbegmon . "', '" . $jackendveh . "', '" . $jackendmon . "', '" . $jacktx . "')", $mysql_link) or die(mysql_error()); 
        echo 'Added the info the db! <br />';

    }
} 

I use the above to loop through the form and add it to the database. Now for my main question. I also want to add in some javascript to do a little math. Basically ($jackendveh - $jackbegveh) - ($jackendmon - $jackbegmon) and have that displayed in jacktx. Currently the only way I know of adding in the math calculations is to rename each input to a unique name and then rewrite my insert from 1 insert to 8 inserts.

+1  A: 

I would add an ID to each input field like so

        <td><input id="jackname" style="width: 50px" type="text" name="jackname[]" /></td>
        <td><input id="jackkey" style="width: 20px" type="text" name="jackkey[]" /></td>
        <td><input id="jackbeg" style="width: 50px" type="text" name="jackbeg[]" /></td>
        <td><input id="jackend" style="width: 50px" type="text" name="jackend[]" /></td>
        <td><input id="jackbegveh" style="width: 50px" type="text" name="jackbegveh" /></td>
        <td><input id="jackname" style="width: 50px" type="text" name="jackbegmon[]" /></td>
        <td><input id="jackname" style="width: 50px" type="text" name="jackendveh" /></td>
        <td><input id="jackendmon" style="width: 50px" type="text" name="jackendmon[]" /></td>
        <td><input id="jacktx" style="width: 50px" type="text" name="jacktx" disabled /></td>

and then using jQuery you should be able to do it like so

$(document).ready(function(){
   $("input").change(function(){
      var value = $("#jackendveh").val() - $("#jackbegveh").val() - $("#jackendmon").val() - $("#jackbegmon").val();
      $("#jacktx").val(value);
   });
});
Travis
While I would love to delve into jquery I would have no idea how to add it lol.
MrStatic
Correction, I have added the jquery but all I get is NaN
MrStatic
Did you add the ID's to the HTML?
Travis
A: 

i think this would be simple

// Function to save sql in array
function save_sql($table,$data,$ref)
    {
    if(!empty($data)) foreach($data as $k => $v) $str .= "$k = '$v',"; $str = substr($str,0,-1);
    $sql = "INSERT INTO $table SET $str";
    $run = mysql_query($sql) or die(mysql_error() . "-Ref# $ref");
    return $run;
    }
// Extract post arrays into variables
extract ($_POST);

foreach ($_POST['jackname'] as $row=>$name) 
{ 
    $jackname = $name;
    $date = $currentdate[$row];
    if ($jacktx[$row] == '') { 
        $jacktx[$row] = '0'; 
    }

    if (empty($jackkey)) {
        echo 'Skipped empty! <br />';

    } else {
    save_sql("ticket_counts_jackson",array('date'=>$currentdata[$row],
                                            'jackname'=>$name,'jackkey'=>$jackkey[$row],
                                            'jackbeg'=>$jackbeg[$row], 'jackend'=>$jackend[$row])
                                            ,"An error while process your request");
    }
} 
apis17
A: 

I took from @Travis and modified his jquery to work with my situation as follows:

<script type="text/javascript">
$(document).ready(function(){
       $("input").change(function(){
              var value = $("#jackendveh").val() - $("#jackbegveh").val();
              var valuetwo = $("#jackendmon").val() - $("#jackbegmon").val();
              var valuethree = value - valuetwo;
              $("#jacktx").val(valuethree);
           });
        });
</script>

So I will just have unique id's on each form section and have seperate jquery functions for each and use my original sql loop.

MrStatic