1

Good evening. I have creating a PHP page will go through a directory and insert pictures into a table. With these pictures will be a select dropdown box and a quantity text input. Each form element is named based on the file's name when it is created. I cannot seem to pass the quantity value to javascript. HELP (please). My code is posted below.

for ($i=0; $i<count($files); $i++) {
    echo $_POST[bandSelect];
    if ($i%3 ==0 && $i!=0) {
        echo '</tr><tr>';
    }
    $pricewallets = "$7.50"; $price3x5 = "$2.00";
    $price4x6 = "$3.00";     $price5x7 = "$4.00";
    $price8x10 = "$9.00";    $price11x14 = "$14.75";
    $price12x18 = "$20.00";  $price16x20 = "$27.00";  
    $price18x24 = "$28.20";

    $num = $files[$i];
    list($width, $height, $type, $attr) = getimagesize($files[$i]);
    echo '<td style="text-align:center;"><a href="'.$num.'" target="_blank">
        <img src="'.$num.'" width ="'.($width/4).'" alt="random image"></a>';

    $fileName = basename($num);
    $qty = $num."qty";
    echo "<br />".$fileName;
    echo '<br /><select name="'.$num.'" id="'.$num.'">
        <option value="'.$num.'Wallets">Wallets (8)</option>
        <option value="'.$num.'3x5">3" x 5"</option>
        <option value="'.$num.'4x6">4" x 6"</option>
        <option value="'.$num.'5x7">5" x 7"</option>
        <option value="'.$num.'11x14">11" x 14"</option>
        <option value="'.$num.'12x18">12" x 18"</option>
        <option value="'.$num.'16x20">16" x 20"</option>
        <option value="'.$num.'18x24">18" x 24"</option>
        </select>';
    echo '<input type="text" name="'.$qty.'" id="'.$qty.'" size="1" value="0" />
        <br />
        <input type="button" name="addCart" value="Add to cart" onclick="addcart( \''.$num.'\',\''.$fileName.'\',\''.$qty.'\')" />
    </td>';
}

Here is my Javascript

function addcart(name, filename, qty)
{
var e = document.getElementById(name);
var strUser = e.options[e.selectedIndex].text;
var price;
var salePrice;
switch (strUser)
{
case "Wallets (8)":
    salePrice = 7.5;
    price = "$7.50";
    break;

case "3\" x 5\"":
    salePrice = 2;
    price = "$2.00";
    break;

case "4\" x 6\"":
    salePrice = 3;
    price = "$3.00";
    break;

case "5\" x 7\"":
    salePrice = 4;
    price = "$4.00";
    break;

case "8\" x 10\"":
    salePrice = 9;
    price = "$9.00";
    break;

case "3\" x 5\"":
    salePrice = 2;
    price = "$2.00";
    break;

case "11\" x 14\"":
    salePrice = 14.75;
    price = "$14.75";
    break;

case "12\" x 18\"":
    salePrice = 20;
    price = "$20.00";
    break;

case "16\" x 20\"":
    salePrice = 27;
    price = "$27.00";
    break;

case "18\" x 24\"":
    salePrice = 28.5;
    price = "$28.50";
    break;
}
var cartTotal=document.picSelect.cart.value;
if(strUser!=="Wallets (8)")
{
document.picSelect.cart.value = cartTotal + " " + filename + " " + strUser + "\t " + price + " x " + qty + "\n";    
}
else
{
document.picSelect.cart.value = cartTotal + " " + filename + " " + strUser + " " + price + " x " + qty + "\n";
}
document.picSelect.Total.value = parseFloat(document.picSelect.Total.value) + salePrice * qty;
textTotal.innerHTML = "Your current  total is:\n\t$" +     parseFloat(document.picSelect.Total.value).toFixed(2);



 }
5
  • what's your html output? what's your javascript? Commented Oct 20, 2012 at 0:42
  • Please see how I've formatted it and proceed similarly in the future. Commented Oct 20, 2012 at 0:43
  • Sorry, I forgot to post my Javascipt. Commented Oct 20, 2012 at 0:44
  • if href="'.$num.'" has the value i dont see the reason why $qty dont have the value... if href="'.$num.'" dont have the value that means the $files[$i] is not properly set.. Commented Oct 20, 2012 at 0:44
  • I found the answer on this page [enter link description here][1] [1]: stackoverflow.com/questions/1567278/… I passed the id (which is dynamic) of the text field to javascript. I then used that variable in an object. Thanks to all who contributed. :) Commented Oct 20, 2012 at 3:12

1 Answer 1

1

You forgot to change the value of the text field:

echo '<input type="text" name="'.$qty.'" id="'.$qty.'" size="1" value="'.$qty.'" />

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.