Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Problem using PHP variables in javascript

    I am working on a webpage that allows commercial fishermen to transfer their share of the total quota to another fisherman. The database is comprised of several tables within Mysql on a remote server.

    The page that I am working on now is an record insert form that the fisherman would complete to record a transfer. This record goes to one table in the database. At the same time, his account balance, in another table, is reduced by the amount he has transfered; and the #2 fisherman, that receives the transfer has his account increased. This all works fine. However, as it stands, account balances can go negative.

    I wish to have an onsubmit form handler check fisherman #1's balance and if there is sufficient quota in his account, then to submit the transfer. If there is not then he would receive a message to that effect and the submit would not occur.

    There are two pieces of data that I need. One is the transfer amount which is found on the form on this current page, and the other is the fisherman's account balance which is in the database. With these I can use a javascript function and test whether balance minus transaction is equal to or greater than zero. The onsubmit handler can call this function.

    I have worked out the code to make this work, if I hard wire the values of the variables, but I can not get it to work when the variables in PHP are populated and then converted into javascript. The PHP variables work fine in inserting data into the database tables so I believe that my problem is in the conversion into javascript. But everything that I have read indicates that I should not be having this problem. Below is my code:

    This is from the current form and is the value for the amount of quota being transfered.

    Code:
    $quotapounds = $_POST['quotapounds'];
    This is the code obtaining the account balance from the database. The variable is $row which is then subsripted based upon the species of fish the quota applies to, and $w is the subsript. There are around 30 species so this is only a partial list. The subscript assignment is lower down.

    Code:
    $query= "SELECT * FROM quotapounds WHERE ID ='$ID'";
    $result=mysql_query($query);
    $row=mysql_fetch_row($result);
    
    
    if ($species == 'Lingcod') {$ling = $quotapounds; $w = 2;}
    if ($species == 'Pacific_Cod') {$Pcod = $quotapounds; $w = 3;}
    if ($species == 'Pacific_Whiting') {$hake = $quotapounds; $w = 4;}
    if ($species == 'Sablefish_N') {$sablen = $quotapounds; $w = 5;}
    if ($species == 'Sablefish_S') {$sables = $quotapounds; $w = 6;}
    This is the subscript assignment to $row and designation of $balance.

    Code:
    $balance = $row[$w];
    This is the javascript function.

    Code:
    <script type="text/javascript">
    var quotapounds = <?php echo $quotapounds; ?>;
    var balance = <?php echo $balance; ?>;
    function checkDeficit(){
      if ((balance - quotapounds) < 0){
        alert("You can not transfer more than is currently in your account");
        return false;
      }
      else
        alert("it could work...");
      return true;
    }
    </script>
    And lastly, this is the HTML <form> tag with the onsubmit handler.

    Code:
    <form action="<?php echo((isset($_POST["ID"]))?$_POST["ID"]:"") ?>" method="post" name="form1" onsubmit="return checkDeficit();">
    Again, if I hard wire values to the variables $quotapounds and $balance, this works just fine. I can set one high and the other low and cause a deficit or switch them and that works the other way. But this does not work with the php assignment to javascript of just the variable name.

    Any suggestion would be greatly appreciated.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    First, good job with a thorough post. Refreshing after so many "my codes are broke wht wrong hlp plzzz".

    You don't ever show a full, contiguous portion of code, so I'm not sure how your logic is set up. Are you pulling all of the user's counts at the beginning of the form? Or are you trying to do that in the onsubmit? If trying in the onsubmit, that's could be your problem as you need AJAX for live interaction with the backside. Javascript doesn't talk to the database otherwise.

    A larger section of code (or pseudocode) would help to determine where it's going screwy.
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for the reply. The code you see is in the same order that it appear in my code. The $quotapounds is coming from the current form. The $balance is from the database. I have used both of these variable to write their contents into update section where the transactions are recorded. They both are producing the correct values. That way I am puzzled as to what is going on.

    If it would be helpful I could paste the entire code here but there are around 500 llines which I figured was way more than anyone would really be willing to look at.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    What do you mean when you say, "The $quotapounds is coming from the current form"? In the original post, it shows that it's a POSTed value. If it's coming through POST, it's coming from somewhere else.

    Else, if you mean that your intent is to use a value from the current form, then you need to code that as a Javascript variable.

    PHP POST vars will only take on values during the full submit-to-server form submission process.
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    You may have identifying what I am doing wrong. Below is a bit more code that appears a few lines prior to the beginning of the html section. This is where the data from the current form is being sent to the database.


    Code:
    $insertSQL = sprintf("INSERT INTO qptransfer2 (ID, newownerID, `month`, `day`, `year`, quotapounds, species) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                           GetSQLValueString($_POST['ID'], "text"),
                           GetSQLValueString($_POST['newownerID'], "text"),
                           GetSQLValueString($_POST['month'], "text"),
                           GetSQLValueString($_POST['day'], "text"),
                           GetSQLValueString($_POST['year'], "text"),
                           GetSQLValueString($_POST['quotapounds'], "double"),
                           GetSQLValueString($_POST['species'], "text"));
    
      mysql_select_db($database_connifq, $connifq);
      $Result1 = mysql_query($insertSQL, $connifq) or die(mysql_error());

    Higher up is where I had place the $quotapounds = $_POST['quotapounds'] code.

    So at this point I need some help with code that can capture the variable quotapounds as a javascript variable.

    Based upon what you are telling me, I should not have the same problem with $balance because it was pulled from the database. I am correct?

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Yep, that's what it is. And yes, $balance is populated because it's pulled from the db.

    You may be confused on PHP/javascript order. When the browser requests a page, PHP on the web server "builds" it and sends it through the wire back to the browser. PHP is done at this point. Any javascript in the page is run in the browser - completely apart from PHP. Oversimplified, perhaps, but maybe will help if that's your confusion.

    That insert probably doesn't work, either, if I understand your thinking.

    It looks like you're trying to do form handling without a page reload. That requires AJAX, which is just a special way of getting the javascript on a page to talk to the server without the necessity of the full form submission by the browser. If that's what you want, you'll need to restructure your logic and fetch a Javascript framework to help with the AJAX (much easier than hand-coding it). Jquery seems to be the popular kid these days.

    If, however, you just want live validation (feedback on errors) and don't mind a full client-server form handling cycle (for the db insert), you can stick with much of what you already have and change only a little (I think). For the validation, you just need to have your function fetch the value of the input using something like
    Code:
    var quotapounds = document.formId.inputId.value
    or
    Code:
    var quotapounds = document.getElementById('inputId').value
    . If the values pass javascript validation, have the form submit to a new form handler page where your insert code will need to be. You'll also want to re-validate the values here since Javascript can be easily fooled.

    There are a dozen other things I could explain, but you may be hitting information overload as it is. If you check out some PHP form handling tutorials for second opinions, they should more or less follow these steps.
    Are you a Help Vampire?

  • #7
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your help. Sometimes I know just enough to be dangerous. So with that thought. Taking your suggestion for a quick fix, I would modify the suggested code to look something like this:

    var quotapounds = document.form1.quotapounds.""

    or I am once again missing something?

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    I'm not sure where those double-quotes come in. It should be the term 'value', I think. Just as written in the examples above.
    Are you a Help Vampire?

  • #9
    New to the CF scene
    Join Date
    Oct 2009
    Location
    Perhaps on the Internet...
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You know, I still don't understand from where you get that $quotapounds variable.

    I'm sure you can think of a better way to get it from where you want it from. Perhaps you could just reload the page on submit with the variable in it (?quotapounds=)and a check in the beginning of the script if it's there or not.

  • #10
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It occurred to me last night. That I still have a problem with $balance, even though I had pulled it from the database. What I have is a row of data for that particular fisherman and until the current form is submitted I will not know which species he wishes to transfer.

    I suppose that I could use the var varname = document.formId.inputId.value approach for species as well, but I am beginning think that I have been delusional.

    I may be simplest to have a form as a currently do, have the fisherman click a submit, and then return a "preview" page which has obtained everthing from the database and passed the first form variables on to be used. This page could then show current balance and the amount remaining (if any) after the transaction. If he approves then he hits submit.

    Thanks for your patience and all your help.

  • #11
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That would be simplest, I think. I would do it that way first and then come back later to refine it with Javascript/AJAX, if live interaction is still desired.
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •