Training, Open Source computer languages

This is page http://www.wellho.net/forum/Writing-PHP/Sending- ... tomer.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Sending mail with a order from a customer

Posted by torriv (torriv), 28 April 2007
Hello again! long time since last time!

I have a little problem, that I can't figure out..

I have a mysql database, where the information on what a customer has choose to buy is,  and this information is shown on det cart side.

now i have just the last step of this webshop before it is complete..

I use a php code that sends the information to email on a contact page, and it work perfect, but when i use it on the cart side, it doesn't work..  the code on the contact side for sending the mail is as follow:

Code:
<?php
$to = "btplast@btplast.com" ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$telefon = $_REQUEST['telefon'] ;
$headers = "From: $from";
$subject = "Mail fra kontaktskjema";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"telefon"} = "telefon";
$fields{"Message"} = "Message";

$body = "Følgende mail ble sendt fra hjemmesiden:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }


if($send)
if($from == '') {print "Du har ikke skrevet inn noen Email";}
else {
if($name == '') {print "Du har ikke skrevet noe navn";}
else {
$send = mail($to, $subject, $body, $headers);
if($send)
{ print "Mailen har nå blitt sendt! Takk for at du tok deg tid! :) <A href='index.html'>Tilbake til start</a>";}
else
{print "Et problem oppstod ved sending av mail. Vennligst send en mail til: tor@trivera.net"; }
}
}
?>
     
     
      <form method="POST" action="kontakt.php">
     <table width="69%" height="0%" border="0" align="center">
       <tr>
         <td height="33%" valign="bottom"><dl>
             <dt class="style14"><strong>Sekret&aelig;r<br />
               </strong>Hilde Granholt...................<span class="style19">911
               53 041</span></dt>
           </dl></td>
       </tr>
       <tr>
         <td height="33%" valign="bottom"><dl>
               <dt class="style14"><strong>Produksjonsansvarlig<br />
                 </strong>Stein Br&aring;then....................915 34 110</dt>
             </dl></td>
       </tr>
       <tr>
         <td height="34%" valign="bottom"><dl>
               <dt class="style14"><strong>Daglig Leder<br />
                 </strong>Lars Br&aring;then.....................995 93 154<em><span class="style14"><strong>
                 </strong></span></em></dt>
             </dl>
           </td>
       </tr>
     </table>
     <br />
       
     <table width="67%" height="0%" border="0" align="center">
       <tr>
         <td height="10%" colspan="2"><div align="right"></div></td>
       </tr>
       <tr>
         <td width="42%" height="11%"><div align="left"><span class="style20">Navn:</span></div></td>
         <td width="58%"><input name="Name" type="text" id="Name" /></td>
       </tr>
       <tr>
         <td height="11%"><div align="left"><span class="style20">Telefon:</span></div></td>
         <td><input name="telefon" type="text" id="telefon" /></td>
       </tr>
       <tr>
         <td height="11%"><div align="left"><span class="style20">E-post:</span></div></td>
         <td><input name="Email" type="text" id="Email" /></td>
       </tr>
       <tr>
         <td height="44%" valign="top"><div align="left"><span class="style20">Din foresp&oslash;rsel:</span></div></td>
         <td><textarea name="Message" rows="5" id="Message"></textarea></td>
       </tr>
       <tr>
         <td height="13%" valign="top"><div align="right"></div></td>
         <td><input type="submit" name="send" value="Send" /> <input type="reset" name="Submit2" value="Angre" /></td>
       </tr>
     </table></form>


And the code on the cart, where it doesn't work is:

Code:
<?php
$to = "tor@trivera.net" ;
$from = $_POST['Email'] ;
$name = $_POST['Name'] ;
$telefon = $_POST['telefon'] ;
$adresse = $_POST['adresse'] ;
$postadr = $_POST['postadr'] ;
$headers = "From: $from";
$subject = "Mail fra kontaktskjema";

$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"telefon"} = "telefon";
$fields{"Message"} = "Message";
$fields{"adresse"} = "adresse";
$fields{"postadr"} = "postadr";

$body = "Følgende mail ble sendt fra hjemmesiden:\n\n" . $row->itemNem . "\n" . 'til ' . $row->itemPrice;


