Jump to content

POST to Array trouble


kool_samule

Recommended Posts

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:

<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

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

Link to comment
https://forums.phpfreaks.com/topic/189863-post-to-array-trouble/
Share on other sites

Thanks for the reply,

 

I have donew that:

<td><input type="checkbox" name="jobadminquote[]" id="jobadminquote_335" value="y" /></td>

Don't think thats the problem though, the problem is when one job doesn't have the checkbox ticked, which results in the jobadminquote array becoming out of sync.

I think I need something to POST a default value of 'n', which will keep the array in check?

Sorry! OK, I have changed it to:

<input type="checkbox" name="jobadminquote[<?php echo $row_rsInvQuote['jobid'];?>]" id="jobadminquote_<?php echo $row_rsInvQuote['jobid'];?>" value="y" />

But I'm guessing this will force me to change the script in someway?

Am I correct in thinking that if I change:

name='jobquote[328]'

name="jobadminquote[328]"

Then I'll have to change the $i value (in the script file) in some way?

$i = 0;

.....

$setprice = $_POST['jobquote'][$i];

$adminoverride = $_POST['jobadminquote'][$i];

..In order to link the arrays correctly to update the database?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.