1

I am trying to make an if statement that will launch my mail(); php code when an image is clicked.

I have tried this so far:

<?php 
$to = "ravncompany@gmail.com";
$subject = $_POST['subject'];
$message = $_POST['TextField'];
$header = "From: MyEmail@email.com";

if (isset($_POST['SendButton'])) {
mail($to, $subject,  $message, $header);
}
?>

html

<input type="image" alt="submit form" name="SendButton" id="SendButton" src="./Photos/SendButton.png">

this does not send my email though I am sure it is the if statement that is the problem since if i remove the if statement then it will send an email.

but even if I did get my code to work then i don't think it would cut it since I am making an Ajax web and right now when I press the input image then it refreshes the page. In my case this means it takes me from contact page to home page.

the way my web functions is that i have a Main.php which i load contact.php, home.html, etc... into . So when in contact.php pressing a submit button will refresh the site loading home.html into main.php since home.html is set to default.

So I need to launch an if statement in php when an image is clicked without refreshing the page.

I have tried several things but can't quite get there. can someone help me do this right?

Nulle
  • 1,301
  • 13
  • 28

2 Answers2

1

call this function in you onclick event

<script>
function mail()
    {
            var subject="your value";
            var message="your message";

                   $.ajax({
                        type: "post",    
                        context: document.body,
                        url: "mail.php",
                        data: {id: subject ,name:message},
                        success: function(data)
                        {         
                              alert("mail sent successfully");                           
                        }

                    });                                               
    }
</script>

in your mail.php

<?php 
$to = "ravncompany@gmail.com";
$subject = $_POST['subject'];
$message = $_POST['TextField'];
$header = "From: MyEmail@email.com";

mail($to, $subject,  $message, $header);

?>

and you image button like this

<input type="image" alt="submit form" onclick="mail()" name="SendButton" id="SendButton" src="./Photos/SendButton.png">
Sahil Manchal
  • 472
  • 6
  • 20
0

You've stated you need to send the email via an ajax call, not a page reload. This cannot be done in pure PHP, since it is a server-side language. While you're on track to send the email from your PHP code, the ajax call to your PHP script must be done via Javascript.

to keep your form from refreshing the page checkout this answer to that very question on stackoverflow: https://stackoverflow.com/a/1263859/2646455

here's some reading material about ajax in jQuery (a really helpful javascript library) http://api.jquery.com/jquery.ajax/ (http://api.jquery.com is a wealth of information for beginners learning javascript and event-driven programming, as is http://www.w3schools.com/jquery/).

Community
  • 1
  • 1
chris.nesbit1
  • 333
  • 2
  • 9