1

I was trying to build web application where user clicks a button and it prompts them to enter email address using JavaScript prompt functionality like this.

function mail_me(x)
{
        var person = prompt('Please enter your email Address','[email protected]');

}

than I want to call PHP function inside my JavaScript and pass person as parameter of that PHP function. Is it possible to do it. I have tried this so far.

function mail_me(x)
{
        var person=prompt('Please enter your email Address','[email protected]');
            alert('<?php mail_tome('person'); ?>');
}
2
  • In this specific case you can use an echo ("<script> alert('" . mail_tome('person') . "')</script>"); .. but this is a workaround the best way is to use AJAX Commented Jun 4, 2013 at 19:08
  • Do not forget that php will generate an HTML and will return it to the client. PHP is processed on the server and the final information is returned to the client. Commented Jun 4, 2013 at 19:10

3 Answers 3

4

Its not possible, PHP is processed on the server, while Javascript is rendered on the client side after the server has completed it works.

You would need to use Ajax to take the value of person, and pass it via ajax back to a php script on the server which would then process your request.

http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

3 Comments

vanilla-js.com contains an AJAX example that is many times more efficient than jQuery's.
@Kolink I'll have to give that a read over. Sounds interesting.
@PhilCross It's literally just vanilla Javascript.
1

Such a thing is impossible. PHP is executed on the server, and the result is passed to the browser.

You can, however, use AJAX to send the variable to the server. Example:

var a = new XMLHttpRequest();
a.open("POST","/path/to/script.php",true);
a.onreadystatechange = function() {
    if( this.readyState != 4) return;
    if( this.status != 200) return alert("ERROR "+this.status+" "+this.statusText);
    alert(this.responseText);
};
a.send("email="+person);

This will cause whatever the PHP script echos to the browser to appear in the alert. Of course, you can customise this as much as you like, this is just a basic example.

Comments

0

You need something that is called AJAX. If you know and using JQuery it has built in functionality for AJAX.

Example:

function mail_me() {
  var person = prompt('Please enter your email Address','[email protected]');
  $.ajax({
    type: 'post',
    url: URL TO YOUR SCRIPT,
    data: {
       email: EMAIL_VARIABLE
    },
    success: function(result) {
        //handle success
    }
  });
}

3 Comments

Do you see a jQuery tag anywhere?
thats why in my answer I state that if he is using JQuery it would be easier to just use its built in functionality
Erm... In my project, it's as easy as ajax("somepage",{key:"value",key2:"value2"},function(r) {alert("Do something!");});. Much easier than your jQuery. It's called DIY functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.