Create working contact form within basket
<p>I've taken over a website where the developer created a bespoke wishlist/basket. Technically it was working but it wasn't getting much response from anyone. I've been tasked with adding a contact form to the end of the process that emails the details to the company. I've used the same contact form on various sites but can't get it to work on this one.</p>

<p>Here's the problematic bit of code:</p>

<pre><code>&lt;script&gt;
function quoteme() {
        $("#msgbox_body").html('&lt;div id="contactform"&gt;&lt;form class="form" method="POST" action="http://www.dtrmedical.com"&gt;&lt;table border="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="name"&gt;&lt;label for="name"&gt;Your Name:&lt;/label&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="name" id="name" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="email"&gt;&lt;label for="email"&gt;E-mail:&lt;/label&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="email" id="email" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="tel"&gt;&lt;label for="tel"&gt;Telephone:&lt;/label&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="tel" id="tel" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="submit"&gt;&lt;input type="submit" value="Submit" name="submit" /&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;? if(isset($_POST['submit'])) { $to = "rob@teamworksdesign.com"; $subject = "DTR Medical Contact enquiry"; $name_field = $_POST['name']; $email_field = $_POST['email']; $tel_field = $_POST['tel']; $body = "DTR Medical,\n\n You have an enquiry from the website, please see the details below:\n\n Name: $name_field\n Company Name: $companyname_field\n Address: $address_field, $address1_field, $address2_field\n Town: $town_field\n County: $county_field\n Postcode: $postcode_field\n Country: $country_field\n E-Mail: $email_field\n Tel: $tel_field\n Message:\n $enquiry_field"; mail($to, $subject, $body); echo "Thank you for getting in touch, we will contact you shortly.";} ?&gt;&lt;/form&gt;&lt;/div&gt;');
        $("#msgbox_actions").html('&lt;input onclick="jQuery.facebox({ ajax: \'wishlist.php?emailme=true\' })" type="button" value="Save list for later" /&gt; &lt;input onclick="jQuery.facebox({ ajax: \'wishlist.php?basket=true\' })" type="button" value="Back to Wishlist" /&gt;');
}
&lt;/script&gt;
</code></pre>

<p>The problem is the form just goes to a blank page on submission. Also I'd like to what products the customer wants in the email that gets sent to the company.</p>

<p>Here's all the code in it's entirety:</p>

<pre><code>&lt;? include "db.php";

function createcode($length = 8, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    $chars_length = (strlen($chars) - 1);
    $string = $chars{rand(0, $chars_length)};
    for ($i = 1; $i &lt; $length; $i = strlen($string))
    {
        $r = $chars{rand(0, $chars_length)};
        if ($r != $string{$i - 1}) $string .=  $r;
    }
    return $string;
}

if (!$_SESSION[cartid] AND ($_POST[update_product] OR $_GET[loadcode])) {
    //echo "&lt;script&gt;alert('cart setup!')&lt;/script&gt;";
    getmycartcode();
}

function getmycartcode() {

    $mycart = mysql_query("SELECT id,basketcode FROM wishlistsaved WHERE sesid = '$sesid'");
    $cart = mysql_fetch_assoc($mycart);

    $sesid = session_id();

    if (!$cart[id]) {
        while ($usecode == false) {
            $basketcode = createcode();
            $findcode = mysql_query("SELECT id FROM wishlistsaved WHERE basketcode = '$sesid'");
            if (mysql_num_rows($findcode) == 0) { $usecode = true; }
        }
        mysql_query("INSERT INTO wishlistsaved (sesid, expires, basketcode) VALUES ('$sesid', NOW() + INTERVAL 14 DAY, '$basketcode')");
        $_SESSION[cartid] = $basketcode;
    } else {
        $_SESSION[cartid] = $cart[basketcode];
    }


    //delete old wishlists!
    $deletethese = mysql_query("SELECT sesid,id FROM wishlistsaved WHERE expires &lt;= NOW()");
    while ($delete = mysql_fetch_assoc($deletethese)) {
        mysql_query("DELETE FROM wishlistbasket WHERE sesid = '$delete[sesid]'");
        mysql_query("DELETE FROM wishlistsaved WHERE id = '$delete[id]'");
    }
}

