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:

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

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

//assuming that only 10 products are ordered...
$prod_name[$i] = $_POST;
$prod_price[$i]=  $_POST;
$prod_painter[$i] =  $_POST;
$prod_quantity[$i]= $_POST;

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

Recommended Answers

All 3 Replies

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/>');

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

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

Be a part of the DaniWeb community

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