Validate input value against calculated value

Discussion in 'PHP' started by koolsamule, Jan 20, 2010.

  1. #1
    Hi Chaps,

    Need a bit of guidence with some PHP code.

    I have a Query that estimates a quote ($price_total) for a job.

    The estimate ($price_total) is the value of an input (jobquote), and the database is updated once the form is submitted (using a seperate script.php page).

    What I need, is to validate the entered value of 'jobquote' against the estimated value of $price_total, just incase a 'custom' price has been agreed with a customer.

    If the values are different, then I need an 'admin override' radio button (admin_quote enum('y','n')) to appear.

    If someone can help or point me in the right direction, I'd be most grateful.

    Cheers
     
    koolsamule, Jan 20, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Which part are you having trouble with?
     
    SmallPotatoes, Jan 20, 2010 IP
  3. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Should have been a bit clearer I guess,

    $price_total=['calculation']
    <input type='text' name='jobquote' id="count" class='price_warning' value="<?php echo number_format($price_total, 1, '.', '').'0'; ?>"/>

    So $price_total is the default value of the input, but it can be changed, and if changed, i need the 'admin override' option to appear.. . .

    I don't know how to start . . .I've checked out Adobe Spry, but can;t find anything that will do the trick
     
    koolsamule, Jan 20, 2010 IP
  4. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #4
    When the quote page loads retrieve the value from the database and store it then when the user enters the value post it to the same page and do the valudation if its false supply an override facility
     
    WebWorth, Jan 20, 2010 IP
  5. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That sounds like what I need to do, but don't know how to start?
     
    koolsamule, Jan 20, 2010 IP
  6. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is what I've got so far, but it doesn't show any validation . . .
    <input type='text' name='jobquote' value="<?php echo $price_total; ?>"/>
    <input type='hidden' name='orignal_jobquote' value="<?php echo $price_total; ?>"/>
    <?php
    	if ($_POST['original_jobquote'] != $_POST['jobquote'])
    		{ ?>
            	<span id="spryradio1">
            	<input type="radio" name="jobquoteadmin" value="y" id="radio" />Confirm<br />
            	<span class="radioRequiredMsg">Please confirm Admin Override</span></span>
            <?php };
    ?>
    Code (markup):
     
    koolsamule, Jan 20, 2010 IP
  7. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #7
    I dont think your a million miles away there, what do you mean it dosnt show any validation?

    is the 'if' statement not the validation ?

    Write out in pseudo what you want to do

    eg
    1. Get Value
    2. Compare Value
    3. If true then display form else display nothing
    4. complete
     
    Last edited: Jan 20, 2010
    WebWorth, Jan 20, 2010 IP
  8. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi, the 'if' statement doesn't work. . . i.e., if orignal_quote = 350 and jobquote = 100, the radio button should appear but it doesn't.

    psudo:

    1. Get $price_total value
    2. Compare $price_total against value entered in 'jobquote' input
    3. If same, pass jobid and jobquote info to script.php page (which is working at the moment), if different, force user to click Confirm (admin_override), jobid, jobquote and admin_override values get passed to script.php to update database
    4. Complete

    Does that sound right?
     
    koolsamule, Jan 20, 2010 IP
  9. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #9
    Looks like you are doing it the right way :)

    have you tried echoing the two values are they of the same data type?

    try

    $original_quote = intval($_POST['original_jobquote']);
    $jobquote = intval($_POST['jobquote']);
    
    echo $original_quote;
    echo $jobquote ;
    
    if ($original_quote != $jobquote) {
    
    echo "values diffrent";
    }
    
    else 
    {
    echo "values the same";
    }
    PHP:
     
    WebWorth, Jan 20, 2010 IP
  10. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #10
    dont know if you noticed but name='orignal_jobquote' is diffrent to $_POST['original_jobquote']

    "spelling" :)


     
    WebWorth, Jan 20, 2010 IP
  11. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    ahhh...that might be it. . . .i'll check it tomorrow and let you know.
    thanks for your help today, might need it again! cheers
     
    koolsamule, Jan 20, 2010 IP
  12. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Hi WebWorth, thanks for your help yesterday, I think we're on the right track, but have a few problems.

    I checked the data types, and the spelling and all is OK now.

    Problem 1. The information is '$_POST'ed to a script file, not to the page itself and at the moment, the $_POST takes place before the PHP validation takes place. If I remove the link to the script page, the validation works.

    Problem 2. (link to script removed for testing) If I change the value (from the default 'original_quote'), then submit, the page reloads, the 'Confirm' radio button appears, but the value of 'job_quote' has reverted back thte default:
    1. Start - job_quote = £350
    2. Change - job_quote = £100
    3. Submit
    4. Page reloads - job_quote = £350, confirm appears

    Is there a way around this?
     
    koolsamule, Jan 21, 2010 IP
  13. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #13
    Can you post your code?

    Thanks
     
    WebWorth, Jan 21, 2010 IP
  14. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    OK, hopefully this will give you an idea:

    Quote.php // Query (note: there are 2 more tables linked together as part of a UNION query, which I have removed for simplicity)
    <?php 
    $colname_rsInvQuote = "-1";
    if (isset($_GET['id'])) {
      $colname_rsInvQuote = $_GET['id'];
    }
    $colname_rsInvQuote2 = "-1";
    if (isset($_GET['proj'])) {
      $colname_rsInvQuote2 = $_GET['proj'];
    }
    mysql_select_db($database_conndb2, $conndb2);
    $query_rsInvQuote = sprintf("
    SELECT      tbl_projects.projid, 
    			tbl_projects.projtitle, 
    			tbl_projects.projdue, 
    			DATE_FORMAT(tbl_projects.projdue, '%%d/%%m/%%Y') as projdue_format, 
    			tbl_projects.projtype,  
    			tbl_projects.projcat, 
    			tbl_projects.FK_custid, 
    			tbl_languaget.langtname,
    			tbl_doctype.doctypename,
    			tbl_jobs.jobid, 
    			tbl_jobs.FK_projid,               
    			tbl_jobs.jobname,               
    			tbl_jobs.FK_langid,               
    			tbl_jobs.jobpages,               
    			tbl_jobs.jobshipped, 
    			tbl_jobs.jobinvsent,
    			tbl_jobs.jobquote,	     
    			tbl_customers.custid,
    			tbl_costs.costcat,
    			tbl_costs.costbase,   
    			tbl_costs.costnm_84,   
    			tbl_costs.cost85_99,   
    			tbl_costs.cost100,   
    			tbl_costs.costrep,   
    			tbl_jobs.wordsgross,
    			(tbl_jobs.wordsgross * costbase as translationcost,
    	 		(tbl_jobs.wordsgross) * costproof_en as proofreadingcost,
    			tbl_jobs.jobquotecomplete, 
    			'tbl_jobs' as fromtable 
    FROM 		tbl_projects   
    INNER JOIN 	tbl_jobs               
    			ON tbl_projects.projid=tbl_jobs.FK_projid   
    INNER JOIN 	tbl_languaget               
    			ON tbl_languaget.langtid=tbl_jobs.FK_langid   
    INNER JOIN 	tbl_customers               
    			ON tbl_customers.custid=tbl_projects.FK_custid 
    INNER JOIN 	tbl_costs
    			ON tbl_costs.FK_custid=tbl_customers.custid  
    INNER JOIN 	tbl_doctype
    			ON tbl_doctype.doctypeid=tbl_jobs.FK_doctypeid 	     
    WHERE 		tbl_projects.FK_custid = %s
    			AND	tbl_projects.projid = %s
    			AND tbl_costs.costcat = %s
    ORDER BY 	tbl_projects.projid ASC", 
    GetSQLValueString($colname_rsInvQuote, "int"), GetSQLValueString($colname_rsInvQuote2, "int"), GetSQLValueString($colname_rsCosts2, "text"));
    $rsInvQuote = mysql_query($query_rsInvQuote, $conndb2) or die(mysql_error());
    $row_rsInvQuote = mysql_fetch_assoc($rsInvQuote);
    $totalRows_rsInvQuote = mysql_num_rows($rsInvQuote);
    ?>
    PHP:
    Quote.php // Table
    <form action="../../admin/scripts/CompleteQuoteSingle.php" method="post" enctype="multipart/form-data">
    <table border="0" cellpadding="0" cellspacing="0" id="tblreport_invoice">
        <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>
        </tr>
          <?php if ($totalRows_rsInvQuote > 0) { // Show if recordset not empty ?>
        <?php do { ?>
          <tr>
              <td><?php echo $row_rsInvQuote['projid']; ?></td>
            <td><?php echo $row_rsInvQuote['projtitle']; ?></td>
            <td><?php echo $row_rsInvQuote['jobname']; ?></td>
            <td><?php echo $row_rsInvQuote['projtype']; ?></td>
            <td><?php echo $row_rsInvQuote['langtname']; ?></td>
            <td><?php echo $row_rsInvQuote['projdue_format']; ?></td>
            <td><?php echo $row_rsInvQuote['doctypename']; ?></td>
            <td><?php echo $row_rsInvQuote['jobpages']; ?></td>
            <td><?php echo $row_rsInvQuote['wordsgross']; ?></td>
            <td><?php echo number_format($row_rsInvQuote['translationcost'], 1, '.', '').'0'; ?></td>
            <td><?php echo number_format($row_rsInvQuote['proofreadingcost'], 1, '.', '').'0'; ?></td>
            <td><?php $price_total = ($row_rsInvQuote['translationcost']+$row_rsInvQuote['proofreadingcost']); ?>
            <input type='text' name='jobquote[]' value="<?php echo number_format($price_total, 1, '.', '').'0'; ?>"/>
            <input type='hidden' name='original_jobquote[]' value="<?php echo number_format($price_total, 1, '.', '').'0'; ?>"/>
            <?php
    			if ($_POST['original_jobquote'] != $_POST['jobquote'])
    				{ ?>
            		<span id="spryradio1">
            		<input type="radio" name="jobquoteadmin" value="y" id="radio" />Confirm<br />
            		<span class="radioRequiredMsg">Please confirm Admin Override</span></span>
            		<?php };
    		?>
            </td>
          </tr>
            <?php
    		$table_name = $row_rsInvQuote['fromtable'];
            $item_id = $row_rsInvQuote['jobid'];
    		$proj_id = $row_rsInvQuote['projid'];
    		?>
            <input type="hidden" name="quote[]" value="<?php echo $table_name; ?>:<?php echo $item_id; ?>:<?php echo $proj_id; ?>" />
          <?php } while ($row_rsInvQuote = mysql_fetch_assoc($rsInvQuote)); ?>
        <?php } // Show if recordset not empty ?>
      </table>
    HTML:
    CompleteQuoteSingle.php
    <?php
    $allowed_tables = Array('tbl_jobs','tbl_jobtransline','tbl_jobxml'); // to prevent SQL injection
     $i = 0;
    foreach($_POST['quote'] 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];
    		$i++;
            if(is_numeric($rowid)) {
                // run your SQL query here to update $table where row matches $rowid
                $query = sprintf("
    			UPDATE $table 
    			SET jobquote='$setprice', jobquotecomplete='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 {
      $mess = "<p>There was a problem</p>";
    }
    	}
    }
    ?>
    
    PHP:
     
    koolsamule, Jan 21, 2010 IP
  15. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #15
    What is $_POST['jobquote'][$i]; and when is it set?

    $_POST['jobquote'] is a value of post / $_POST['jobquote'][$i] doesnt exist

    $_POST is an array try doing

    print_r($_POST);

    to see its contents if post is blank then nothing has been posted to that page

    the names you have in the post form have [] after them why is this?

    sory im not home right now so cant test the code
     
    WebWorth, Jan 21, 2010 IP
  16. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Hi WebWorth, sorry if this is a bit of a pain.
    I'm having to use arrays, due to there being more than one 'job' per project.
    That is why I'm using a loop to retrieve all the jobs linked to a given project.
    Each job has it's own quote, based on costbase*wordsgross(+others if applicable)
    I'm using an array to POST the project id, job id and the table it came from, and another array to POST the jobquote value. The CompletSingleScript.php file then sets the jobquote value to the corresponding table/project/job
    $_POST['jobquote'][$i] is in the CompletSingleScript.php, and hopefully it will make a bit more sense if you can test the code.
     
    koolsamule, Jan 21, 2010 IP
  17. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #17
    can we chat on google chat?
     
    WebWorth, Jan 21, 2010 IP
  18. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Hi WebWorth, thanks for the offer, but I think I may have found a jQuery solution, courtesy of David Power@AdobeForums:
    <script type="text/javascript" src="scripts/jquery-1.4.min.js"></script>
    <script type="text/javascript">
    $(function() {
         var jobquote = $('#jobquote');
         var value = jobquote.val();
         $('#editquote').click(function() {
              if (jobquote.attr('readonly')) {
                   jobquote.removeAttr('readonly');
                   jobquote.val('');
              } else {
                   jobquote.attr('readonly', 'readonly');
                   jobquote.val(value);
              }
         });
    });
    </script>
    </head>
    
    <body>
    <?php
    if ($_POST) print_r($_POST);
    ?>
    <form id="form1" name="form1" method="post" action="">
      <p>
        <label for="jobquote">Quote:</label>
        <input name="jobquote" type="text" id="jobquote" value="£200" readonly="readonly" />
        <input type="checkbox" name="editquote" id="editquote" value="y" />
        <label for="editquote">Edit this value</label>
      </p>
      <p>
        <input type="submit" name="send" id="send" value="Submit" />
      </p>
    </form>
    </body>
    </html>
    Code (markup):
     
    koolsamule, Jan 22, 2010 IP