if ($_POST[updatebasket]) {

    $mybasket = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' ORDER BY pid,qid,oid ASC");
    while ($item = mysql_fetch_assoc($mybasket)) {
        $postname = $item[pid]."_".$item[qid]."_".$item[oid];
        $thisqty = $_POST[$postname];
        if ($thisqty) {
            mysql_query("UPDATE wishlistbasket SET qty = '$thisqty' WHERE sesid = '$sesid' AND id = '$item[id]'")or die(mysql_error());
        } else {
            mysql_query("DELETE FROM wishlistbasket WHERE id = '$item[id]' AND sesid = '$sesid'")or die(mysql_error());
        }

    }
    header("Location: index.php?id=3&amp;basket=true");
    exit;
}

if ($_GET[clearall]) {
    mysql_query("DELETE FROM wishlistbasket WHERE sesid = '$sesid'");
}

if ($_POST[update_product]) {

    $productid = $_POST[productid];
    $get_product_quantities = mysql_query("SELECT * FROM product_quantities WHERE product_id='$productid' ORDER BY id ASC") or die(mysql_error());
    while ($product_quantity = mysql_fetch_assoc($get_product_quantities)) {

        $get_product_options = mysql_query("SELECT * FROM product_options WHERE quantity_id='$product_quantity[id]' ORDER BY id ASC");
        while ($product_option = mysql_fetch_assoc($get_product_options)) {
            $postname = $productid."_".$product_quantity[id]."_".$product_option[id];
            $thisqty = $_POST[$postname];
                $item = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'");
                    if (mysql_num_rows($item) == 0) {
                        $noitem = true;
                        echo "no item&lt;br /&gt;";
                    } else {
                        $noitem = false;
                        echo "item found&lt;br /&gt;";
                    }
                if ($thisqty) {
                    if ($noitem) { //doesnt exist yet so add it to basket
                        echo "insert&lt;br /&gt;";
                        mysql_query("INSERT INTO wishlistbasket (sesid, pid, qid, oid, qty) VALUES ('$sesid','$productid','$product_quantity[id]','$product_option[id]','$thisqty')")or die(mysql_error());
                    } else { //already exists so update basket!
                        echo "update&lt;br /&gt;";
                        mysql_query("UPDATE wishlistbasket SET qty = '$thisqty' WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'")or die(mysql_error());
                    }
                } else { //no post item found but found in basket so delete from basket!
                    echo "delete&lt;br /&gt;";
                    if (!$noitem) { mysql_query("DELETE FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'")or die(mysql_error()); }
                }
        }
    }

    header("Location: index.php?id=3&amp;productid=$productid");
    exit;
}

if ($_GET[loadcode]) {
    $mycode = $_GET[loadcode];

    if ($mycode == $_SESSION[cartid]) {
          echo "&lt;br /&gt;The code you entered is the same as the code already loaded.&lt;br /&gt;&lt;input type=\"text\" name=\"loadcode\" id=\"loadcode\" value=\"$mycode\" class=\"loadcode\" /&gt;";
          exit;
    }

    $mycart = mysql_query("SELECT sesid,id FROM wishlistsaved WHERE basketcode = '$mycode'");
    $cart = mysql_fetch_assoc($mycart);

    if (mysql_num_rows($mycart) == 0) { //did not find code!
        echo "&lt;br /&gt;Sorry the code you entered wasn't found, please check the code and try again...&lt;br /&gt;&lt;input type=\"text\" name=\"loadcode\" id=\"loadcode\" value=\"$mycode\" class=\"loadcode\" /&gt;";
    } else { //found code and do stuff!
        echo "&lt;br /&gt;Code successful, your wish list has been loaded and the code '$mycode'&lt;br /&gt;is valid for another 14 days to use again.&lt;br /&gt;";
        $getitems = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$cart[sesid]'");
        while ($item = mysql_fetch_assoc($getitems)) {
            $searchitems = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$item[pid]' AND qid = '$item[qid]' AND oid = '$item[oid]'")or die(mysql_error());
            if (mysql_num_rows($searchitems) == 1) {
                $searched = mysql_fetch_assoc($searchitems);
                $newqty = $searched[qty] + $item[qty];
                mysql_query("UPDATE wishlistbasket SET qty = '$newqty' WHERE sesid = '$sesid' AND pid = '$item[pid]' AND qid = '$item[qid]' AND oid = '$item[oid]'")or die(mysql_error());
            } else {
                mysql_query("INSERT INTO wishlistbasket (sesid, pid, qid, oid, qty) VALUES ('$sesid','$item[pid]','$item[qid]','$item[oid]','$item[qty]')")or die(mysql_error());
            }
        }
        mysql_query("UPDATE wishlistsaved SET expires = NOW() + INTERVAL 14 DAY WHERE id = '$cart[id]'");
    }
    exit;

}

