Hi

This is a piece of code in javascript, embedded in html, for the
order page on an ecommerce site that i have made. I have this
particular script nested inside a <form> tag and have set the
action to a php file, but the tricky part is to extract the data
from the javascript code and to post these variables into a php
files, because their values...So i put in this data in textboxes
with names that change according to the counter and have
made the type hidden. The code is below:

//JAVASCRIPT
if (parent.item_num > 0){
for (i =1;i < parent.item_num;i++){
if (parent.itemlist.quan > 0){
index = index + 1;
document.write('<input size=35 type=hidden name=c+i
value= ' +  parent.itemlist.code + '>'+'<input size=3
type=hidden name=p+i value=' +  parent.itemlist.price +
'>'+'<input size=18 type=hidden  name=d+i  value= '+
parent.itemlist.desc + '>'+'<input size=3 type=hidden
name=q+i  value= '+  parent.itemlist.quan + '><br/>');
}
}
}



The piece of code in php  meant to handle the script is
<?php


$prod_name[]= "";
$prod_price[]= "";
$prod_painter[]= "";
$prod_quantity[]="";


//assuming that only 10 products are ordered...
for($i=1;$i<=10;$i++)
{
$prod_name[$i] = $_POST;
$prod_price[$i]=  $_POST;
$prod_painter[$i] =  $_POST;
$prod_quantity[$i]= $_POST;
}



for($i=0;$i<=10;$i++)
{
echo $prod_name[$i];
echo $prod_price[$i];
echo $prod_painter[$i];
echo  $prod_quantity[$i];
}


?>

The problem is with the php codes, the javascript works fine,
as for now I just want to get the data from js, I'll write it into a
file later...

Thankyou so much..

I would suggest that you check the output generated by your javascript. You will then see that the names of the hidden inputs are not being generated as you expect them. As an example, instead of name=c1, you will see name=c+i.

Revise your document.write to:

document.write('<input size=35 type=hidden name=c'+i+
'value= ' +  parent.itemlist[i].code + '>'+'<input size=3  
    type=hidden name=p'+i+' value=' +  parent.itemlist[i].price + 
    '>'+'<input size=18 type=hidden  name=d'+i+'  value= '+  
    parent.itemlist[i].desc + '>'+'<input size=3 type=hidden 
    name=q'+i+'  value= '+  parent.itemlist[i].quan + '><br/>');

hi
never thanked you for the help...the code worked smoothly..
thanks..

Dear nisrin,
thank u so much for ur help, it helped me a lot,
Sunilpro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.