Copy link to clipboard
Copied
I am constructing a webpage page that allows commercial fishermen to transfer their individual quota to another fisherman. I have used dw8, on a server using mysql. The page I am having problems with has an insert data form that the fisherman completes. When submitted this form adds this record to a table storing such transactions. At the same time, the amount of quota transferred is subtracted from fisherman #1's account and added to fisherman #2's account. This all works well. However, I wish to add a verification onsubmit that would check fisherman #1's account balance to ensure that he has sufficient quota prior to the transfer. If not the transfer would not occur. Currently, the site will allow a deficit to occur. This is not a good way to do business.
I have approached this by attempting to obtain two pieces of information (in addition to identifiers). One is the current transfer amount which would come from the current form. I have used $quotapounds = $_post[quotapounds] to obtain this piece. And fishermen #1's current balance, which I have obtained from the database using a select command. I have determined that both of these pieces of data are actually there by substituting them in commands that modify data tables.
My problem is that to have this verification occur client side I need to get them into javascript to be used in a function that can be called onsubmit. The have tried lines such as: quotapounds = <?php echo $quotapounds ?>, but I do not seem to be obtaining any values. My goal is to use these two variables in an if statement where If (balance - quotapounds) < 0 then an alert would pop-up informing the fisherman that he does not have enough quota to make the transaction.
Can anyone make some helpful suggestion on how I should be doing this?
Copy link to clipboard
Copied
The form method is set to POST, not GET, right? Also, you typed it as:
$quotapounds = $_post[quotapounds]
...and should be:
$quotapounds = $_POST['quotapounds'];
Is it that way on the actual page?
Copy link to clipboard
Copied
Nate
Thanks for the response. Yes to both questions, it is $_POST['quotapounds']. I am confident that both variables $quotapounds and $balance that I obtain are valid and contain the information that I need. My problem appears to be in transforming them into javascript. Everything that I have read says that this is easy using the echo statement as I have done. But it does not seem to work. This forum does not allow cut and paste of code otherwise I would post it here.
Pete
Copy link to clipboard
Copied
Generally it's pretty straightforward (just echo or print the values into the JavaScript code). If you hard code a value, does it transfer? Something like:
<?php $quotapounds = 23; ?>
<script type="text/javascript">!--
var quotapounds = <?php echo $quotapounds; ?>;
alert(quotapounds);
//-->
</script>
Copy link to clipboard
Copied
Nate
I attached a Word file with a bit of cut and paste from my code. There is the javascript function and the <form> tag with the onsubmit code.
When I run the code as it is shown I received no alerts at all. Even though I know there will be a deficit in the account, the code simply processes the entry. I took your suggestion and hard wired values to the php variables. I then received the "true" alert notice. If I switched the > and < for the inqualilty of balance - quotapounds, I still recieve the true response even though the answer is opposite. I never reveived the alert telling me that I do not have enough in my account.
I may well have the function all fouled-up, but it is odd that hard wiring generated a response, while as variables there was none.
Pete
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Could be a different issue, but I see in the form tag this onsubmit trigger:
checkDeficit(this);
...so it's passing a reference to the form object to your function. But the function is expecting two parameters, one for the balance and the other for the quotapounds:
function checkDeficit(balance, quotapounds)
If you check the source code in the browser, is the PHP successful in writing the $balance variable into the JavaScript? If so, make sure JavaScript errors are enabled - looks like that might be where the problems are.
Copy link to clipboard
Copied
Nate
I straightened out the function call but that did not solve the problem. As far as looking at the source code, once I hit submit the data is transfered and the form is refresh and there is nothing left to see. Unless I am unaware of some trick.
Pete
Copy link to clipboard
Copied
I guess I don't follow how the onsubmit event ties in. If the variables are only available after the form has been submitted, then they aren't passed to JavaScript until after it's too late? If it's important, I wouldn't rely on JavaScript at all. Do the check in PHP before the part of the script that updates the database record. If it's not valid, print out an error message or redirect to an error page instead of processing the form.
Could also use JavaScript and onsubmit to supplement the PHP condition/check, but wouldn't rely on just the JavaScript. If you went that route, you'd need to pass the balance to JS as before, but then pull the quotapounds from the form field (assuming the user enters a number of pounds in a field to transfer).
That being said, it's highly possible that I'm completely lost at what you're doing 🙂
Copy link to clipboard
Copied
Nate
I am no expert, but my understanding is that when the javascript function is called by the onsubmit action, it does its thing and if the result is false, then the data is not submitted. If it is true, then the data is submitted to the server. This is the sort of action that occurs with data validation on webpage input forms for data missing in fields or the wrong data type. I have also read that the onsubmit can call only one function, but that serveral functions can be nested into one. I need to get this one to work before I try that.
Pete
Copy link to clipboard
Copied
That's all basically true, but I'm assuming "quotapounds" is something that's being entered into the form by the visitor, right? If that's the case, the value needs to be pulled by JavaScript from the form field, not supplied by PHP (since PHP is processed before the page is fed to the browser, you can't access the $_POST variables for the form until after it's submitted (so, it's available to the script that the form is submitted to, not from the form page itself). Looks like you're expecting the $_POST data to update as the form is filled in, and it's not. Also, JavaScript isn't always enabled on the browser, so if it's an important feature, don't rely entirely on JavaScript.
So, what I generally do is go ahead and use JavaScript with the onsubmit event (in your case, grabbing the balance from PHP as the page loads, and the quotapounds from a form field value) to prevent the form from submitting if the values are invalid. Then, in case JavaScript is disabled and it submits anyway, include a similar conditional in the PHP script that the form is submitted to.
If I'm understanding your application at all (and again, I might not be), then you'd want something like this for the JavaScript:
<script type="text/javascript">
var balance = <?php echo $balance; ?>;
function checkDeficit(thisForm) {
// get the quotapounds and make sure it's a number
var quotapounds = thisForm.quotapounds.value;
quotapounds = parseFloat(quotapounds);
if (isNaN(quotapounds)) quotapounds = 0;
// validate form before allowing submit
var msg = "";
if (quotapounds <= 0)
msg = "Please enter the number of pounds to transfer.";
else if (quotapounds > balance)
msg = "You can't transfer more than what's available.";
if (msg != "") {
alert(msg);
return false;
} else return true;
}
</script>
...then this in your form tag, using the "this" keyword:
<form
action="<?php echo((isset($_POST["ID"]))?$_POST["ID"]:"") ?>"
method="post" name="form1"
onsubmit="return checkDeficit(this);">
...and a back-up check similar to this in the PHP script it's submitted to:
<?php
$quotabalance = (isset($_POST['quotabalance']))
? (float) $_POST['quotabalance'] : 0;
if ($quotabalance <= 0) { /*flag empty value error*/ }
if ($quotabalance > $balance) { /*flag invalid value error */ }
?>
Copy link to clipboard
Copied
Nate
First I wish to thank you for your thoughts and comments, they have helped me think more clearly. It occurred to me late last night that even if I "capture" the input value for quotapounds from the current form so that I can use it in javascript; I have one more problem and that is with $balance. I have pulled this record from the database but all I really have is a row of data which includes the current balance for every speceis that fisherman has quota for. I would also need to "capture" the species ID from the current form to proper identify which value in that row is the one I need. I have been doing this upon submit and wrongly thinking that I had the value that I need.
I am going to put this effort aside for now. I plan on using the current form as a input form that upon submit returns a "preview" form with all of the data that I need pulled from the database. If the balance if sufficient and the fisherman wishes to continue, then he will be asked "complete" the transaction.
Thanks again.
Pete
Copy link to clipboard
Copied
Sounds like a good solution. You can always enhance it with JavaScript later if you're feeling crazy. Good luck!
Copy link to clipboard
Copied
There are many ways to do this, but you might want to consider using a stored procedure. The sproc would take the transaction details as input parameters, and return a status message as an output parameter. If there is insufficient balance, the status message would indicate that the transaction failed, otherwise it would indicate success. I think this would also be the simplest solution.
Copy link to clipboard
Copied
bregent
I am willing to try anything at this point. I thought that this was going to fairly simple. Can you point me in the right direction to read-up on what exactly you are proposing. That said, the approach that I have tried seems to be a common use of onsubmit and I sure would like to learn what it is that I am doing wrong.
Pete
Copy link to clipboard
Copied
You may want to continue down your current path just so you can learn that technique, but I think the stored procedure is a better approach as it only requires one trip to the database. To learn more about stored procedures:
http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.html
So, your stored procedure would contain input parameters like the accounts transferring from and to as well as the amount. The procedure would first run a SQL select to determine if the 'From' account had sufficient balance. If it doesn't, then return the message in the output parameter. If the balance is good, proceed to transfer the balance and then return the success message.