bhogg Posted July 30, 2006 Share Posted July 30, 2006 Hi all,My apologies if this question has already been asked before. I've tried searching the forums and googling but no success, as the code given on multiple sites does not work (on my server, anyways).Basically I have a form with multiple textboxes with the same name, and I'm using the square brackets as suggested by php.net:[code]<input type="text" name="txtQty[]">[/code]There's a few of these lines in the HTML. Now in the PHP page that the form is submitted to, I do a die(print_r($_POST)) ; and get:[code]Array( [cboCompanyName] => [txtInvoiceDate] => 2006-07-30 [txtInvoiceId] => 1 [txtPONumber] => [cboTerms] => [cboItem] => Array [txtQty] => Array [txtDescription] => Array [txtRate] => Array [cboTaxCode] => Array [txtInvoiceMemo] => )1[/code]Looks like everything is great, so I do:[code] $txtQty = $_POST['txtQty'] ; die($txtQty[0]) ;[/code]And what is printed is not the value of the first textbox, but the letter 'A' (the first character in the string "Array"). Why is PHP not returning an array of values as expected but rather the string "Array"?Thanks for any help.Brian Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 you see array because that is what you have, an array.if you want to see the elements of the array you could do something like thisforeach($_POST['txtQty'] as $qty) { echo $qty.'<BR>';}now instead of seeing "array" you will see each of the elements in that arrayhope this helps Quote Link to comment Share on other sites More sharing options...
bhogg Posted July 30, 2006 Author Share Posted July 30, 2006 I tried using array_keys before thinking that was the issue. When I tried your code or my array_keys($_POST['txtQty']) code, I get the following error:[code]Warning: Invalid argument supplied for foreach() in processInvoice.php on line 43[/code]Which looks like that element really is just the string "Array", which doesn't really make any sense... Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 hhmmm... could you post your html form Quote Link to comment Share on other sites More sharing options...
bhogg Posted July 30, 2006 Author Share Posted July 30, 2006 Sure, it's fairly long though (the lines of the invoice form are repeated a few times). I was just thinking it had something to do with the checkInvoiceForm() javascript (which currently does nothing but call window.document.frmInvoice.submit()) but I tried bypassing that with no change.[code]<html><head><title>Shop Admin Control Panel - Add Invoice</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link href="/bhc/admin/include/admin.css" rel="stylesheet" type="text/css"><script language="JavaScript" type="text/javascript" src="/bhc/library/common.js"></script><script language="JavaScript" type="text/javascript" src="/bhc/admin/library/invoice.js"></script></head><body><table width="750" border="0" align="center" cellpadding="0" cellspacing="1" class="graybox"> <tr> <td colspan="2"><img src="/bhc/admin/include/banner-top.gif" width="750" height="75"></td> </tr> <tr> <td width="150" valign="top" class="navArea"> <p> </p> <a href="/bhc/admin/index.php" class="leftnav">Home</a> <a href="/bhc/admin/index.php?view=invoice" class="leftnav">Invoices</a> <a href="/bhc/admin/index.php?view=customer" class="leftnav">Customers</a> <a href="/bhc/admin/index.php?view=company" class="leftnav">Companies</a> <a href="/bhc/admin/index.php?view=category" class="leftnav">Shop Categories</a> <a href="/bhc/admin/index.php?view=product" class="leftnav">Shop Products</a> <a href="/bhc/admin/index.php?view=order" class="leftnav">Shop Orders</a> <a href="/bhc/admin/index.php?view=shipping" class="leftnav">Shop Shipping</a> <a href="/bhc/admin/index.php?view=config" class="leftnav">Shop Config</a> <a href="/bhc/admin/index.php?view=user" class="leftnav">Admin Users</a> <a href="/bhc/admin/index.php?logout" class="leftnav">Logout</a> <p> </p> <p> </p> <p> </p> <p> </p></td> <td width="600" valign="top" class="contentArea"> <table width="100%" border="0" cellspacing="0" cellpadding="20"> <tr> <td> <input type="submit" name="previous" value="Previous" class="box"> <input type="submit" name="next" value="Next" class="box"><form action="processInvoice.php?action=add" method="post" enctype="multipart/form-data" name="frmInvoice" id="frmInvoice"> <p align="center" class="formTitle">Invoice</p> <p><table border="0" align="left" cellpadding="5" cellspacing="1" class="entryTable"> <tr> <td width="150" class="label">Company Name</td> </tr> <tr> <td class="content"> <select name="cboCompanyName" class="box" onChange="changeBillTo(this.selectedIndex);"> <option value=""></option><option value="1100 Walkers Line">1100 Walkers Line</option><option value="Bob Loree">Bob Loree</option> </select></td> </tr> </table> <table border="0" align="right" cellpadding="5" cellspacing="1" class="entryTable"> <tr> <td class="label" width=75 align=right>Date</td><td width=75 class="content"><input class="box" type="text" name="txtInvoiceDate" value="2006-07-30" size=11></td> <td class="label" width=75 align=right>Invoice #</td><td width=75 class="content"><input class="box" type="text" name="txtInvoiceId" value="1" size=10></td> </tr> </table> <br clear="all"> </p><p> <table border="0" cellpadding="5" cellspacing="1" class="entryTable"> <tr> <td class="label" width=150>Bill To</td> </tr> <tr> <td class="content"><div id="billToField"><br><br><br><br><br><br></div></td> </tr> </table></p> <table border="0" align="right" cellpadding="5" cellspacing="1" class="entryTable"> <tr> <td class="label" width=75 align=right>PO Number</td><td width=75 class="content"><input type="text" name="txtPONumber" width=6 class="box"></td> <td class="label" width=75 align=right>Terms</td><td width=75 class="content"> <select name="cboTerms" class="box"> <option value=""></option> <option value="Net 15">Net 15</option> <option value="Net 30">Net 30</option> <option value="Due on Receipt">Due on Receipt</option> </select> </td> </tr> </table><br clear="right"> <table border="0" align="center" cellpadding="2" cellspacing="1"> <tr align="center" id="listTableHeader"> <td width=100>Item</td> <td width=50>Qty</td> <td width=300>Description</td> <td width=50>Rate</td> <td width=50>Amount</td> <td width=50>Tax</td> <td width=25> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(0, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(1, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(2, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(3, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(4, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(5, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(6, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(7, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td><select name="cboItem[]" class="box" onChange="itemClick(8, this.selectedIndex);"><option value=""></option><option value="In-Person Work">In-Person Work</option><option value="Re-Imb">Re-Imb</option><option value="Remote Work">Remote Work</option></select></td> <td><input type="text" name="txtQty[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtDescription[]" size=30 class="box"></td> <td><input type="text" name="txtRate[]" size=5 class="box" onChange="calcTotals();"></td> <td><input type="text" name="txtAmount[]" size=5 class="box" value="0.00"></td> <td><select name="cboTaxCode[]" onChange="calcTotals();" class="box"> <option value=".">.</option> <option value="G">G</option> <option value="P">P</option> <option value="S">S</option> </select></td> <td> </td> </tr> <tr> <td colspan=4 align=right class="entryTable">GST</td> <td class="text"><div id="totalGST">0.00</div></td> <td> </td> <td> </td> </tr> <tr> <td colspan=4 align=right class="entryTable">Total</td> <td class="text"><div id="totalAmount">0.00</div></td> <td> </td> <td> </td> </tr> <tr> <td align="right" class="entryTable">Memo:</td> <td class="text" colspan=6><input type="text" name="txtInvoiceMemo" size=40 class="box"></td> </tr> </table> <!-- Submit Buttons --> <p align="right"> <input name="btnSaveClose" type="button" id="btnSaveClose" value="Save & Close" onClick="checkInvoiceForm();" class="box"> <input name="btnSaveNew" type="button" id="btnSaveNew" value="Save & New" onClick="checkInvoiceForm();" class="box"> <input name="btnClear" type="reset" id="btnClear" value="Clear" onClick="changeBillTo(0); calcTotals();" class="box"> <input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php?catId=';" class="box"> </p></form><script language="javascript"> billTo = new Array() ; itemRate = new Array() ; itemDescription = new Array() ; itemTaxCode = new Array() ; billTo[0] = '<br><br><br><br><br><br>' ;billTo[1] = '1100 Walkers Line<br>Hayley Thomas<br><br> <br><br>905-331-3616' ;billTo[2] = 'Bob Loree<br>Bob Loree<br><br> <br><br>905-637-0105' ; itemRate[0] = 0 ; itemTaxCode[0] = 0 ; itemDescription[0] = '' ;itemRate[1] = 70.00 ; itemDescription[1] = '' ; itemTaxCode[1] = 1 ;itemRate[2] = 0.00 ; itemDescription[2] = '' ; itemTaxCode[2] = 0 ;itemRate[3] = 55.00 ; itemDescription[3] = '' ; itemTaxCode[3] = 1 ; // Disable the amount textboxes for (intCount = 0; intCount < 9; intCount++) { document.frmInvoice["txtAmount[]"][intCount].disabled = true ; } function changeBillTo(index) { billToField.innerHTML = billTo[index] ; } function itemClick(index, intItem) { // Auto-fill the rate, description and tax code document.frmInvoice["txtRate[]"][index].value = itemRate[intItem].toFixed(2) ; document.frmInvoice["txtDescription[]"][index].value = itemDescription[intItem] ; document.frmInvoice["cboTaxCode[]"][index][itemTaxCode[intItem]].selected = true ; } function calcTotals() { // alert("Called"); fGST = 0; fTotal = 0; // alert(document.frmInvoice.cboItem[0].selectedIndex) ; for (intCount=0; intCount < 9; intCount++) { // Don't force them to pick an item right away. if (document.frmInvoice["cboTaxCode[]"][intCount][document.frmInvoice["cboTaxCode[]"][intCount].selectedIndex].value == 'G' || document.frmInvoice["cboTaxCode[]"][intCount][document.frmInvoice["cboTaxCode[]"][intCount].selectedIndex].value == 'S') { fGST += document.frmInvoice["txtQty[]"][intCount].value * document.frmInvoice["txtRate[]"][intCount].value * 0.06 ; } document.frmInvoice["txtAmount[]"][intCount].value = document.frmInvoice["txtQty[]"][intCount].value * document.frmInvoice["txtRate[]"][intCount].value ; // Format the values so they look nice if (document.frmInvoice["txtAmount[]"][intCount].value != '') { document.frmInvoice["txtAmount[]"][intCount].value = parseFloat(document.frmInvoice["txtAmount[]"][intCount].value).toFixed(2) ; } if (document.frmInvoice["txtQty[]"][intCount].value != '') { document.frmInvoice["txtQty[]"][intCount].value = parseFloat(document.frmInvoice["txtQty[]"][intCount].value).toFixed(2) ; } if (document.frmInvoice["txtRate[]"][intCount].value != '') { document.frmInvoice["txtRate[]"][intCount].value = parseFloat(document.frmInvoice["txtRate[]"][intCount].value).toFixed(2) ; } fTotal += document.frmInvoice["txtQty[]"][intCount].value * document.frmInvoice["txtRate[]"][intCount].value ; } // Add the GST onto the total now fTotal += fGST ; // Update the page totalGST.innerHTML = fGST.toFixed(2) ; totalAmount.innerHTML = fTotal.toFixed(2) ; } </script></td> </tr> </table></td> </tr></table><p> </p></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
bhogg Posted July 30, 2006 Author Share Posted July 30, 2006 My apologies, there was a chunk of code in a common PHP file that tried to deal with magic quotes:[code]if (!get_magic_quotes_gpc()) { if (isset($_POST)) { foreach ($_POST as $key => $value) { $_POST[$key] = trim(addslashes($value)); } } if (isset($_GET)) { foreach ($_GET as $key => $value) { $_GET[$key] = trim(addslashes($value)); } } }[/code]I'll need to look for a better way of dealing with ' and slash characters in form submissions, since this one can't deal with $_POST arrays, unless someone knows of one off hand?Thanks again,Brian Quote Link to comment Share on other sites More sharing options...
bhogg Posted July 30, 2006 Author Share Posted July 30, 2006 In case anyone needs it, I modified the code slightly to deal with arrays in $_POST up to one level deep:[code]if (!get_magic_quotes_gpc()) { if (isset($_POST)) { foreach ($_POST as $key => $value) { if (is_array($_POST[$key])) { foreach ($_POST[$key] as $keysub => $valuesub) { $_POST[$key][$keysub] = trim(addslashes($valuesub)) ; } } else { $_POST[$key] = trim(addslashes($value)); } } } if (isset($_GET)) { foreach ($_GET as $key => $value) { if (is_array($_GET[$key])) { foreach ($_GET[$key] as $keysub => $valuesub) { $_GET[$key][$keysub] = trim(addslashes($valuesub)); } } else { $_GET[$key] = trim(addslashes($value)); } } } }[/code] Quote Link to comment Share on other sites More sharing options...
Drumminxx Posted July 30, 2006 Share Posted July 30, 2006 well, I just tried a little test with your html and it seems to be working here is the result I gotArray( [cboCompanyName] => [txtInvoiceDate] => 2006-07-30 [txtInvoiceId] => 1 [txtPONumber] => [cboTerms] => [cboItem] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => ) [txtQty] => Array ( [0] => 3.00 [1] => 6.00 [2] => 8.00 [3] => [4] => [5] => [6] => [7] => [8] => ) [txtDescription] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => ) [txtRate] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => ) [cboTaxCode] => Array ( [0] => . [1] => . [2] => . [3] => . [4] => . [5] => . [6] => . [7] => . [8] => . ) [txtInvoiceMemo] => [test] => test)note the values in txtQty Quote Link to comment Share on other sites More sharing options...
bhogg Posted July 30, 2006 Author Share Posted July 30, 2006 I actually got it working, since the magic quotes code that was in the template was messing up the arrays in $_POST. Thanks again for the quick replies and taking the time to test the code, much appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.