开发者

Create working contact form within basket

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.

Here's the problematic bit of code:

<script>
function quoteme() {
        $("#msgbox_body").html('<div id="contactform"><form class="form" method="POST" action="http://www.dtrmedical.com"><table border="0"><tbody><tr><td><p class="name"><label for="name">Your Name:</label></p></td><td>&nbsp;</td><td><input type="text" name="name" id="name" /></td></tr><tr><td><p class="email"><label for="email">E-mail:</label></p></td><td>&nbsp;</td><td><input type="text" name="email" id="email" /></td></tr><tr><td><p class="tel"><label for="tel">Telephone:</label></p></td><td>&nbsp;</td><td><input type="text" name="tel" id="tel" /></td></tr><tr><td><p class="submit"><input type="submit" value="Submit" name="submit" /></p></td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table><? 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.";} ?></form></div>');
        $("#msgbox_actions").html('<input onclick="jQuery.facebox({ ajax: \'wishlist.php?emailme=true\' })" type="button" value="Save list for later" /> <input onclick="jQuery.facebox({ ajax: \'wishlist.php?basket=true\' })" type="button" value="Back to Wishlist" />');
}
</script>

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.

Here's all the code in it's entirety:

<? include "db.php";

function createcode($length = 8, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    $chars_length = (strlen($chars) - 1);
    $string = $chars{rand(0, $chars_length)};
    for ($i = 1; $i < $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 "<script>alert('cart setup!')</script>";
    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 <= 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&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());
    whi开发者_如何学编程le ($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<br />";
                    } else {
                        $noitem = false;
                        echo "item found<br />";
                    }
                if ($thisqty) {
                    if ($noitem) { //doesnt exist yet so add it to basket
                        echo "insert<br />";
                        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<br />";
                        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<br />";
                    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&productid=$productid");
    exit;
}

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

    if ($mycode == $_SESSION[cartid]) {
          echo "<br />The code you entered is the same as the code already loaded.<br /><input type=\"text\" name=\"loadcode\" id=\"loadcode\" value=\"$mycode\" class=\"loadcode\" />";
          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 "<br />Sorry the code you entered wasn't found, please check the code and try again...<br /><input type=\"text\" name=\"loadcode\" id=\"loadcode\" value=\"$mycode\" class=\"loadcode\" />";
    } else { //found code and do stuff!
        echo "<br />Code successful, your wish list has been loaded and the code '$mycode'<br />is valid for another 14 days to use again.<br />";
        $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 = "<font face=Tahoma size=2>Your wish list code is: $_SESSION[cartid]<br /><br />For an updated view of whats in your wish list please type your code into our website: http://www.dtrmedical.com/products/ <br /><br />Many Thanks,<br />DTR Medical</font>";
    $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 "<br />Your code has been sent to: <b>$_GET[myemail]</b>";

    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 = "<br />
    Your current wish list code is: <strong>$cart[basketcode]</strong>, insert your new code below:
    <br />"; }

}

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

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

    <tr>
        <td colspan="4" style="padding:0px 9px">
            <strong><?php echo $product_quantity['quantity_name']; ?>:</strong>
        </td>
    </tr>
<?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 "<tr><td colspan='4'>No products in this quantity</td></tr>";
    } else {
?>
        <tr>
        <td style="width: 10px;">&nbsp;</td>
        <td style="width: 50px;"><span style="color: #aaa;">QTY:</span></td>
        <td style="width: 60px;"><span style="color: #aaa;">Code:</span></td>
        <td style="width: 380px;"><span style="color: #aaa;">Description:</span></td>
        <td>&nbsp;</td>
    </tr>

<?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);
?>
    <tr>
        <td> </td>
        <td><input type="textbox" class="textbox" value="<? echo $item[qty]; ?>" name="<? echo $productid."_".$product_quantity[id]."_".$product_option[id]; ?>" /></td>
        <td style="color: #7c7c7c;font-size:11px;"><?php echo $product_option['product_code']; ?>
        </td>
        <td style="font-size:11px;"><?php echo $product_option['description']; ?>
        </td>
        <td>&nbsp;</td>
    </tr>
<?php
    } // END while ($product_option = mysql_fetch_assoc( ...

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

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

    $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 "<tr><td><br /><br /><center>There are no items in your wish list.</center></td></tr>"; $listempty = true; }
    while ($item = mysql_fetch_assoc($mybasket)) {

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

            if ($thispid <> "") { $br = "<br />"; }

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

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

            echo "<tr><td colspan=\"5\"><strong style=\"display:block;padding-top:5px;\">$thisqty[quantity_name]</strong></td></tr>";
            $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);

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

    }

