Page 1 of 1

POST into Array problem

Posted: Mon Nov 30, 2009 7:42 am
by koolsamule
Hi Chaps,

I have a repeat region, displaying rows of data: jobid, fromtable, translatorcharge
In each row there is an input field to enter a cost for each job 'charge'.
//INPUT - TRANSLATOR CHARGE

Code: Select all

<input type="text" name="translatorcharge" id="count" class="price" value="<?php echo $row_rsInvPending['jobtranslatorcharge']; ?>"/>
I have a hidden input that collects the information for each row, before sending all the information to a script page:
//HIDDEN INPUT - INFO COLLECTOR

Code: Select all

<?php
$table_name = $row_rsInvPending['fromtable'];
$item_id = $row_rsInvPending['jobid'];
?>
<input type="hidden" name="jobinvsent[]" value="<?php echo $table_name; ?>:<?php echo $item_id; ?>:<?php $_POST['translatorcharge'] ?>" />
The script page then updates the translatorcharge column in the relevant table:
//SCRIPT - TO UPDATE DATABASE

Code: Select all

$allowed_tables = Array('tbl_jobs','tbl_jobtransline','tbl_jobxml'); // to prevent SQL injection
foreach($_POST['jobinvsent'] as $var) {
    $arr = explode(':', $var);
    if(in_array($arr[0], $allowed_tables)) {
        $table = $arr[0];
        $rowid = $arr[1];
        $transcharge = $_POST['translatorcharge'];
        if(is_numeric($rowid)) {
            // run your SQL query here to update $table where row matches $rowid
            $mess = $ref = $_SERVER['HTTP_REFERER']; header( 'refresh: 0; url='.$ref);
            $query = sprintf("UPDATE $table SET jobtranslatorcharge='$transcharge' WHERE jobid=$rowid");
            $result = mysql_query($query, $conndb2) or die(mysql_error());
        }
    else {
  $mess = "<p>There was a problem</p>";
}
    }
}
The problem is, although I'm entering different values for each row, only the last entered value is being passed to the array and therefore updating all rows with the same value.
My questions is, how can I 'individualise' each input, relating to each row?

Re: POST into Array problem

Posted: Mon Nov 30, 2009 9:23 am
by AbraCadaver
$_POST['translatorcharge'] doesn't exist until you actually submit the form so your hidden input doesn't include that. Also, you only submit one "translatorcharge" in the other input. Remove it from the hidden input and use an array in the other input:

<input type="text" name="translatorcharge[]" id="count" class="price" value="<?php echo $row_rsInvPending['jobtranslatorcharge']; ?>"/>

-Shawn

Re: POST into Array problem

Posted: Mon Nov 30, 2009 9:51 am
by koolsamule
Hi Shawn, thanks for the advice, I did what you said and got this returned:
Array
(
[translatorcharge] => Array
(
[0] =>
[1] => 10
[2] => 20
[3] => 30
[4] => 40
)
[jobinvsent] => Array
(
[0] => :
[1] => tbl_jobxml:86
[2] => tbl_jobxml:69
[3] => tbl_jobs:145
[4] => tbl_jobs:144
)
)
How can I 'match up' the values from translatorcharge to jobinvsent to then update the table/jobid/translator charge?

Re: POST into Array problem

Posted: Mon Nov 30, 2009 10:30 am
by AbraCadaver
If you have the inputs paired up then the indexes will match:

Code: Select all

<input type="text" name="translatorcharge[]" ...
<input type="hidden" name="jobinvsent[] ...
 
$_POST['translatorcharge'][0] matches with $_POST['jobinvsent'][0] etc...

Another way to do it would be to add the id as the index:

Code: Select all

<input type="text" name="translatorcharge[<?php echo $row_rsInvPending['jobid']; ?>]" ...
<input type="hidden" name="jobinvsent[<?php echo $row_rsInvPending['jobid']; ?>]" ...
And change your loop accordingly.