thaiall logomy background
coding : cart. cartprocess.pl.
my town
Source : Perl | PHP | ASP | JSP | HTML | HTML5 | JavaScript | Java | COBOL | C++ | MS Access | VBNet | VB | Python | โค้ดดี
File : cartprocess.pl. ID : 1002
Skin : Default | Sons-of-obsidian | Sunburst | Highlighter | Full
#!/usr/bin/perl
&parse;
$mailprog='/var/qmail/bin/qmail-inject';
if ($config{'action'} eq "additem") { &additem; }
elsif ($config{'action'} eq "removeitem") { &removeitem; }
elsif ($config{'action'} eq "viewcart") { &viewcart; }
elsif ($config{'action'} eq "tomail") { &tomail; }
else { 
  print "Content-type: text/html\n\n"; 
  print "Nothing to do"; exit; 
}
sub tomail {
  print "Content-type: text/html\n";
  if (&GetCookies('cart')) {
    $mailcookie = "$Cookies{'cart'}";
    $tobeadded = $mailcookie;
    @msg = split(/\|/, $mailcookie);
    if ($config{'email'} && @msg > 0) {
      &sendmail;
      print "\n Your order is processed .. Thank you ";
    } else { 
      print "\n Nothing in cart or invalid e-mail\n";
    }
  } else {
    print "\n Nothing in cart";
  }
}
sub sendmail {
  open (MAIL, "|$mailprog") || die "Can't open $mailprog!\n";  
  print MAIL "To: webmaster\@thaiall.com,$config{'email'}\n";
  print MAIL "From: $config{'email'}\n";  
  print MAIL "Subject: shopping cart \n\n";
  print MAIL "Dear sir\n Thank for your order\n";
  print MAIL "Order by $config{'Name'}\n Address : $config{'Addr'}\n";
  print MAIL "Pay type: $config{'CardType'} \n\n Order listing\n";
  foreach $msg (@msg) {
    ($mquantity, $mprice, $mitem) = split(/:/ , $msg);
    print MAIL "$mitem  $mprice $mquantity\n";
  }
  &totalmny;
  print MAIL "Total: $TheTotal\n";
  print Mail "From IP: $ENV{'HTTP_REFERER'}";
  close (MAIL);
}
sub viewcart {
  print "Content-type: text/html\n";
  if (&GetCookies('cart')) {
    $viewcookie = "$Cookies{'cart'}";
    $tobeadded = $viewcookie;
    @dcarts = split(/\|/, $viewcookie);
    print "\n View cart";
    &incart; 
  } else {
    print "\n Empty";
  }
}
sub removeitem {
  print "Content-type: text/html\n"; 
  if (&GetCookies('cart')) {
    (@ToBeModified) = split(/\|/, $Cookies{'cart'});
    $KillNumber = $config{'number'};
    splice(@ToBeModified,$KillNumber,1);
    $tobeadded = ""; 
    foreach  $restd (@ToBeModified) { 
      if ($tobeadded eq "") { $tobeadded = $restd; 
      } else { $tobeadded = join "|",$tobeadded,$restd; }
      push @dcarts,$restd;
    } 
    &SetCookies('cart',$tobeadded);
    print "\n Remove product in cart";	
    &incart; 
  }
}
sub totalmny {
  $RunningTotal = 0;
  (@tobetotaled) = split(/\|/, $tobeadded);
  foreach $tobetotaled (@tobetotaled) {
    ($HowMany, $HowMuch, $What) = split(/:/, $tobetotaled);
    $ItemTotal = $HowMany * $HowMuch;
    $RunningTotal = $RunningTotal + $ItemTotal;
  }
  $TheTotal = $RunningTotal;
}
sub additem {
  print "Content-type: text/html\n";  
  if (&GetCookies('cart')) {
    $tobeadded="$Cookies{'cart'}\|$config{'quantity'}:$config{'price'}:$config{'item'}";
    &SetCookies('cart',$tobeadded);
    &totalmny;
    @dcarts = split(/\|/, $tobeadded);
    print "\n <b><u>Add new product</u></b>"; 
  } else {
    $newitem = "$config{'quantity'}:$config{'price'}:$config{'item'}";
    &SetCookies('cart',$newitem); 
    $tobeadded = $newitem;
    &totalmny;
    @dcarts = split(/\|/, $newitem);
    print "\n <b><u>First product</u></b>"; 
  }
  print "<br>Product:$config{'item'}<br>Quantity:$config{'quantity'}";
  print "<br>Price:$config{'price'}<hr>";
  &incart;
}
sub parse {
  if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
  } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
  }  
  foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $config{$name} = $value;
  }
}
sub incart {
  $RemoveNumber=0;
  print "<table><tr><td>No</td><td>Product</td><td>Amount</td><td>Price</td><td>Total</td></tr>";
  foreach $dcart (@dcarts) {
    ($quantity, $price, $item) = split(':',$dcart);  
    if ($quantity > "0") {
       $seq = $RemoveNumber + 1;
       print "<tr><td>$seq</td><td>$item</td><td>$quantity</td><td>$price</td><td align=right>";
       printf("%.2f", $price*$quantity);
       print "</td><td><a href=cartprocess.pl?action=removeitem\&number=$RemoveNumber>";
	   print "Remove</a></td></tr>";
      $RemoveNumber++;
    }
  }
  &totalmny;
  print "<tr><td colspan=4 align=right>Total:</td><td align=right>";
  printf("%.2f", $TheTotal);
  print "</td></tr></table>";
  print "<a href=cartprocess.pl?action=viewcart>viewcart</a> | <a href=cartprocess.htm>Product</a>";
  print "<hr><form action=cartprocess.pl method=post><input type=hidden name=action value=tomail>";
  print "<b>Check out</b><table><tr><td>E-mail</td><td><input type=text name=email></td></tr>";
  print "<tr><td>Name-Surn</td><td><input type=text name=Name></td></tr>";
  print "<tr><td>Address</td><td><input type=text name=Addr></td></tr>";
  print "<tr><td>Pay type</td><td>";
  print "<select name=CardType><option value=พกง>พกง<option value=โอนเงิน>โอนเงิน</select>";
  print "</td></tr><tr><td><input type=Submit></td></tr></table></form>";
}
sub GetCookies {
  local(@ReturnCookies) = @_;
  local($cookie_flag) = 0;
  local($cookie,$value);
  if ($ENV{'HTTP_COOKIE'}) {
    foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {
      ($cookie,$value) = split(/=/); 
      foreach $ReturnCookie (@ReturnCookies) {
        if ($ReturnCookie eq $cookie && $value) {
          $Cookies{$cookie} = $value;
          $cookie_flag = 1;
          $cookiename = $cookie;
        }
      }
    }
  }
  return $cookie_flag;
}
sub SetCookies {
  local(@cookies) = @_;  
  ($cookie,$value) = @cookies;  
  print 'Set-Cookie: '.$cookie.'='.$value."\n";
}
จำนวน : 167 บรรทัด
าษาเฮชทีเอ็มแอล (HTML = HyperText Markup Language) คือ ภาษาคอมพิวเตอร์ที่ออกแบบมาเพื่อใช้ในการสร้างเว็บเพจที่เรียกดูผ่านทางเว็บเบราว์เซอร์ (Web Browser) เริ่มพัฒนาโดย ทิม เบอร์เนอรส์ ลี (Tim Berners Lee) ในปัจจุบัน HTML ล่าสุดคือ รุ่น 5 เป็นมาตรฐานหนึ่งของ ISO ซึ่งจัดการโดย World Wide Web Consortium (W3C) ในปัจจุบัน ทาง W3C ผลักดัน รูปแบบของ HTML แบบใหม่ ที่เรียกว่า XHTML รุ่นแรกคือ 1.0 (ม.ค.2543) ซึ่งมีโครงสร้างเป็นแบบ XML (eXtensible Markup Language)
าษาพีเอชพี (PHP Language) คือ ภาษาคอมพิวเตอร์ประเภทโอเพนท์ซอร์ท (Open Source Computer Language) สำหรับพัฒนาเว็บเพจแบบไดนามิก เมื่อเครื่องบริการได้รับคำร้องจากผู้ใช้ก็จะส่งให้กับ ตัวแปลภาษา ทำหน้าที่ประมวลผลและส่งข้อมูลกลับไปยังเครื่องของผู้ใช้ที่ร้องขอ ในรูปเอชทีเอ็มแอล ภาพ หรือแฟ้มดิจิทอลอื่นใด ลักษณะของภาษามีรากฐานคำสั่งมาจากภาษาซี เป็นภาษาที่สามารถพัฒนาให้ใช้งานแบบโต้ตอบกับผู้ใช้ได้
าษาไพทอน (Python Language) คือ ภาษาคอมพิวเตอร์ประเภทโอเพนท์ซอร์ท (Open Source Computer Language) สำหรับพัฒนาแอพพลิเคชั่นโดยไม่ยึดติดกับแพลตฟอร์ม (Platform) และใช้ในงานได้หลายประเภท ทั้งใช้ในการประมวลผลผ่านคอมมานด์ไลน์ หรือเป็นเว็บเพจแบบไดนามิก เมื่อเครื่องบริการได้รับคำร้องจากผู้ใช้ก็จะส่งให้โค้ดให้กับตัวแปลภาษา เพื่อทำหน้าที่ประมวลผลและส่งข้อมูลกลับไปยังเครื่องของผู้ใช้ที่ร้องขอ ในรูปข้อความ เอชทีเอ็มแอล ภาพ หรือแฟ้มดิจิทอลอื่นใด ลักษณะของภาษามีรากฐานคำสั่งมาจากภาษาซี เป็นภาษาที่สามารถพัฒนาให้ใช้งานแบบโต้ตอบกับผู้ใช้ได้ จุดที่แตกต่างกับภาษาอื่น คือ การรวบรวมจุดเด่นของแต่ละภาษามารวมเข้าด้วยกัน
อสคิวแอล (SQL = Structured Query Language) คือ ภาษาสอบถามข้อมูล หรือภาษาจัดการข้อมูลอย่างมีโครงสร้าง มีการพัฒนาภาษาคอมพิวเตอร์ และโปรแกรมฐานข้อมูลที่รองรับมากมาย เพราะจัดการข้อมูลได้ง่าย เช่น MySQL, MariaDB, MsSQL, PostgreSQL หรือ MS Access เป็นต้น สำหรับโปรแกรมฐานข้อมูลที่ได้รับความนิยมคือ MySQL หรือ MariaDB เป็น Open Source ที่ใช้งานได้ทั้งใน Linux และ Windows
วัสดีชาวโลก (Hello World) คือ ผลลัพธ์ครั้งแรกที่นักพัฒนาโปรแกรมนิยมเลือกใช้แสดงผลในโปรแกรมแรกที่เขียนขึ้นเพื่อทดสอบการทำงาน ข้อความนี้ทำให้นึกถึงมนุษย์ต่างดาวที่มาเยือนโลกและกล่าวกับมนุษย์เมื่อพบกันครั้งแรกในภาพยนตร์
ตัวอย่าง Hello World
ปรับปรุงโปรแกรม source.pl : 2565-07-24
rspsocial
Thaiall.com