Page 1 of 1

POST to Array trouble

Posted: Tue Jan 26, 2010 7:52 am
by koolsamule
Hi Chaps,

I have a Query that calculates a Quote for a job/jobs for a given project.
As there can be more than one job for a project, I have to loop through the query and present the data in a table.
Users have the option to 'override' the estimated quote and enter a 'custom quote'.
The ProjectID, JobID and TableInfo are part of my 1st Array, the estimated/custom quote figure and the 'override' option are part of my 2nd Array.
The information is then POSTed to a script file that joins the information together and then updates the relevant Table/JobID based on the figures and override options.

Example of HTML Code:

Code: Select all

<form action="CompleteQuoteSingle.php" method="post" enctype="multipart/form-data">  <table border="0" cellpadding="0" cellspacing="0">    <caption><input type="submit" id="button" value="Submit" /></caption>    <tr>      <th>Project No.</th>      <th>Project Title</th>      <th>Job Title</th>      <th>Type</th>      <th>Language</th>      <th>Deadline</th>      <th>Document Format</th>      <th>Pages</th>      <th>Word Count</th>      <th>Net Total</th>      <th>EN Proofreading Cost</th>      <th>Total</th>      <th>Admin Override</th>    </tr>    <script type="text/javascript">         $(function() {        var jobquote = $('#jobquote_328');        var value = jobquote.val();        $('#jobadminquote_328').click(function() {          if (jobquote.attr('readonly')) {               jobquote.removeAttr('readonly');               jobquote.val('');        }         else {               jobquote.attr('readonly', 'readonly');               jobquote.val(value);                }        });    });    </script>      <tr>        <td>1111</td>        <td>QuickTrace - Project Template</td>        <td>TEST JOBSHEET</td>        <td>DTP</td>        <td>EN</td>        <td>31/12/2010</td>        <td>MS Word</td>        <td>20</td>        <td>280</td>        <td>£350.40</td>        <td>£ 8.40</td>        <td>£<input type='text' name='jobquote[]' id="jobquote_328" value="358.80" readonly="readonly" /></td>        <td><input type="checkbox" name="jobadminquote[]" id="jobadminquote_328" value="y" /></td>      </tr><input type="hidden" name="jobinfo[]" value="tbl_jobs:328:1111" />      <script type="text/javascript">         $(function() {        var jobquote = $('#jobquote_335');        var value = jobquote.val();        $('#jobadminquote_335').click(function() {          if (jobquote.attr('readonly')) {               jobquote.removeAttr('readonly');               jobquote.val('');          } else {               jobquote.attr('readonly', 'readonly');               jobquote.val(value);          }        });    });    </script>      <tr>        <td>1111</td>        <td>QuickTrace - Project Template</td>        <td>TEST</td>        <td>DTP</td>        <td>CZ</td>        <td>31/12/2010</td>        <td>InDesign CS4</td>        <td>654</td>        <td>280</td>        <td>£ 50.40</td>        <td>£ 0.00</div></td>        <td>£<input type='text' name='jobquote[]' id="jobquote_335"  class='price' value="50.40" readonly="readonly" /></td>        <td><input type="checkbox" name="jobadminquote[]" id="jobadminquote_335" value="y" /></td>      </tr><input type="hidden" name="jobinfo[]" value="tbl_jobs:335:1111" />    </table>
CompleteQuoteSingle.php

Code: Select all

$allowed_tables = Array('tbl_jobs','tbl_jobtransline','tbl_jobxml'); // to prevent SQL injection
 $i = 0;
