...

View Full Version : Passing value problem...



knox203
08-21-2008, 12:26 AM
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:


<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

rangana
08-21-2008, 05:53 AM
Please show us the involved markup (HTML).

knox203
08-21-2008, 06:53 AM
Here's the code to populate the invoice select list:



<?
$invoice_select = "SELECT invoice_num, invoice, invoice_name, date_format(invoice_date, '&#37;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:


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


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

<?
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;
?>

rangana
08-21-2008, 07:01 AM
I'm sorry. This goes out of my wits.

I don't understand the DB part

chaosprime
08-21-2008, 07:07 AM
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:
<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>

knox203
08-21-2008, 07:19 AM
Thanks Chaos, that did the trick!

I knew it would be something small I was overlooking :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum