Skip to main content
Participating Frequently
October 13, 2009
Question

Problem accessing php variables onsubmit

  • October 13, 2009
  • 2 replies
  • 2208 views

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?

This topic has been closed for replies.

2 replies

Participating Frequently
October 13, 2009

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.

Participating Frequently
October 13, 2009

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

Participating Frequently
October 13, 2009

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.

Inspiring
October 13, 2009

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?

Participating Frequently
October 13, 2009

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

Participating Frequently
October 13, 2009

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.


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