?>
</table>
</div>
<script>
function quoteme() {
        $("#msgbox_body").html('<div id="contactform"><form class="form" method="POST" action="http://www.dtrmedical.com"><table border="0"><tbody><tr><td><p class="name"><label for="name">Your Name:</label></p></td><td>&nbsp;</td><td><input type="text" name="name" id="name" /></td></tr><tr><td><p class="email"><label for="email">E-mail:</label></p></td><td>&nbsp;</td><td><input type="text" name="email" id="email" /></td></tr><tr><td><p class="tel"><label for="tel">Telephone:</label></p></td><td>&nbsp;</td><td><input type="text" name="tel" id="tel" /></td></tr><tr><td><p class="submit"><input type="submit" value="Submit" name="submit" /></p></td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table><? 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.";} ?></form></div>');
        $("#msgbox_actions").html('<input onclick="jQuery.facebox({ ajax: \'wishlist.php?emailme=true\' })" type="button" value="Save list for later" /> <input onclick="jQuery.facebox({ ajax: \'wishlist.php?basket=true\' })" type="button" value="Back to Wishlist" />');
}
</script>
<? if (!$listempty) { ?>
<div id="msgbox_actions"><input onclick="quoteme()" type="button" value="Request quote" /> <!-- <input onclick="jQuery.facebox({ ajax: 'wishlist.php?emailme=true' })" type="button" value="Save list for later" /> --> <input type="submit" value="Update Qty's" /> <input onclick="jQuery.facebox({ ajax: 'wishlist.php?basket=true&clearall=true' })" type="button" value="Clear list" /></div>
<? } ?>
</form>
</div>
<? } ?>


The reason the form goes to a blank page is because the <form> has the action attribute set to whishlist.php AND on submit you are not stopping the form's submit action which is to navigate to the given page with the supplied parameters using the method (post or get) you provided.

To avoid redirection add return false; at the end of onclick function of the submit button.

Aside from that you have many things wrong with your code. Before I try to fix them, I would need to know what is the end result you want.

Do you want the whishlist.php to send the mail ?

What is the code in whishlist.php as that is the page requested via ajax and thus will be responsible for initiating any server side action.

Also why are you using PHP <? ?> code in your javascript function quoteme() as the $.html() contents. It wont work that way.

The most likely solution to your problem is (best guess without the additional info):

Add the email sending code to wishlist.php, AND send the name, email and telephone to wishlist.php via AJAX which you are already doing with only one parameter.

So your wishlist.php should look like :

...
if(isset($_GET['emailme']) && $_GET['emailme'] == 'true') { 
    $to = "youremail@company.com"; 
    $subject = "Company Contact enquiry"; 
    $name_field = $_GET['name']; 
    $email_field = $_GET['email']; 
    $tel_field = $_GET['tel']; 

    /* get other needed details */

    $body = "Company,\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.";
}
....

While your AJAX Call on submit button should be like:

jQuery.facebox({ ajax: (\'wishlist.php?emailme=true&name=\' + this.form.name.value + \'&email=\' + this.form.email.value + \'&tel=\' + this.form.tel.value) }); return false;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