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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Passing value problem...

    Hello everyone, I'm trying to pass a variable from a select list from one form to two other forms. I'm using this piece of code:

    Code:
    <script language="JavaScript"><!--
    function pass_val() {
        var selectedItem      = document.invoice.invoice_num.selectedIndex;
        var selectedItemValue = document.invoice.invoice_num.options[selectedItem].value;
        
        if (selectedItem != 0) {
            document.email.invoice_num.value = selectedItemValue;
            document.download.invoice_num.value = selectedItemValue;
        }
        else {
            document.email.invoice_num.value = "";
            document.download.invoice_num.value = "";
        }
    }
    //--></script>
    For the most part, it works great! But I can't seem to download the first invoice in the list... no matter who logs in. I checked the source variable in the source code... and it's not null just like all the others, I can't figure this out... please help!!

    - Adam
    Last edited by knox203; 08-21-2008 at 01:33 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Please show us the involved markup (HTML).
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Here's the code to populate the invoice select list:

    PHP Code:
    <?
    $invoice_select 
    =  "SELECT invoice_num, invoice, invoice_name, date_format(invoice_date, '%b %e, %Y') as date
                        FROM $databasename.invoice
                        WHERE driver = '"
    .$_SESSION['username']."'
                        ORDER BY invoice_date"
    ;
    $invoice_query mysql_query($invoice_select);

    $invoice_array = array();

    while (
    $invoice_fetch mysql_fetch_assoc($invoice_query)) {
           
    $invoice_array[] = array(Invoice_Num => $invoice_fetch['invoice_num'], 
                                    
    Invoice_Date => $invoice_fetch['date']);
    }
    $count count($invoice_array);
    ?>
    The Invoice Select List:
    PHP Code:
    <form name="invoice" method="get">
    <select name="invoice_num" size="6" id="invoice_num" style="width:200px" onchange="pass_val()" />
    <? 
    for ($row 0$row $count$row++) {
      echo 
    "<option value='".$invoice_array[$row]["Invoice_Num"]."'>".$invoice_array[$row]["Invoice_Num"]." - ".$invoice_array[$row]["Invoice_Date"]."</option>";
    }
    ?>
    </select>
    </form>
    The E-Mail & Download Forms:
    Code:
    <form name="email" method="post" action="history.php?stage=2">
    <input type="text" name="email" id="email" />
    <input name="invoice_num" type="HIDDEN" id="invoice_num" />
    <input type="submit" name="submit2" id="submit2" value="Send" />
    </form>
    
    <form name="download" method="get" action="download.php">
    <input name="invoice_num" type="HIDDEN" id="invoice_num" />
    <input type="submit" name="submit" id="submit" value="Download" />
    </form>
    * I should also note that what I meant by saying "I can't seem to download the first invoice in the list" is it downloads "download.php" instead of the pdf file. The behavior is like it's not getting the invoice number... even though it's in the "value=" portion of the source code just like all the others.

    Here's the code for download.php:
    PHP Code:
    <?
    require_once("conduit/ic_commission.php");
    $invoice_num $_GET['invoice_num'];
    $query "SELECT invoice_num, invoice, invoice_name FROM $databasename.invoice where invoice_num=$invoice_num";
    $result mysql_query($query);
    $data mysql_result($result,0,"invoice");
    $name mysql_result($result,0,"invoice_name");
    header("Cache-Control: must-revalidate");
    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=$name");
    header("Content-Length: ".filesize($data));
    header("Pragma: no-cache");
    header("Content-transfer-encoding: binary");
    echo 
    $data;
    ?>
    Last edited by knox203; 08-21-2008 at 08:12 AM.

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    I'm sorry. This goes out of my wits.
    Edit:
    I don't understand the DB part
    Last edited by rangana; 08-21-2008 at 08:17 AM.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You can't download the first invoice because that's what you're telling it to do with the logic 'if (selectedItem != 0)'. As with most arrays in computing, 0 is the index of the first item.

    I don't really think that check is doing anything for you, but if you have to have it, it has at least some chance of being meaningful if you check against null instead of 0. If you're going to validate the selection, though, it's senseless to do the value lookup before the validation. So I'd rewrite your code like this:
    Code:
    <script type="text/javascript">
    function pass_val() {
        var selectedItem = document.invoice.invoice_num.selectedIndex;
        if(selectedItem === null) {
            document.email.invoice_num.value = '';
            document.download.invoice_num.value = '';
        } else {
            var selectedItemValue = document.invoice.invoice_num.options[selectedItem].value;
            document.email.invoice_num.value = selectedItemValue;
            document.download.invoice_num.value = selectedItemValue;
        }
    }
    </script>
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes

  • Users who have thanked chaosprime for this post:

    knox203 (08-21-2008)

  • #6
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks Chaos, that did the trick!

    I knew it would be something small I was overlooking


  •  

    Posting Permissions

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