PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Wed Apr 18, 2012 7:21 am
hi guys
i have this cart.php code:
Code: Select all
<?php
if(cart_empty()){
echo 'Cart is empty.';
}else {
if(isset($_POST['id'], $_POST['quantity']))
{
update_quantity($_POST['id'], $_POST['quantity']);
}
foreach($_SESSION['cart'] as $product => $quantity)
{
$info = get_details($product);
if($info !== null){
?>
<div style="border:1px dashed #c9c9c9; width:400px;">
<h3><?php echo $info['pages_header']; ?></h3>
<h4 style="color:red;">Price for 1 product: <span style="color:#e27c0e;"><?php echo $info['pages_price']; ?></span></h4>
<center><img src="<?php echo $info['pages_pic']; ?>" width="93" height="101" border="0" alt=""></center>
<p><?php echo $info['pages_text']; ?></p>
<div>
<form action="cart.php" method="post">
<label for="quantity">Quantity: </label><input name="quantity" type="text" value="<?php echo $quantity; ?>" size="2" maxlength="2" />
<input type="hidden" name="id" value="<?php echo $product; ?>" />
<input type="submit" value="Update quantity" />
</form>
<a style="color:red; text-decoration:none;" href="remove_from_cart.php?id=<?php echo $product; ?>">Remove from cart</a>
</div>
</div></br></br>
<?php
}
}
}
?>
and this is my contact form:
Code: Select all
<?php
ob_start();
//Contact us error array
$err=array();
//If the user presses "send" button,this code is executed
if(($_SERVER['REQUEST_METHOD']=="POST") && (isset($_POST['contact'])))
{
//Removing forbidden symbols to prevent SQL Injection,XSS attacks and removing spaces
foreach($_POST as $key=>$value)
{
$_POST[$key]=addslashes(strip_tags(trim($value)));
}
//Extracting array into variables
extract($_POST);
//Checking the name of the sender
if(!empty($name))
{
if(preg_match("/^[a-zא-תا-ي ]+$/i",$name))
{
if((strlen($name)<2) || (strlen($name)>50))
{
$err[]="Your name has to be between 2 and 50 symbols";
}
}
else
{
$err[]="Your name can contain letters only";
}
}
else
{
$err[]="Please enter your name";
}
//Checking the phone number
if(!empty($phone))
{
if(is_numeric($phone))
{
if((strlen($phone)<7) || (strlen($phone)>18))
{
$err[]="Your phone number has to be between 7 and 18 symbols";
}
}
else
{
$err[]="Your phone number can contain digits only";
}
}
else
{
$err[]="Please enter your phone number";
}
//Checking the email of the sender
if(!empty($email))
{
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$err[]="Wrong email input format";
}
}
else
{
$err[]="Please enter your email";
}
//Checking the user's message
if(empty($message))
{
$err[]="Please enter your message";
}
//If all the information entered by the user is correct,we send the message to the administrator
if(empty($err))
{
$message = "<div style='font-size:24px; font-weight:bold;color:green'>New Contact Message</div><div style='height:4px;background:gray'></div><div style='margin:2px 0 10px 0;height:1px;background:gray'></div><div>Hello Mikha,<br><br>There is a new contact message waiting for you:<br>Sender:<b>".$name."</b><br>Email:<b>".$email."</b><br>Phone number:".$phone."<br><br>Message:<b>".$message."</b><br>Site Team</div><div style='margin:5px 0;height:4px;background:gray'></div>";
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'To: Mikha Matta <mekha.r99@gmail.com>' . "\r\n";
$headers .= 'From: <info@mysite.com>' . "\r\n";
mail("mekha.r99@gmail.com", 'A new contact message is received'.time(), $message, $headers);
?>
<script type="text/javascript">location.href="contact_form.php?th=1"</script>
<?php
die();
}
}
if((isset($_GET['th'])) && ($_GET['th']==1))
{
?>
<script type="text/javascript">alert('Thank you your message was sent!');location.href="index.php"</script>
<?php
}
else
{
?>
<form method="post">
<table border="0" align="center" width="300px">
<tr>
<td><p>Name:</p></td>
<td><input type="text" class="forms" maxlength="50" name="name" /></td>
</tr>
<tr>
<td><p>Phone Number:</p></td>
<td><input type="text" class="forms" maxlength="50" name="phone" /></td>
</tr>
<tr>
<td><p>Email:</p></td>
<td><input type="text" class="forms" maxlength="80" name="email" /></td>
</tr>
<tr>
<td><p>Your message:</p></td>
<td><textarea class="forms" name="message" rows="10" cols="25" wrap="physical"></textarea></td>
</tr>
<tr>
<td> </td>
<td style="padding-left:22px;"><input type="submit" style="cursor:pointer;" value="Send" name="contact" /></td>
</tr>
<?php
//If the information entered by the user isn't correct,we display the errors here:
if(!empty($err))
{
?>
<tr>
<th colspan="2">
<?php
foreach($err as $value)
{
echo '<span class="errors">'.$value.'</span><br />';
}
?>
</th>
</tr>
<?php
}
?>
</table>
</form>
<?php
}
?>
.
now, i need when i click check out to get all products and send him to email :\
any suggestions ?
thank you
Last edited by
mekha on Wed Apr 18, 2012 11:25 am, edited 1 time in total.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Apr 18, 2012 8:51 am
mekha wrote: now, i need when i click check out
Where? I don't see any "checkout" thing.
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Wed Apr 18, 2012 9:20 am
the contact form will be my checkout.php file...
just i cant success to add the sessions in the first code to the contact form :\
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Wed Apr 18, 2012 9:41 am
if you want to use sessions you have to start a session with session_start()
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Wed Apr 18, 2012 11:25 am
Ok guys... i solved this problem by myself...
this is the new Contact From code:
Code: Select all
<?php
//Contact us error array
$err=array();
//If the user presses "send" button,this code is executed
if(($_SERVER['REQUEST_METHOD']=="POST") && (isset($_POST['contact'])))
{
//Removing forbidden symbols to prevent SQL Injection,XSS attacks and removing spaces
foreach($_POST as $key=>$value)
{
$_POST[$key]=addslashes(strip_tags(trim($value)));
}
//Extracting array into variables
extract($_POST);
//Checking the name of the sender
if(!empty($name))
{
if(preg_match("/^[a-zא-תا-ي ]+$/i",$name))
{
if((strlen($name)<2) || (strlen($name)>50))
{
$err[]="Your name has to be between 2 and 50 symbols";
}
}
else
{
$err[]="Your name can contain letters only";
}
}
else
{
$err[]="Please enter your name";
}
//Checking the phone number
if(!empty($phone))
{
if(is_numeric($phone))
{
if((strlen($phone)<7) || (strlen($phone)>18))
{
$err[]="Your phone number has to be between 7 and 18 symbols";
}
}
else
{
$err[]="Your phone number can contain digits only";
}
}
else
{
$err[]="Please enter your phone number";
}
//Checking the email of the sender
if(!empty($email))
{
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$err[]="Wrong email input format";
}
}
else
{
$err[]="Please enter your email";
}
//Checking the user's message
if(empty($message))
{
$err[]="Please enter your message";
}
//If all the information entered by the user is correct,we send the message to the administrator
if(empty($err))
{
if(cart_empty()){
$products_email = "<p>No products in cart.</p>";
}else {
$products_email = "<ul>";
foreach($_SESSION['cart'] as $product => $quantity)
{
$info = get_details($product);
if($info !== null){
$products_email .= "<li><ul><li>Name: {$info['pages_header']}</li><li>Quantity: {$quantity}</li><li>Price for 1: {$info['pages_price']}</li><li>Total Price: " . ((str_replace('$', '', $info['pages_price']) * $quantity)) . "$</li></ul></li>";
}
}
$products_email .= "</ul>";
}
$message = "<div style='font-size:24px; font-weight:bold;color:green'>New Contact Message</div><div style='height:4px;background:gray'></div><div style='margin:2px 0 10px 0;height:1px;background:gray'></div><div>Hello Mikha,<br><br>There is a new contact message waiting for you:<br>Sender:<b>".$name."</b><br>Email:<b>".$email."</b><br>Phone number:".$phone."<br><br>Message:<b>".$message."</b><br>Site Team</div><div style='margin:5px 0;height:4px;background:gray'></div><div><p>Products:</p>".$products_email."</div>";
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'To: Mikha <mekha.r99@gmail.com>' . "\r\n";
$headers .= 'From: <' . $email . '>' . "\r\n";
mail("mekha.r99@gmail.com", 'A new contact message is received'.time(), $message, $headers);
//
//
empty_cart();
//
//
?>
<script type="text/javascript">location.href="contact_form.php?th=1"</script>
<?php
die();
}
}
if((isset($_GET['th'])) && ($_GET['th']==1))
{
?>
<script type="text/javascript">alert('Thank you! Your message was sent.');location.href="index.php"</script>
<?php
}
else
{
//If the information entered by the user isn't correct,we display the errors here:
if(!empty($err)):
?>
<tr>
<th colspan="2">
<?php
foreach($err as $value)
{
echo '<span class="errors">'.$value.'</span><br />';
}
?>
</th>
</tr>
<?php
endif;
?>
<form method="post">
<table border="0" align="center" width="300px">
<tr>
<td><p>Name:</p></td>
<td><input type="text" class="forms" maxlength="50" name="name" /></td>
</tr>
<tr>
<td><p>Phone Number:</p></td>
<td><input type="text" class="forms" maxlength="50" name="phone" /></td>
</tr>
<tr>
<td><p>Email:</p></td>
<td><input type="text" class="forms" maxlength="80" name="email" /></td>
</tr>
<tr>
<td><p>Your message:</p></td>
<td><textarea class="forms" name="message" rows="10" cols="25" wrap="physical"></textarea></td>
</tr>
<tr>
<td> </td>
<td style="padding-left:22px;"><input type="submit" style="cursor:pointer;" value="Send" name="contact" /></td>
</tr>
</table>
</form>
<?php
}
?>
<a href="cart.php">View Cart</a>
<center>
<?php
if(cart_empty()){
echo 'Cart is empty.';
}else {
foreach($_SESSION['cart'] as $product => $quantity)
{
$info = get_details($product);
if($info !== null){
?>
<div style="border:1px dashed #c9c9c9; width:400px;">
<h3><?php echo $info['pages_header']; ?></h3>
<h4 style="color:red;">Price for 1 product: <span style="color:#e27c0e;"><?php echo $info['pages_price']; ?></span></h4>
<center><img src="<?php echo $info['pages_pic']; ?>" width="93" height="101" border="0" alt=""></center>
<p><?php echo $info['pages_text']; ?></p>
<p>
Quantity: <?php echo $quantity; ?>
</p>
</div></br></br>
<?php
}
}
}
?>