Jump to content

PHP submit form loop


naykidd

Recommended Posts

Im wondering if anyone could offer a solution, i'm having a nightmare with a submit form.

 

Im trying to make an invoice system, using a 3rd party system that im modifying (Tried to look on their site but they have no help system).

 

I need to add items to the order, AND post them to an sql table whilst automatically working out the VAT + Total cost

I then need to be able to add more items, and them also submit to the sql table in another row (using Submit2, its kind of a form within a form)

 

Finally, when i go to submit the final page, the page submits the customers name/invoice/address details etc to another table via $_POST, and then brings it all up on the invoice page (I use WHERE invoiceno="" to bring back all the seperate parts of the order from the database)

 

So far, I can get the different items to come up on the invoice, but they wont store via a $_POST method (I get an error)

and if i use anything but a loop, like just a variable, the item does store in the table when i submit it, but it wont remember the item for the invoice page, and the prices all become the same when i add multiple items!

 

I'd love to get some feedback, i've been php coding for a while now, and this is one i'm really struggling with!

Theres a config file this code links too, but its not needed to see whats going on so ive left it out!

I know the update function won't work at the moment either, but i'm working on that.

 

Thanks,

<?php
/************************************************************************/
/* PHP InvoiceIt v1.0                                                   */
/* ===========================                                          */
/*                                                                      */
/*   Written by Steve Dawson - http://www.stevedawson.com               */
/*   Freelance Web Developer - PHP, MySQL, HTML programming             */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* but please leave this header intact, thanks                          */
/************************************************************************/
include "config.php";
require_once("Connections/connection.php");
error_reporting (E_ALL ^ E_NOTICE);

error_reporting (E_ALL ^ E_NOTICE);



## The processing function thingy ma jig.
function processform() {
global $HeaderDistance;
global $BorderColour;
global $TableBGcolour;
global $PageTitle;
global $CurrencyUnit;
global $InvoiceSignature;
global $DisplayVat;
global $TaxName;
global $VatRate;
global $vat_at;
global $customer_id;
global $customer_address;
global $customer_name;
global $date;
global $order;
global $price;
global $notes;
?>
<HTML>
<HEAD>
<TITLE>PHP InvoiceIt - Invoice Created</TITLE>
<LINK REL="STYLESHEET" HREF="invoice.css">
</HEAD>
<BODY>
<img src="blank.gif" width="1" height="<?php print $HeaderDistance; ?>">
<DIV align="center">
<?php print "$part"; ?> 
  <TABLE border="0" width="600">
    <tr> 
      <TD valign="top" colspan="2"> <table>
          <tr> 
            <td align="left"> To:<br> <?php print $customer_name; ?><br> <?php print str_replace("\n", "<br>", $customer_address); ?> 
            </td>
          </tr>
          <tr> 
            <td align="right"> <b>Date:</b>      <?php print "$date"; ?> 
            </td>
          </tr>
	            <tr> 
            <td align="right"> <b>Ref:</b>      <?php print "$customer_id"; ?> 
            </td>
          </tr>
          <tr> 
            <td> <br><br><table width="100%" CELLPADDING="2" CELLSPACING="0" border="1" bordercolor="<?PHP print "$BorderColour"; ?>">
                <tr bgcolor="<?PHP print "$TableBGcolour"; ?>"> 
                  <th>Product</th>
                  <th>Cost</th>
                  <th><?PHP print "$TaxName"; ?></th>
                  <th>Total</th>
                </tr>
                <tr> 
                  <td><img src="blank.gif" width="450" height="14"></td>
                  <td><img src="blank.gif" width="60" height="14"></td>
                  <td><img src="blank.gif" width="60" height="14"></td>
                  <td><img src="blank.gif" width="60" height="14"></td>
                </tr>
<?php
$tot_cost = "0";
$tot_vat = "0";
$tot_price = "0";
for($l=0; $l<sizeof($order); $l++){
if(!strlen($order[$l]) && $l == (sizeof($order)-1)) { continue; }
?>
                <tr> 
                  <td><?php print $order[$l]; ?></td>
                  <td align="right"><?php print "$CurrencyUnit"; ?><?php print number_format($price[$l]/($vat_at+100)*100, 2); ?></td>
                  <td align="right"><?php print "$CurrencyUnit"; ?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100), 2); ?></td>
                  <td align="right" width="50"><?php print "$CurrencyUnit"; ?><?php print number_format($price[$l],2); ?></td>
                </tr>
<?php
$tot_cost += $price[$l]/($vat_at+100)*100;
$tot_vat += $price[$l]-($price[$l]/($vat_at+100)*100);
$tot_price += $price[$l];
}
?>

                <tr> 
                  <td colspan="4"><?php if($DisplayVat == "Y") { print "<i>$TaxName charged at a rate of $vat_at%</i>"; } else print "<img src=\"blank.gif\" width=\"1\" height=\"14\">";?></td>
                </tr>
              </table>
              <br> <table align="right" width="0" CELLPADDING="2" CELLSPACING="1" border="1" bordercolor="<?PHP print "$BorderColour"; ?>">
                <tr> 
                  <td width="60" align="right"><b><?php print "$CurrencyUnit"; ?><?php print number_format($tot_cost,2); ?></b></td>
                  <td align="right" width="60"><b><?php print "$CurrencyUnit"; ?><?php print number_format($tot_vat,2); ?></b></td>
                  <td align="right" width="60"><b><?php print "$CurrencyUnit"; ?><?php print number_format($tot_price,2); ?></b></td>
                </tr>
              </table> <br> <br><br><br><?php print tag($notes); ?>
              <br> <br> <?php print "$InvoiceSignature"; ?> </td>
          </tr></table></TD></TR></TABLE>
</DIV>
</BODY>
</HTML>
<?php
}
function mainform() {

///////////////////////////////////////////////// variables 
$customer_id = $_POST['invoice'];
$customer_id = $_POST['invoiceno'];
$part = $_POST['part'];
$tot_cost = $_POST['totcost'];
$tot_vat = $_POST['totvat'];
$tot_price = $_POST['totprice'];
$description = $_POST['description'];


$Submit = $_POST['Submit'];
$Submit2 = $_POST['Submit2'];

//////////////////////////////////////
$query = sprintf("SELECT * FROM invo where ID='$upd'"); 
$result = @mysql_query($query); //tells the database
$rowUpdate = mysql_fetch_array($result); //fetches the result
//////////////////////////////////////
/////////////////////////////// The following checks if there is stuff in every field and not in the hidden update field


global $HeaderDistance;
global $BorderColour;
global $TableBGcolour;
global $PageTitle;
global $CurrencyUnit;
global $DisplayVat;
global $TaxName;
global $VatRate;
global $InvoiceSignature;
global $n_order;
global $n_price;
global $vat_at;
global $customer_id;
global $customer_address;
global $customer_name;
global $date;
global $order;
global $price;
global $notes;

?>
<HTML>
<HEAD>
<TITLE>PHP InvoiceIt - Create the Invoice</TITLE>
<LINK REL="STYLESHEET" HREF="invoice.css">
</HEAD>
<BODY><img src="blank.gif" width="1" height="<?php print $HeaderDistance; ?>">
<DIV align="center"> 
  <TABLE WIDTH="90%" BORDER="0" CELLPADDING="2" CELLSPACING="2" ALIGN="CENTER">
    <TR> 
      <TD valign="top" colspan="2"><H4 align="center"><?PHP print $PageTitle; ?></H4> 
        <br> <form id="form1" name="form1" method="post">
          <table>
            <tr> 
              <td align="left"> To:<br>
		  <input type="text" name="customer_name" size="38" value="<?php if($customer_name) { print tag($customer_name); } else { print "Insert Customer Name"; } ?>"> 
        <br> <textarea name="customer_address" rows="5" cols="40"><?php if($customer_address) { print $customer_address; } else { print "Insert Customer Address"; } ?></textarea> 
              </td>
            </tr>
            <tr> 
              <td align="right"> <b>Date:</b>      
		  <input type="text" name="date" size="15" value="<?php if($date){ print tag($date); } else { print date("d/m/Y"); } ?>"> 
              </td>
            </tr>
		            <tr> 
              <td align="right"> <b>Ref Number:</b>   
		 <input type="text" name="customer_id"  size="15" value="<?php if($customer_id){ echo $rowUpdate['invoice']; print tag($customer_id); } else { print "Customer Ref"; } ?> "> 
              </td>
            </tr>
            <tr> 
              <td> <table width="100%" CELLPADDING="2" CELLSPACING="0" border="1" bordercolor="<?PHP print "$BorderColour"; ?>">
                  <tr bgcolor="<?PHP print "$TableBGcolour"; ?>"> 
                    <th>Product</th>
                    <th>Description</th>
                    <th>Cost</th>
                    <th><?PHP print "$TaxName"; ?></th>
                    <th>Total</th>
                  </tr>
                  <tr> 
                    <td><img src="blank.gif" width="400" height="14"></td>
                    <td><img src="blank.gif" width="110" height="14"></td>
                    <td><img src="blank.gif" width="60" height="14"></td>
                    <td><img src="blank.gif" width="60" height="14"></td>
                    <td><img src="blank.gif" width="60" height="14"></td>
                  </tr>
<?php
$tot_cost = 0;
$tot_vat = 0;
$tot_price = 0;
for($l=0; $l<sizeof($order); $l++){
?>
                  <input type="hidden" name="order[<?php print $l; ?>]" value="<?php print tag($order[$l]); ?>">
                  <input type="hidden" name="price[<?php print $l; ?>]" value="<?php print tag($price[$l]); ?>">
                  <tr> 
                    <td><?php print $part; ?></td>
                    <td><?php print $description; ?></td>
                    
                    <td align="right"><?php print "$CurrencyUnit"; ?><?php print number_format($price[$l]/($vat_at+100)*100, 2); ?></td>
                    <td align="right"><?php print "$CurrencyUnit"; ?><?php print number_format($price[$l]-($price[$l]/($vat_at+100)*100),2); ?></td>
                    <td align="right"><?php print "$CurrencyUnit"; ?><?php print number_format($price[$l],2); ?></td>
                    
                  </tr> 
                  <?php
$tot_cost += $price[$l]/($vat_at+100)*100;
$tot_vat += $price[$l]-($price[$l]/($vat_at+100)*100);
$tot_price += $price[$l];
$order2 = $order[$l];
$order2 = $_POST['order'];
}
?>
<form id="form2" name="form2" method="post">

<b>Part:</b>   
		 <input type="text" name="part"  size="15" value="<?php echo $rowUpdate['part']; ?> "> 
              <input type="text" name="description"  size="15" value="<?php echo $rowUpdate['description']; ?> ">
		  <input name="Submit2" type="Submit" value="Submit2" class="button"></p>
             <?php  if($Submit2){ 

/////////////////////////////////////////////////////// this inserts it
$query 		= sprintf("INSERT INTO invo2 (part, invoiceno, totcost, totvat, description, totprice, order) values ('$part', '$customer_id', '$tot_cost', '$tot_vat', '$description', '$tot_price', '$order2')");
		  mysql_query($query)or die (mysql_error());

} {}
?>

</FORM>


                  <tr> 
                    <td>
                      <input type="text" name="order[<?php print sizeof($order); ?>]" size="70"></td>
                    <td align="right"><img src="blank.gif" width="60" height="14"></td>
                    <td align="right"><img src="blank.gif" width="60" height="14"></td>
                    <td align="right"><?php print "$CurrencyUnit"; ?> <input type="text" name="price[<?php print sizeof($price); ?>]" size="5"></td>
                  </tr> <tr> 
                    <td colspan=4><?php if($DisplayVat == "Y") {
				print "<input type=\"hidden\" name=\"vat_at\" value=\"$VatRate\" size=\"4\">
				<i>$TaxName charged at a rate of $VatRate%</i>"; } else print "<img src=\"blank.gif\" width=\"1\" height=\"14\">"; ?></td>
                  </tr>
                </table> <table align="right" width="0"  CELLPADDING="2" CELLSPACING="1" border="1" bordercolor="<?PHP print "$BorderColour"; ?>">
                  <br>
                  <tr> 
                    <td width="60" align="right"><b><?php print "$CurrencyUnit"; ?><?php print number_format($tot_cost,2); ?></b></td>
                    <td align="right" width="60"><b><?php print "$CurrencyUnit"; ?><?php print number_format($tot_vat,2); ?></b></td>
                    <td align="right" width="60"><b><?php print "$CurrencyUnit"; ?><?php print number_format($tot_price,2); ?></b></td>
                  </tr>
                </table><br>
<br>
<br>Enter any additional notes:<br>
<textarea name="notes" cols="100%" rows="5"><?php print "$notes"; ?></textarea> <br> <br><br>
                <?php print "$InvoiceSignature"; ?><br></td>
            </tr>
            <tr> 
              <td><br> <br> <p align="center"><input name="action" type="submit" value="Update Invoice" class="button">   
		  <input name="Submit" type="Submit" value="Submit" class="button"></p></td>
            </tr></table>
        </FORM> </TD>
    </TR>
  </TABLE>
</DIV>
</BODY>
</HTML>
<?php
}
if($Submit == "Submit"){ processform();

/////////////////////////////////////////////////////// this inserts it
$query 		= sprintf("INSERT INTO invo (invoice) values ('$customer_id')");
		  mysql_query($query)or die (mysql_error());

} else {mainform();}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/208908-php-submit-form-loop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.