I am developing module for Joomla 2.5. and I had one problem. I wanted to send data to same page and receive answer in it, without reloading. I found solution for it, but then I stuck again.
The problem is that when I want to insert into div tags code <?php echo $msg; ?>
using javascript it turns into comment <!--php echo $msg; ?-->
.
Here is full function
<script type="text/javascript">
window.addEvent('domready', function request() {
$('SNbutton').addEvent('click', function(event) {
event.stop();
var url = window.location.href;
var message = document.getElementById('message');
var msg = '<?php echo $msg; ?>';
var req = new Request.HTML({
method: 'post',
url: url,
data: {'artID' : $('artid').get('value')},
onComplete: function(response) { message.set('html', msg).setStyle('display','inline');
}
}).send();
});
});
</script>
and here is result html:
<form name="accept_form" action="#" ><br/>
<input type="hidden" id="artid" name="artID" value="4"/>
<input type="button" class="SNbutton" id="SNbutton" value="I take it!" title="Accept this job and bound it to your profile.">
</form>
<div id="message" style="display:inline;" ><!--php echo $msg ?--></div>
P.S. This script should take article id and send it to same page, where, depending on article, page will generate message which should be displayed in right place, but somehow its getting corrupt.
P.S.S. Don't worry about that server executes php code. As far as I know its not wotking if script is included trough src in head tags.
Could you try doing it this way?
var msg = <?php echo "'".$msg."'"; ?>;
I'm thinking that maybe the fact it's inside quotes is messing up the interpreter.
A chunk of PHP code inserted into a page that has already been loaded into a browser can't be interpreted by the server - it's just a string. To get the $msg
value you want, you need another page that can respond to your Request.HTML
call and get some code back.
The distinction here is between server-side and client-side code. I think to test this make your url
something like provideMessage.php
and then have THAT page respond to your Request.HTML
.
It's possible I'm missing something about how Joomla modules work.