Skip to content Skip to sidebar Skip to footer

Calling Javascript With Php

I want to call a PHP function when pressing on a button, sort of like:

Here is what you could use on in your javascript client using the jQuery library as a helper to do the AJAX call:

<inputtype="button"value="Enter"onclick="output()"/><scripttype="text/javascript">functionoutput(){
    $.ajax({
        type: "POST",
        url: "submit_data.php",
        data:   "username=" + "SomeUser" 
        +   "&email=" + "someEmail@google.com"
                    +       "&functionName=" + "theFunction1",
        success: function(html){
            alert('sucess!  Result is:' + html);
        }
    });

    }   
</script>

and you can use code such as this to catch the data your javascript is passing in. In this example you would want to call this file name as "submit_data.php" to match the javascript above:

<?php// Variables$Username = $_POST['username'];
    $Email    = $_POST['email'];    
    $FunctionName = $_POST['functionName'];

    //Add code here to choose what function to call and echo the result// If $FunctionName equals 'theFunction1' then execute theFunction1// If $FunctionName equals 'theFunction2' then execute theFunction2echo"You called A Page!";

?>

Here I am doing nothing with the "username" and "email" simply grabbing it and storing them into holding variables. But you can easily add extra functionality here, such as checking for a name of a function that you want to run.

Solution 3:

PHP is server side and javascript is client side. So I'm not sure if that is really what you want to be doing??

Perhaps you could explain why you want to specifically call a php function?

Solution 4:

I googled PHP function from button and found this question on webdeveloper.com

It doesn't use Javascript.

Solution 5:

This is PHP you're talking about, not ASP.NET. In PHP, there is no such thing as a button click event. PHP runs entirely on the server and has absolutely no knowledge of client-side events.

Your first try won't work because the PHP code only runs when the page first loads. It does not run when you call a JavaScript function. Your second example won't work because JavaScript and PHP can't talk directly to eachother like that. Trying to directly call a PHP function from JavaScript just doens't make sense. Remember, PHP only runs on the server. By the time you get to the point where JavaScript can run, the PHP code has long since completed its work.

If you want to do something when a button is clicked, you have to explicitly make a request back to the server. You can do this by just POSTing the form as CTphpnwb suggested. Just be aware that this will reload the page and you will have to manually save and restore the page state, e.g. repopulate input boxes. There is no built-in magic that will do this for you.

Alternatively, you can get all AJAXy and do the POST in JavaScript. However, you will have to write the JavaScript to send the request and process the response, and write the server-side PHP code to handle the request. This gets a little awkward to do in a single page.

From : http://www.dreamincode.net/forums/showtopic72353.htm

Post a Comment for "Calling Javascript With Php"