POST into Array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

POST into Array problem

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: POST into Array problem

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: POST into Array problem

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: POST into Array problem

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply