Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2006, 05:31 PM   #1 (permalink)
madhumati
Registered User
 
Join Date: Mar 2006
Posts: 1
madhumati is on a distinguished road
php $_POST with multiple hidden form elements


I am a webdesigner creating a add to cart for a product. I have used JavaScript to handle the add to cart which adds the products to the cart and displays the result in hidden fields in a form. Now I want to mail the contents of the cart using php along with some other information from the buyer.
JavaScript used is:

<SCRIPT LANGUAGE="JavaScript">

function FormContent() {
return GetFromCart()
}

function checkForm(thisForm, checkForCreditCard) {
bFormError = false; //Boolean variable to store form state
bIsValidCard = false; //Boolean variable to store CC state
strErrorList = ""; //String list of missing/errorsum fields

if( thisForm.FIRST.value=='' ) {bFormError = true; strErrorList += "First Name, ";}
if( thisForm.LAST.value=='' ) {bFormError = true; strErrorList += "Last Name, ";}
if( thisForm.ADDRESS.value=='') {bFormError = true; strErrorList += "Address, ";}
if( thisForm.CITY.value=='' ) {bFormError = true; strErrorList += "City, ";}
if( thisForm.STATE.value=='' ) {bFormError = true; strErrorList += "State, ";}
if( thisForm.ZIP.value=='' ) {bFormError = true; strErrorList += "Zip, ";}
if( thisForm.PHONE.value=='' ) {bFormError = true; strErrorList += "Phone, ";}
if( thisForm.ACCOUNT.value=='') {bFormError = true; strErrorList += "Credit Card Number, ";}
if( thisForm.MONTH.value=='' ) {bFormError = true; strErrorList += "Month, ";}
if( thisForm.YEAR.value=='' ) {bFormError = true; strErrorList += "Year ";}
if( bFormError == true ) {
alert("I'm sorry, but you had one or more missing or invalid entries.\n"
+"Please check the following fields: \n\n"
+strErrorList
+"\n\n");
return false;
}

//Check for valid Visa
if (((thisForm.ACCOUNT.value.length == 16) || (thisForm.ACCOUNT.value.length == 13)) &&
(thisForm.ACCOUNT.value.substring(0,1) == 4))
bIsValidCard = true;

//Check for valid MasterCard
firstdig = thisForm.ACCOUNT.value.substring(0,1);
seconddig = thisForm.ACCOUNT.value.substring(1,2);
if ((thisForm.ACCOUNT.value.length == 16) &&
(firstdig == 5) && ((seconddig >= 1) &&
(seconddig <= 5))
)
bIsValidCard = true;

if (bIsValidCard == false){
alert("I'm sorry, but you need to enter a valid credit card number.\n");
return false;
}

return needComments();
} //END function checkForm


//---------------------------------------------------------------------||
// FUNCTION: CKquantity ||
// PARAMETERS: Quantity to ||
// RETURNS: Quantity as a number, and possible alert ||
// PURPOSE: Make sure quantity is represented as a number ||
//---------------------------------------------------------------------||
function CKquantity(checkString) {

strNewQuantity = ""; // String Adjusted Item Quantity
count = 0; // Generic Loop Counter

for (i = 0; i < checkString.length; i++) {
ch = checkString.substring(i, i+1);

if ((ch >= "0" && ch <= "9") || (ch == '.')) {
strNewQuantity += ch;
}
}

if (strNewQuantity.length < 1)
strNewQuantity = "1";

return strNewQuantity;
}


//---------------------------------------------------------------------||
// FUNCTION: AddToCart ||
// PARAMETERS: Form Object ||
// RETURNS: Cookie to user's browser, with prompt ||
// PURPOSE: Adds a product to the user's shopping cart ||
//---------------------------------------------------------------------||
function AddToCart(thisForm) {

iNumberOrdered = 0; //Integer number of products already ordered

iNumberOrdered = GetCookie("NumberOrdered");
iNumberOrdered++;

if ( iNumberOrdered > 12 )
alert("I'm Sorry, your cart is full, please proceed to checkout.");
else {
dbUpdatedOrder = thisForm.QUANTITY.value + "|"
+ thisForm.PRICE.value + "|"
+ thisForm.ID_NUM.value + "|"
+ thisForm.NAME.value;

NewOrder = "Order." + iNumberOrdered;
SetCookie (NewOrder, dbUpdatedOrder, null, "/");
SetCookie ("NumberOrdered", iNumberOrdered, null, "/");

notice = thisForm.QUANTITY.value + " "
+ thisForm.NAME.value
+ " added to your shopping cart.";

alert(notice);
}
}


//---------------------------------------------------------------------||
// FUNCTION: getCookieVal ||
// PARAMETERS: offset ||
// RETURNS: URL unescaped Cookie Value ||
// PURPOSE: Get a specific value from a cookie ||
//---------------------------------------------------------------------||
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}


//---------------------------------------------------------------------||
// FUNCTION: FixCookieDate ||
// PARAMETERS: date ||
// RETURNS: date ||
// PURPOSE: Fixes cookie date, stores back in date ||
//---------------------------------------------------------------------||
function FixCookieDate (date) {
var base = new Date(0);
var skew = base.getTime();
date.setTime (date.getTime() - skew);
}


//---------------------------------------------------------------------||
// FUNCTION: GetCookie ||
// PARAMETERS: Name ||
// RETURNS: Value in Cookie ||
// PURPOSE: Retrieves cookie from users browser ||
//---------------------------------------------------------------------||
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}

return null;
}


//---------------------------------------------------------------------||
// FUNCTION: SetCookie ||
// PARAMETERS: name, value, expiration date, path, domain, security ||
// RETURNS: Null ||
// PURPOSE: Stores a cookie in the users browser ||
//---------------------------------------------------------------------||
function SetCookie (name,value,expires,path,domain,secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}


//---------------------------------------------------------------------||
// FUNCTION: DeleteCookie ||
// PARAMETERS: Cookie name, path, domain ||
// RETURNS: null ||
// PURPOSE: Removes a cookie from users browser. ||
//---------------------------------------------------------------------||
function DeleteCookie (name,path,domain) {
if (GetCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}


//---------------------------------------------------------------------||
// FUNCTION: MoneyFormat ||
// PARAMETERS: Number to be formatted ||
// RETURNS: Formatted Number ||
// PURPOSE: Reformats Dollar Amount to #.## format ||
//---------------------------------------------------------------------||
function moneyFormat(input) {
var dollars = Math.floor(input)
var tmp = new String(input)
for (var decimalAt = 0; decimalAt < tmp.length; decimalAt++) {
if (tmp.charAt(decimalAt)==".")
break;
}

var cents = "" + Math.round(input * 100)
cents = cents.substring(cents.length-2, cents.length)
dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;

return dollars + "." + cents
}


//---------------------------------------------------------------------||
// FUNCTION: RemoveFromCart ||
// PARAMETERS: Order Number to Remove ||
// RETURNS: Null ||
// PURPOSE: Removes an item from a users shopping cart ||
//---------------------------------------------------------------------||
function RemoveFromCart(RemOrder) {
if (confirm("Click 'Ok' to remove this product from your shopping cart.")) {
NumberOrdered = GetCookie("NumberOrdered");
for(i=RemOrder; i < NumberOrdered; i++) {
NewOrder1 = "Order." + (i+1);
NewOrder2 = "Order." + (i);
database = GetCookie(NewOrder1);
SetCookie (NewOrder2, database, null, "/");
}
NewOrder = "Order." + NumberOrdered;
SetCookie ("NumberOrdered", NumberOrdered-1, null, "/");
DeleteCookie(NewOrder, "/");
location.href=location.href;
}
}


//---------------------------------------------------------------------||
// FUNCTION: GetFromCart ||
// PARAMETERS: Null ||
// RETURNS: Product Table Written to Document ||
// PURPOSE: Draws current cart product table on HTML page ||
//---------------------------------------------------------------------||
function GetFromCart() {
NumberOrdered = 0;
Total=0;
TOTotal=0;
TOquantity = " ";
TOprice = " ";
TOid_num = " ";
TOname = " ";
NumberOrdered = GetCookie("NumberOrdered");
whattowrite = "";

for (i = 1; i <= NumberOrdered; i++) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 );
fields[1] = database.substring( Token0+1, Token1 );
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, database.length );

Total = Total + (fields[1] * fields[0]);
TOTotal = moneyFormat(Total);

whattowrite += "<tr><td>" + fields[2] + "</td><td><font size=-1>"
+ fields[3] + "</font></td><td>$" + fields[1]
+ "</td><td><input type=text size=2 name=\"QUANTITY_"+ i +"\" value=\""
+ fields[0] + "\"></td>"
+ "<td><input type=button value=\" Remove \" onClick=\"RemoveFromCart("+i+")\"></td>"
+ "<input type=hidden name=\"ID_"+ i +"\" value=\"" + fields[2] + "\">"
+ "<input type=hidden name=\"NAME_"+ i +"\" value=\"" + fields[3] + "\">"
+ "<input type=hidden name=\"PRICE_"+ i +"\" value=\"" + fields[1] + "\">";
}

document.write(whattowrite);
document.write("</td></tr><tr><td colspan=2><b>SUBTOTAL</b></td><td>$");
document.write(TOTotal);
document.write("</td><td></td>");
}


//---------------------------------------------------------------------||
// FUNCTION: WriteToForm ||
// PARAMETERS: Null ||
// RETURNS: Product hidden fields Written to Document ||
// PURPOSE: Draws current cart product hidden fields on HTML form ||
//---------------------------------------------------------------------||
function WriteToForm() {
NumberOrdered = 0;
Total=0;
TOTotal=0;
TOquantity = " ";
TOprice = " ";
TOid_num = " ";
TOname = " ";
NumberOrdered = GetCookie("NumberOrdered");
whattowrite = "";

for (i = 1; i <= NumberOrdered; i++) {
NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 );
fields[1] = database.substring( Token0+1, Token1 );
fields[2] = database.substring( Token1+1, Token2 );
fields[3] = database.substring( Token2+1, database.length );

Total = Total + (fields[1] * fields[0]);
TOTotal = moneyFormat(Total);

document.write("<input type=hidden name=\"ID_"+ i +"\" value=\"" + fields[2] + "\">");
document.write("<input type=hidden name=\"NAME_"+ i +"\" value=\"" + fields[3] + "\">");
document.write("<input type=hidden name=\"PRICE_"+ i +"\" value=\"" + fields[1] + "\">");
document.write("<input type=hidden name=\"QUANTITY_"+ i +"\" value=\"" + fields[0] + "\">");
}
}

</script>

What should be the php code for the output?
madhumati is offline   Reply With Quote
Old 03-18-2006, 05:49 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
if you add this code to the top of the page that your form submits to, then you will see how PHP structures the POST data you're sending it:
PHP Code:
<?
if ($_POST) {
  echo 
"<pre>"print_r($_POST); echo "</pre>";
}
generally, you would access POST data with the following format: $_POST['varname']

i don't understand your final question really, but i thought this info may be relevant.
sde is offline   Reply With Quote
Old 03-19-2006, 07:26 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
You are putting the ordered items inside a cookie thru javascript.
This does have issues on computers that disabled javascript, but that's an issue for later concerns.

NewOrder = "Order." + iNumberOrdered;
SetCookie (NewOrder, dbUpdatedOrder, null, "/");
SetCookie ("NumberOrdered", iNumberOrdered, null, "/");

Here you put each item in seperate cookies. You can read this data thru PHP using $_COOKIE.

PHP Code:
if (isset($_COOKIE['NumberOrdered'])) {
    for (
$i=0$i<=$_COOKIE['NumberOrdered']; ++$i) {
        
// do stuff with:
        
$_COOKIE["Order.$i"];
    }

DJMaze is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
can we make single form multiple submit buttons salmanjoo PHP 7 08-20-2005 07:44 AM
sending POST multiple post requests in one php script. sde PHP 2 08-09-2003 05:10 PM
PHP - Hidden Fields and HREF's rockyracoon PHP 4 07-11-2002 10:33 PM
HTML form preview then INSERT using PHP & MySQL SteveSoler PHP 5 07-07-2002 09:54 AM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 11:19 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting