简体   繁体   中英

call php function from javascript/jquery using ajax

Sorry, I am very new to AJAX, and I while there are tons of examples out there, it is hard for me to understand. If you could please help me out with my specific example I'd much appreciate it. The AJAX syntax is just very strange to me. Anyway, I am validating a contact form in javascript. If it every field has values in it, then it is to call a php function included in the page to send the email. Could someone please show me how to do this and EXPLAIN it? I am very confused by how AJAX works. Server/client differences? Sorry. Thank you. Here is my code (that fails not using AJAX):

<?php 
        require_once("mail.php");
    ?>
    <script language='Javascript' type='text/javascript'>
        function validation(){
            var name = document.getElementById('name').value;
            var email = document.getElementById('email').value;
            var phone = document.getElementById('phone').value;             
            var subject = document.getElementById('subject').value;
            var message = document.getElementById('message').value;

            if (name == '') {   
                alert('Please enter your name.');
                location.reload(false);
            }
            else {
                if (email == '') {
                    alert('Please enter your email.');
                    location.reload(false);
                }
                else {
                    if (phone == '') {
                        alert('Please enter your phone number.');
                        location.reload(false);
                    }
                    else {
                        if (subject == '') {
                            alert('Please enter a subject.');
                            location.reload(false);
                        }
                        else {
                            if (message == '') {
                                alert('Please enter a message.');
                                location.reload(false);
                            }
                            else {
                                var send = confirm('Name: ' + name + '\r\nEmail: ' + email + '\r\nPhone: ' + phone + 'Subject: ' + subject + '\r\nMessage:\r\n' + message + '\r\nPress [Ok] to send or [Cancel] to return to the contact page.');
                                if (send) {
                                    window.location='';
                                }
                                else {
                                    location.reload(false);
                                }
                            }
                        }
                    }
                }
            }
        }
    </script>

I know the javascript works up to the part where I need to invoke the php sendmail() function. Thanks for the help and support!

Javascript is executed on client (browser), PHP on server-side. So you cannot call server-side functions from javascript; you have to send your client data (email, phone, etc...) with an HTTP get/post to the server (with AJAX, FORMS or whatever you wnat), then when your server-side PHP script receive the data you can prepare mail to be sent and call the sendmail() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM