VladutZzZ
VladutZzZ

Reputation: 720

Call a php function in a javascript with params


I need to call a PHP function with params from JavaScript when I click on some text in a html file; yeah, I know, it sounds like $#@!$@#

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript" language="javascript">
$(document).ready(function() { /// Wait till page is loaded
$('#detailed').click(function(){
$('#main').load('parse.php?file=gz.xml', function() {
/// can add another function here
});
});
}); //// End of Wait till page is loaded
</script>


<a><div id="detailed">Stuff1</div></a>
<a><div id="detailed">Stuff2</div></a>
<div id="main">Hello - This is my main Div that will be reloaded using jQuery.</div>

What I want to make, is when I click Stuff1 on page, to call function parseLog('Stuff1'), and when I click Stuff2, to call function parseLog('Stuff2').
function parseLog(), is a PHP function, in functions.php
Anybody know a solution?
Thanks!

Upvotes: 0

Views: 2326

Answers (2)

Sergei Akimov
Sergei Akimov

Reputation: 21

You should try something like this :

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript" language="javascript">
function myFunc (myData) {
    $.ajax( {
        type: "POST",
        url: "parse.php?file=gz.xml",
        dataType: 'json',
        data: { param: myData},
        success: function( result ) {
             $("#main").append(result);
        }
    }
}

<a href="#" onclick='myFunc("Stuff1");'><div id="detailed">Stuff1</div></a>
<a href="#" onclick='myFunc("Stuff2");'><div id="detailed">Stuff2</div></a>
<div id="main"></div>

Upvotes: 1

karmendra
karmendra

Reputation: 2243

Javascript is client side script and PHP is server side script. So if you think, you need a call to that function over the network and get the response over the network. you should try using a coding method called AJAX. To make life easier and cross browser compatible use jQuery library and its ajax functionality.

Look at this post for a quick intro to jQuery and AJAX.

It helped me , hope this helps you too.

Upvotes: 1

Related Questions