0
votes

I am trying to make a simple price match contact form on my product page in Magento. I have a separate php file but it doesn't seem to receive the commands from the view.phtml file.

this is the form code which is located inside /app/design/frontend/default/my_design/template/catalog/product/view.phtml

<div class="pricematcher">
                    <body class="body">
                        <div id="pricematch" style="display:none;">
                        <!-- Popup Div Starts Here -->
                            <div id="popupContact">
                            <!-- Contact Us Form -->
                            <form action="send_contact.php" id="form" method="post" name="form">
                            <img id="close" src="<?php echo $this->getSkinUrl(); ?>images/close.png" onclick="div_hide()"</img>
                            <h2 class="h2price">Price Match</h2>
                            <hr id="hrprice">
                            <input id="name" name="name" placeholder="Name" type="text">
                            <input id="email" name="email" placeholder="Email" type="text">
                            <input id="productname" name="productname" placeholder="<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?><?php echo $this->__(' PART #: ');?><?php echo $_helper->productAttribute($_product, nl2br($_product->getSku()), 'sku') ?>" type="text" readonly>
                            <input id="competitor" name="competitor" placeholder="Competitor Product Link" type="text">
                            <textarea id="msg" name="msg" placeholder="Message"></textarea>
                            <a href="javascript:%20check_empty()" id="submit">Submit</a>
                            </form>
                            </div>
                            <!-- Popup Div Ends Here -->
                        </div>
                            <!-- Display Popup Button -->

                    </body>
                </div> 
                <img id="popup" src="<?php echo $this->getSkinUrl(); ?>images/price-match.png" onclick="div_show()"</img>

this is the javascript which makes the form pop up and display

// Validating Empty Field
function check_empty() {
if (document.getElementById('name').value == "" ||      document.getElementById('email').value == "" ||     document.getElementById('msg').value == "") {
alert("Fill All Fields !");
} else {
document.getElementById('form').submit();
alert("Thank You for submitting a Price Match inquiry");
}
}
//Function to Hide Price Match
function div_hide(){
document.getElementById('pricematch').style.display = "none";
}
//Function To Display Price Match
function div_show() {
document.getElementById('pricematch').style.display = "block";
}

this is the php file that the form action should send to send_contact.php file located in the same directory as view.phtml

<?php
$subject = "Price Match";
$message = $_POST['msg'];
$name = $_POST['name'];
$product = $_POST['productname'];
$competitor = $_POST['competitor'];
$mail_from = $_POST['email'];

// Enter Your email Adress
$to = "[email protected]";
$body = "$message, $competitor, $product";
$send_contact = mail($to,$subject,$body, "From: " . $mail_from);

//Check if message sent
if ($send_contact){
   echo "we recieved";
}
else {
echo "error";
}
?>

Every thing works when I make this in a seperate folder on my server and execute outside of magento.

2
i have also tried to put a magento contact form that works on my contact us page but doesnt work on the product page i have put this in my catalog/product/view.phtml: <?php echo $this->getLayout()->createBlock("core/template")->setTemplate("contacts/form.phtml")->setBlockId(1)->toHtml(); ?> this displays the contact form and the required fields work but it doesnt send the email. Is this being caused by the view.phtml being a block inside of the product page? - Colin Green
to add email action you will need to add it in controller this will not work the way you are putting it. create a custom module implement a controller with it. set your form's action to this controller and then place the mail function into it - Harshada Chavan

2 Answers

0
votes

You should put send_contact.php at magento's root directory.

0
votes

I think you can create an action in ProductController.php,for example,postEmailAction, then in the postEmailAction,you can get the data from the form:

$message = $_POST['msg'];
$name = $_POST['name'];
$product = $_POST['productname'];
$competitor = $_POST['competitor'];
$mail_from = $_POST['email'];

, flow the code: `

$mail = Mage::getModel('core/email');
$mail->setToName('YourStore Admin');
$mail->setToEmail($toEmail);
$mail->setBody($body);
$mail->setSubject('YourStore: New Product Review');
$mail->setFromEmail('[email protected]');
$mail->setFromName("YourStore");
$mail->setType('html');
 try {
  $mail->send();
}
catch (Exception $e) {
  Mage::logException($e);
}

` the action url will be the product controller + the action name; Hope this will help you.