if ($_GET[myemail]) {

    $message = "&lt;font face=Tahoma size=2&gt;Your wish list code is: $_SESSION[cartid]&lt;br /&gt;&lt;br /&gt;For an updated view of whats in your wish list please type your code into our website: http://www.dtrmedical.com/products/ &lt;br /&gt;&lt;br /&gt;Many Thanks,&lt;br /&gt;DTR Medical&lt;/font&gt;";
    $headers = "From: noreply@dtrmedical.com\r\n" .
           'X-Mailer: PHP/' . phpversion() . "\r\n" .
           "MIME-Version: 1.0\r\n" .
           "Content-Type: text/html; charset=utf-8\r\n" .
           "Content-Transfer-Encoding: 8bit\r\n\r\n";

    mail($_GET[myemail],'Your DTRMedical Wishlist Code',$message,$headers);

    echo "&lt;br /&gt;Your code has been sent to: &lt;b&gt;$_GET[myemail]&lt;/b&gt;";

    exit;
}

if ($_GET[newcode]) {

if ($_SESSION[cartid]) {
    $mycart = mysql_query("SELECT * FROM wishlistsaved WHERE sesid = '$sesid'");
    $cart = mysql_fetch_assoc($mycart);

    if ($cart[id]) { $currentcart = "&lt;br /&gt;
    Your current wish list code is: &lt;strong&gt;$cart[basketcode]&lt;/strong&gt;, insert your new code below:
    &lt;br /&gt;"; }

}

?&gt;
&lt;div id="msgbox_title"&gt;Load Wish List...&lt;div id="closeme" onclick="jQuery(document).trigger('close.facebox')"&gt;x&lt;/div&gt;&lt;/div&gt;
&lt;form method="post"&gt;
&lt;div id="msgbox_body"&gt;Here you can load a previous wish list by using the code that was provided to you,&lt;br /&gt;remember these codes expire 14 days after being created.
&lt;br /&gt;
&lt;div id="newcode"&gt;
&lt;? echo $currentcart; ?&gt;
&lt;input type="text" name="loadcode" id="loadcode" class="loadcode" /&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;div id="msgbox_actions"&gt;&lt;input type="button" onclick="loadmycode()" value="Load Code" /&gt;&lt;/div&gt;
&lt;/form&gt;
&lt;script&gt;
function loadmycode() {
        var thiscode = $("#loadcode").val();
        $("#newcode").html('&lt;br /&gt;loading code...&lt;br /&gt;');
            $('#newcode').load('wishlist.php?loadcode='+thiscode, "",
                function(responseText, textStatus, XMLHttpRequest) {
                    if(textStatus == 'error') {
                        var msg = "Error:&lt;br /&gt;";
                        $("#newcode").html(msg + xhr.status + " " + xhr.statusText);
                    }
                }
            );
}
&lt;/script&gt;
&lt;? } else if ($_GET[addproduct]) { 

$productid = $_GET[addproduct];
$get_product_quantities = mysql_query("SELECT * FROM product_quantities WHERE product_id='$productid' ORDER BY id ASC") or die(mysql_error());

?&gt;
&lt;form method="post" action="wishlist.php"&gt;
&lt;input type="hidden" name="update_product" value="true" /&gt;
&lt;input type="hidden" name="productid" value="&lt;? print $productid; ?&gt;" /&gt;
&lt;div id="msgbox_title"&gt;Choose your option quantities...&lt;div id="closeme" onclick="jQuery(document).trigger('close.facebox')"&gt;x&lt;/div&gt;&lt;/div&gt;
&lt;div id="msgbox_body"&gt;
&lt;table border="0" cellpadding="0" cellspacing="3" align="center"&gt;
&lt;?php
    while ($product_quantity = mysql_fetch_assoc($get_product_quantities)) {
?&gt;

    &lt;tr&gt;
        &lt;td colspan="4" style="padding:0px 9px"&gt;
            &lt;strong&gt;&lt;?php echo $product_quantity['quantity_name']; ?&gt;:&lt;/strong&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
&lt;?php 
    $get_product_options = mysql_query("SELECT * FROM product_options WHERE quantity_id='$product_quantity[id]' ORDER BY id ASC");
    if (mysql_num_rows($get_product_options)==0) {
        echo "&lt;tr&gt;&lt;td colspan='4'&gt;No products in this quantity&lt;/td&gt;&lt;/tr&gt;";
    } else {
?&gt;
        &lt;tr&gt;
        &lt;td style="width: 10px;"&gt;&amp;nbsp;&lt;/td&gt;
        &lt;td style="width: 50px;"&gt;&lt;span style="color: #aaa;"&gt;QTY:&lt;/span&gt;&lt;/td&gt;
        &lt;td style="width: 60px;"&gt;&lt;span style="color: #aaa;"&gt;Code:&lt;/span&gt;&lt;/td&gt;
        &lt;td style="width: 380px;"&gt;&lt;span style="color: #aaa;"&gt;Description:&lt;/span&gt;&lt;/td&gt;
        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;

&lt;?php
    while ($product_option = mysql_fetch_assoc($get_product_options)) {
    $thisitem = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' AND pid = '$productid' AND qid = '$product_quantity[id]' AND oid = '$product_option[id]'");
  $item = mysql_fetch_assoc($thisitem);
?&gt;
    &lt;tr&gt;
        &lt;td&gt; &lt;/td&gt;
        &lt;td&gt;&lt;input type="textbox" class="textbox" value="&lt;? echo $item[qty]; ?&gt;" name="&lt;? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?&gt;" /&gt;&lt;/td&gt;
        &lt;td style="color: #7c7c7c;font-size:11px;"&gt;&lt;?php echo $product_option['product_code']; ?&gt;
        &lt;/td&gt;
        &lt;td style="font-size:11px;"&gt;&lt;?php echo $product_option['description']; ?&gt;
        &lt;/td&gt;
        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
&lt;?php
    } // END while ($product_option = mysql_fetch_assoc( ...

    } // END if (mysql_num_rows($get_product_options)==0) ... else
?&gt;
    &lt;tr colspan="4"&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;?php
    } // END while ($product_quantity = mysql_fetch_assoc ...
?&gt;
&lt;/table&gt;

&lt;/div&gt;
&lt;div id="msgbox_actions"&gt;&lt;input type="submit" value="Add To Wish List" /&gt;&lt;/div&gt;
&lt;/form&gt;
&lt;? } else if ($_GET[basket]) { ?&gt;
&lt;div id="msgbox_title"&gt;Your Wish List&lt;div id="closeme" onclick="jQuery(document).trigger('close.facebox')"&gt;x&lt;/div&gt;&lt;/div&gt;
&lt;form method="post" action="wishlist.php"&gt;
&lt;div id="msgbox_body" style="max-height:400px;min-height:100px;overflow-y:auto;overflow-x:hidden;"&gt;
&lt;input type="hidden" name="updatebasket" value="1" /&gt;
&lt;table border="0" cellpadding="0" cellspacing="3" align="center" style="min-width:390px;"&gt;
&lt;?

    $mycart = mysql_query("SELECT * FROM wishlistsaved WHERE sesid = '$sesid'");
    $cart = mysql_fetch_assoc($mycart);

    $mybasket = mysql_query("SELECT * FROM wishlistbasket WHERE sesid = '$sesid' ORDER BY pid,qid,oid ASC");
    if (mysql_num_rows($mybasket) == 0) { echo "&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;br /&gt;&lt;center&gt;There are no items in your wish list.&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;"; $listempty = true; }
    while ($item = mysql_fetch_assoc($mybasket)) {

        if ($thispid &lt;&gt; $item[pid]) {
            $products = mysql_query("SELECT product_name FROM products WHERE id = '$item[pid]'");
            $product = mysql_fetch_assoc($products);

            if ($thispid &lt;&gt; "") { $br = "&lt;br /&gt;"; }

            echo "&lt;tr&gt;&lt;td colspan=\"5\" style=\"text-align: right;\"&gt;$br&lt;strong style=\"font-size:14px;\"&gt;$product[product_name]&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;";
            $thispid = $item[pid];
        }

        if ($thisqid &lt;&gt; $item[qid]) {
            $quanities = mysql_query("SELECT quantity_name FROM product_quantities WHERE id = '$item[qid]'");
            $thisqty = mysql_fetch_assoc($quanities);

            echo "&lt;tr&gt;&lt;td colspan=\"5\"&gt;&lt;strong style=\"display:block;padding-top:5px;\"&gt;$thisqty[quantity_name]&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;";
            $thisqid = $item[qid];
        }

        $get_product_options = mysql_query("SELECT * FROM product_options WHERE id = '$item[oid]' ORDER BY id ASC");
        $product_option = mysql_fetch_assoc($get_product_options);

        ?&gt;
      &lt;tr&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td style="padding:0px 2px;"&gt;&lt;input type="textbox" class="textbox" value="&lt;? echo $item[qty]; ?&gt;" name="&lt;? echo $item[pid]."_".$item[qid]."_".$item[oid]; ?&gt;" /&gt;&lt;/td&gt;
          &lt;td style="padding:0px 8px;color: #7c7c7c;font-size:11px;"&gt;&lt;?php echo $product_option['product_code']; ?&gt;&lt;/td&gt;
          &lt;td style="padding:0px 2px;font-size:11px;"&gt;&lt;?php echo $product_option['description']; ?&gt;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
        &lt;?

    }

?&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;script&gt;
function quoteme() {
        $("#msgbox_body").html('&lt;div id="contactform"&gt;&lt;form class="form" method="POST" action="http://www.dtrmedical.com"&gt;&lt;table border="0"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="name"&gt;&lt;label for="name"&gt;Your Name:&lt;/label&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="name" id="name" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="email"&gt;&lt;label for="email"&gt;E-mail:&lt;/label&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="email" id="email" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="tel"&gt;&lt;label for="tel"&gt;Telephone:&lt;/label&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="tel" id="tel" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p class="submit"&gt;&lt;input type="submit" value="Submit" name="submit" /&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;? if(isset($_POST['submit'])) { $to = "rob@teamworksdesign.com"; $subject = "DTR Medical Contact enquiry"; $name_field = $_POST['name']; $email_field = $_POST['email']; $tel_field = $_POST['tel']; $body = "DTR Medical,\n\n You have an enquiry from the website, please see the details below:\n\n Name: $name_field\n Company Name: $companyname_field\n Address: $address_field, $address1_field, $address2_field\n Town: $town_field\n County: $county_field\n Postcode: $postcode_field\n Country: $country_field\n E-Mail: $email_field\n Tel: $tel_field\n Message:\n $enquiry_field"; mail($to, $subject, $body); echo "Thank you for getting in touch, we will contact you shortly.";} ?&gt;&lt;/form&gt;&lt;/div&gt;');
        $("#msgbox_actions").html('&lt;input onclick="jQuery.facebox({ ajax: \'wishlist.php?emailme=true\' })" type="button" value="Save list for later" /&gt; &lt;input onclick="jQuery.facebox({ ajax: \'wishlist.php?basket=true\' })" type="button" value="Back to Wishlist" /&gt;');
}
&lt;/script&gt;
&lt;? if (!$listempty) { ?&gt;
&lt;div id="msgbox_actions"&gt;&lt;input onclick="quoteme()" type="button" value="Request quote" /&gt; &lt;!-- &lt;input onclick="jQuery.facebox({ ajax: 'wishlist.php?emailme=true' })" type="button" value="Save list for later" /&gt; --&gt; &lt;input type="submit" value="Update Qty's" /&gt; &lt;input onclick="jQuery.facebox({ ajax: 'wishlist.php?basket=true&amp;clearall=true' })" type="button" value="Clear list" /&gt;&lt;/div&gt;
&lt;? } ?&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;? } ?&gt;
</code></pre>
