Jump to content

POST into Array problem


kool_samule

Recommended Posts

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

<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

<?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

$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?

Link to comment
Share on other sites

I helped him via IM.

just in case someone stumbles upon this in the years to come, the solution is below.

 

The problem was here:

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

<?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'] ?>" />

 

He expected the user-entered value of the text input field to be in the hidden input field.

So he just made the text input's name an array, and on the processing page used parallel arrays ( $jobsinvent[], $translatorcharge[]) to update query.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.