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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Transferring variables to and from js and php

    Hi,

    I've got an orderform which uses javascript to run client-side calculations, and php to process the form. It's a three-stage system which has display, confirm / modify and process stages. Presently at http://www.eleceng.adelaide.edu.au/t...erform_sp5.php

    So the user enters what they want and the javascript calculates the total. User clicks Review, reviews the info and then decides they want to enter something else. When they get taken back to the first page, the fields on the order table are all populated, BUT... if a new row is entered, the grand total resets to zero.

    Inspecting the JS, I can see why; when the js is executed, ordobj is created with zeros for the unit cost, total and quantity. I.e.:
    Code:
    ordData = new MakeArray(RowsInForm)
    for (var i=1; i<= RowsInForm; i++) {
       ordData[i] = new ordobj(0,0,0)
    }
    
    function ordobj(Qty, Unit_Cost, Line_Total) {
       this.Qty = Qty
       this.Unit_Cost = Unit_Cost
       this.Line_Total = Line_Total
    }
    So what I need to do is populate ordobj with the variables entered by the user rather than a string of zeros, i.e. the values stored in $_POST so the grand total starts off at the correct value.

    The fields in the order table are named
    Code:
    Qty1, Unit_Cost1, Line_Total1, Qty2, Unit_Cost2, Line_Total2
    etc for as many rows as are entered. So I can't just have the variable name hard-coded like:
    Code:
    for (var i=1; i<= RowsInForm; i++) {
    var newqty = '<?=$_POST['Qty1']?>';
       ordData[i] = new ordobj(newqty,....)
    }
    because the actual PHP field name needs to be based on the value of i in the loop. E.g.
    Code:
    var newqty = '<?=$_POST['Qty'+i+'']?>';
    which doesn't work because that literal value doesn't exist.

    I then thought of getting the value of i, writing it to a php variable, and then using that within the above statement, e.g.
    Code:
    <?=$currentrow?> = 'Qty'+i
    var newqty = '<?=$_POST[$currentrow]?>';
    No luck.
    Code:
    var qty = 'Qty'+i
    var jqty = '<?=$_POST[qty]?>';
    No luck either.


    I think I'm at the limits of my js and php knowledge, and I don't have much hair left that can be torn out! I'm sure it's just a syntax thing; can anyone help with this?

    Thanks in anticipation!

    Greg
    Last edited by gregp; 12-05-2006 at 03:47 AM.

  • #2
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply.

    Tables are not used for layout. CSS is used as you've suggested it should be. Tables are only used for the order table itself which, being tabular in nature, is an appropriate use of table structure.

    Thanks for pointing out the two closing form tags, will attend to.

    However I don't think the above points affect the fundamental operation of the js/php.

    One major problem I face is that I'm no js expert, at all. The js is from an orderform I got from someone else and modified to suit. It probably isn't the best way of doing it but I don't know enough js to write a clean one from scratch. That's why a lot of it is done with php.

    "Wouldn't it be easier to start with just one row and then have the user click a button each time they want to add a new row, along with a button to delete the last row?"

    Yep no doubt. But that would require js to generate the fields, not php, and php is running that at present. They were originally created with js but I changed this in order to get the fields re-populated with the values stored in $_POST when the user clicked 'Modify'.

    So you're suggesting use a GET method for the first page of the form and only go to POST when it actually comes to submitting the form?

    "Wouldn't it be easier, server-side, to use array type form field names? :
    Part_No[] Description[] Unit_Cost[]
    You can easily loop through them with PHP, without knowin how many there are, just by using foreach:"

    Yep I can do that to create the fields. The issue is grabbing the data in those php fields and storing them in ordobj. That's one reason I need to transfer php variables to js

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I hate to state the obvious when you have such a complex question but your JS code in the first post is missing all sorts of semi-colons.

    Code:
    ordData = new MakeArray(RowsInForm);
    for (var i=1; i<= RowsInForm; i++) {
       ordData[i] = new ordobj(0,0,0);
    }
    
    function ordobj(Qty, Unit_Cost, Line_Total) {
       this.Qty = Qty;
       this.Unit_Cost = Unit_Cost;
       this.Line_Total = Line_Total;
    }
    Just in case that was an issue.

    david_kw

  • #4
    New to the CF scene
    Join Date
    Dec 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks David; I habitually add semi-colons to my code from my C background but numerous operational examples of js I've seen omit them so I figured maybe they weren't required and consciously left them out. I'll stick them back in and satisfy the pedant in me!

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the only time a semi-colon isn't necessary is when it is the last command in a code block. Like:

    Code:
    if (x < 5) {
      alert("less than 5");
      x += 12;
      showX(x)
    }
    I put the semi-colon in anyway since if you add more code you don't have to worry about adding it later. Similar to the way CSS works. I'm not aware of a time where adding the semi-colon causes a problem.

    david_kw

  • #6
    New to the CF scene
    Join Date
    Oct 2006
    Location
    India
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If You Need To Simply Pass The Variables You Can Use Hidden Fields In The Cliant Side And Store The Value In That Fields After That You Can Use php To Track The Values Of The Hidden Fields

    OR

    You Can Use AJAX I Think It Would Be The Best Method

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe you already fixed your problem, but looking at your site it seems to be working as you said you wanted it to unless I misread something.

    david_kw


  •  

    Posting Permissions

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