View Full Version : Transferring variables to and from js and php

12-05-2006, 02:56 AM

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/training/php/orderform_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.:

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
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:

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.
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.

<?=$currentrow?> = 'Qty'+i
var newqty = '<?=$_POST[$currentrow]?>';

No luck.

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!


12-06-2006, 12:31 AM
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

12-06-2006, 12:42 AM
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.

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.


12-06-2006, 02:00 AM
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! :)

12-06-2006, 07:33 AM
I think the only time a semi-colon isn't necessary is when it is the last command in a code block. Like:

if (x < 5) {
alert("less than 5");
x += 12;

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.


12-06-2006, 07:38 AM
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


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

12-06-2006, 07:49 AM
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.