if(isset($_REQUEST['submit']))
if($from == '') {print "Du har ikke skrevet inn noen Email";}
else {
if($name == '') {print "Du har ikke skrevet noe navn";}
else {
$send = mail($to, $subject, $body, $headers);
if($send)
{ print "Mailen har nå blitt sendt! Takk for at du tok deg tid! :) <A href='index.html'>Tilbake til start</a>";}
else
{print "Et problem oppstod ved sending av mail. Vennligst send en mail til: tor@trivera.net"; }
}
}
?>
     
     
     <form name="frmCart" method="get" action="cart.php" >
       <table width="100%" cellspacing="0" cellpadding="0" border="0">
         <tr bgcolor="#FFFFFF">
           <td width="18%" height="25"> <strong><font face="verdana" size="1" color="#000000">
             &nbsp;&nbsp;Antall </font> </strong></td>
           <td width="35%" height="25"> <strong><font face="verdana" size="1" color="#000000">
             Produkt </font> </strong></td>
           <td width="16%" height="25"> <strong><font face="verdana" size="1" color="#000000">
             Pris for 1 </font> </strong></td>
           <td width="31%" height="25"> <strong><font face="verdana" size="1" color="#000000">
             fjerne? </font> </strong></td>
         </tr>
         <?php
                 
                 while($row = mysql_fetch_array($result))
                 {
                       // Increment the total cost of all items
                       $totalCost += ($row["qty"] * $row["itemPrice"]);
                       ?>
         <tr>
           <td width="18%" height="25"> <font face="verdana" size="1" color="black">
             <select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
               <?php
                                               
                                                     for($i = 1; $i <= 200; $i++)
                                                     {
                                                           echo "<option ";
                                                           if($row["qty"] == $i)
                                                           {
                                                                 echo " SELECTED ";
                                                           }
                                                           echo ">" . $i . "</option>";
                                                     }
                                               ?>
             </select>
             </font> </td>
           <td width="35%" height="25"> <font face="verdana" size="1" color="black">
             <?php echo $row["itemName"]; ?> </font> </td>
           <td width="16%" height="25"> <font face="verdana" size="1" color="black">
             <?php echo number_format($row["itemPrice"], 2, ".", ","); ?>,-NOK
             </font> </td>
           <td width="31%" height="25"> <font face="verdana" size="1" color="black">
             <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Fjern
             fra handlekurv</a> </font> </td>
         </tr>
         <?php
                 }
                 
                 // Display the total
                 ?>
         <tr>
           <td colspan="4"> <hr size="1" color="red" NOSHADE> </td>
         </tr>
         <tr>
           <td colspan="2"> <font face="verdana" size="1" color="black"> <a href="bestilling.php">&lt;&lt;
             Fortsett å handle</a> </font> </td>
           <td colspan="2"> <font face="verdana" size="2" color="black"> <b>Total
             Beløp: <?php echo number_format($totalCost, 2, ".", ","); ?>,-NOK</b>
             </font> </td>
         </tr>
         <tr>
           <td colspan="4"><div align="left"><br />
               <table width="75%" border="0" align="center" cellpadding="3">
                 <tr>
                   <td><div align="right">Navn/Bedrift: </div></td>
                   <td><input name="Name" type="text" id="Name" /></td>
                 </tr>
                 <tr>
                   <td><div align="right">Telefon nummer: </div></td>
                   <td><input name="telefon" type="text" id="telefon" /></td>
                 </tr>
                 <tr>
                   <td><div align="right">Adresse: </div></td>
                   <td><input name="adresse" type="text" id="adresse" /></td>
                 </tr>
                 <tr>
                   <td><div align="right">Post adresse:</div></td>
                   <td><input name="postadr" type="text" id="postadr" /></td>
                 </tr>
                 <tr>
                   <td><div align="right">E-post:</div></td>
                   <td><input name="Email" type="text" id="Email" /></td>
                 </tr>
                 <tr>
                   <td valign="middle"><div align="right">Kommentar:</div></td>
                   <td><textarea name="Message" id="Message"></textarea></td>
                 </tr>
                 <tr>
                   <td valign="top">&nbsp;</td>
                   <td><input name="send" type="submit" id="send" value="Send" /></td>
                 </tr>
               </table>
             </div>
             <div align="left"> </div>
             </td>
         </tr>
         <tr>
           <td colspan="2">&nbsp;</td>
           <td colspan="2">&nbsp;</td>
         </tr>
       </table>
                       </form>


Can you see where the problem is?

Thanks for your time!

Posted by admin (Graham Ellis), 28 April 2007
It's very diificult so spot an error in a long piece of code, especially when the analysis is "does not work".  That leaves me looking for a needle in a haystack (that may not translate well) without knowing what the needle even looks like  

However ....

I GUESS that simply nothing happens, because of the
if(isset($_REQUEST['submit']))
line.   In this line, you're checking to see whether you have a form element  that includes a name=submit, but I don't see one.  You've probably renamed your "submit" button to "send" ....

Posted by torriv (torriv), 28 April 2007
yes, it was as simple as that..

but know i get the error: "you havent insert any email"

The line on norwegian on the code is:
Code:
if($from == '') {print "Du har ikke skrevet inn noen Email";}


but i do insert a email...

Posted by admin (Graham Ellis), 28 April 2007
There are two ways of sending form data to the next script - GET and POST.  You are specifying in the form tag that you wish to use the GET method, but then you have used $_POST which *only* collects data from the POST method.

Suggestion - replace method=get by method=post in the open form tag.

Posted by torriv (torriv), 28 April 2007
and again, it was that simple... it's hard to see that small things when u have that much of a code:p

when i got the email, it had everything in the FORM that i had input.. BUT not the thing i had order...

i have change a little on the code on the $body="" as follow:
Code:
$body = "Følgende mail ble sendt fra hjemmesiden:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $row->itemNem . "\n" . 'til ' . $row->itemPrice;


and this is supposed to give me the things i have ordered..

if u wonna see the code for the whole code for this page it is here: http://norskwebforum.no/pastebin/8381



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho