0


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!

3
  • you have to use AJAX and refresh your DOM in the AJAX callback Commented Jan 29, 2015 at 9:39
  • possible duplicate of Call PHP function from jQuery? Commented Jan 29, 2015 at 9:39
  • 1
    FYI value of id attribute should be unique on the page. For similar html items use class. Commented Jan 29, 2015 at 9:43

2 Answers 2

1

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.

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

Comments

1

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>

Comments

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.