0

I have this button which is not a submit button, just a regualr button and it is not inside a <form> tag.

<button type="button" id="send" class="btn btn-outline-blue btn-sm">Send</button>      

I need to execute a separate PHP file (send.php) when I click on the send button

I know how to do this if the button is a type="submit" and is inside a <form>

But in this kind of a situation, I can't think of how to do it. Please HELP...

the_dempa
  • 41
  • 8

3 Answers3

3

Don't use a button. Use a regular link. Use CSS to make it look however you like.

Alternatively, make it a submit button and put it inside a form. The form doesn't need anything else in it. You can style the form to be display: inline if you like.

Alternatively, add a click event handler with JavaScript which assigns a new value to location.

Alternatively, add a click event handler with JavaScript which uses the XMLHttpRequest object or the fetch object to make an HTTP request without leaving the page.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You can use ajax to call the php class on button click event

0

Here's how I did it:

with some research I ended up writing this javascript code which use ajax

Here it's my HTML

<div class="md-form">
    <i class="fa fa-user prefix grey-text"></i>
    <input type="text" id="subject" name="subject" class="form-control ">
    <label for="form-name">subject</label>
</div>

<div class="md-form">
    <i class="fa fa-envelope prefix grey-text"></i>
    <input type="text" name="to" id="to" class="form-control ">
    <label for="form-email">Your email</label>
</div>

<div class="md-form">
    <i class="fa fa-tag prefix grey-text"></i>
    <input type="text"  name="message" id="message" class="form-control">
    <label for="form-Subject">Message</label>
</div>

Here's my PHP file

<?php
    $mailto=$_POST['to'];
    $mailSub=$_POST['subject'];
    $mailMSG=$_POST['message'];
    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail ->IsSmtp();
    $mail ->SMTPDebug=0;
    $mail ->SMTPAuth=true;
    $mail ->SMTPSecure='tls';
    $mail ->Host='smtp.gmail.com';
    $mail ->Port= 587; //465; //or 587
    $mail ->IsHTML(true);
    $mail ->Username="myemail";
    $mail ->Password="password";
    $mail ->SetFrom("email");
    $mail ->Subject=$mailSub;
    $mail ->Body=$mailMSG;
    $mail ->AddAddress($mailto);

    if(!$mail ->Send() || !isset($mailto) || trim($mailto)=='' || !isset($mailSub) || trim($mailSub)=='' || !isset($mailMSG) || trim($mailMSG)=='' ){       
                echo '<script language="javascript">';
        echo 'alert("Mail not Sent!!!!")';
        echo '</script>';
    }else{
        echo '<script language="javascript">';
        echo 'alert("message successfully sent")';
        echo '</script>';           
    }
?>

This is myjavascript in HTML file...

   <script type="text/javascript">
        $(document).ready(function () {
            $("#send").click(function () {

                $.ajax({
                    url: 'SendEmail.php',
                    type: 'POST',
                    dataType: "json",
                    data: {
                        to: $('#to').val(),
                        subject: $('#subject').val(),
                        message: $('#message').val()
                    }
                }).done(function (data) {
                    alert(JSON.stringify(data));                        
                });
            });
        });
    </script>

Well, hope you can get understand about this. Thanks everyone helped :)

the_dempa
  • 41
  • 8