Skip to main content
January 20, 2010
Answered

Validate input value against calculated value

  • January 20, 2010
  • 1 reply
  • 813 views

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

This topic has been closed for replies.
Correct answer David_Powers

Hi David,

Thanks for your reply. I think I get what you mean, can I just clarify before I continue. . . .

  • The default value of the 'jobquote' input is caluclated when the page loads.
  • The user only has one value to change, the 'jobquote' input.
  • And it's if this default value is changed, that I need an 'Admin Override' radio button to confirm he change in the quote

Do you know any jQuery? I think the following might help with what you're trying to do:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make field editable</title>
<script type="text/javascript" src="js/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>

For it to work, you need to download jquery-1.4.min.js from jquery.com. If you test it, you'll see that it clears the preset value from the field and allows you to edit it when the checkbox is selected. However, if you deselect the checkbox, it reverts to the original value.

1 reply

January 21, 2010

Got a bit further:



PHP Code:

<input type='text' name='jobquote' value="<?php echo $price_total; ?>"/>

        <input type='hidden' name='original_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 };
?>
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?
David_Powers
Inspiring
January 21, 2010

It sounds to me as though you need a page that contains a form to get the job quote. Submit that form to a second page that calculates the quote and inserts the result (plus any other information brought over from the first page) into a form where the user continues entering the other details. Then process the complete set of details when the form in the second page is submitted.

January 22, 2010

Do you know any jQuery? I think the following might help with what you're trying to do:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make field editable</title>
<script type="text/javascript" src="js/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>

For it to work, you need to download jquery-1.4.min.js from jquery.com. If you test it, you'll see that it clears the preset value from the field and allows you to edit it when the checkbox is selected. However, if you deselect the checkbox, it reverts to the original value.


Bingo!

I have heard jQuery but haven't had a chance to research it fully.

This looks like something that will deffinately do the trick

Sweet, cheers David