foreach($_POST['jobinfo'] as $var) {
    $arr = explode(':', $var);
    if(in_array($arr[0], $allowed_tables)) {
        $table = $arr[0];
        $rowid = $arr[1];
        $projid = $arr[2];
        $setprice = $_POST['jobquote'][$i];
        $adminoverride = $_POST['jobadminquote'][$i];
        $i++;
        if(is_numeric($rowid)){
            if($adminoverride=='y') {
            // run your SQL query here to update $table where row matches $rowid
            $query = sprintf("
            UPDATE $table 
            SET jobquote='$setprice', jobquotecomplete='y', jobadminquote='y'
            WHERE jobid=$rowid");
            //$result = mysql_query($query, $conndb2) or die(mysql_error());
            //$mess = $ref = $_SERVER['HTTP_REFERER']; header( 'refresh: 0; url=../../projects/project_details.php?id='.$projid);
        }
            else {
            // run your SQL query here to update $table where row matches $rowid
            $query = sprintf("
            UPDATE $table 
            SET jobquote='$setprice', jobquotecomplete='y', jobadminquote='n'
            WHERE jobid=$rowid");
            //$result = mysql_query($query, $conndb2) or die(mysql_error());
            //$mess = $ref = $_SERVER['HTTP_REFERER']; header( 'refresh: 0; url=../../projects/project_details.php?id='.$projid);
            }
        }
    }
}
My problem is:
The Override option only gets passed to the Array, if selected. This means that if I have two jobs, and I select the override option for the second job, the array looks like this:
Array
(
[jobquote] => Array
(
[0] => 358.80
[1] => 100
)

[jobinfo] => Array
(
[0] => tbl_jobs:328:1111
[1] => tbl_jobs:335:1111
)

[jobadminquote] => Array
(
[0] => y
)

)
Question:
Is there a way of POSTing a default value of 'n' for the 'jobadminquote' checkbox, so that the above would look like:
[jobadminquote] => Array
(
[0] => n
[1] => y
)
I hope this is clear?!

Re: POST to Array trouble

Posted: Tue Jan 26, 2010 10:51 am
by JakeJ
Before writing to the array, do this:

Code: Select all

if($_POST['jobadminpost'] == null) {
    $_POST['jobadminpost'] = 1 # or "no" or whatever you want it to.
}
   
You wouldn't even necessarily need to write that variable to an array in that case. It's either yes or no. But if you want it to be an array with both yes and no elements anyway create the array from within the IF statement, just do an ELSE for the alternative.

Re: POST to Array trouble

Posted: Tue Jan 26, 2010 11:05 am
by AbraCadaver
To answer your question, no. Checkboxes are only included in the $_POST array if they are checked. Here is how I handle this. Actually number your array indexes so that you know which checkbox is which and with which of the other inputs they should be related. You would probably do this in your loop and use an incrementing var:

Code: Select all

<input type='text' name='jobquote[1]' id="jobquote_328" value="358.80" readonly="readonly" /></td>
<td><input type="checkbox" name="jobadminquote[1]" id="jobadminquote_328" value="y" /></td>
</tr><input type="hidden" name="jobinfo[1]" value="tbl_jobs:328:1111" />
You would then need to change the processing to look at the jobadminquote that corresponds with the index of the jobinfo (something similar to this):

Code: Select all

foreach($_POST['jobinfo'] as $key => $var) {
    $arr = explode(':', $var);
    if(in_array($arr[0], $allowed_tables)) {
        $table = $arr[0];
        $rowid = $arr[1];
        $projid = $arr[2];
        $setprice = $_POST['jobquote'][$key];
        //$adminoverride = $_POST['jobadminquote'][$i];
        //$i++;
        if(is_numeric($rowid)){
            if(isset($_POST['jobadminquote'][$key])) {  //changed to use $key
            // run your SQL query here to update $table where row matches $rowid
            $query = sprintf("
            UPDATE $table
            SET jobquote='$setprice', jobquotecomplete='y', jobadminquote='y'
            WHERE jobid=$rowid");
            //$result = mysql_query($query, $conndb2) or die(mysql_error());
            //$mess = $ref = $_SERVER['HTTP_REFERER']; header( 'refresh: 0; url=../../projects/project_details.php?id='.$projid);
        }
HTH