Reputation: 933
This is a follow up to a previous question.
I've got an icon when I click it the icon changes and calls go.php?go=
When you click it again, the icon reverts back to the original icon and calls go.php?stop=
This works. Currently go.php just writes the correct info to a text file, ideally i'd like to see the output from go.php reflected into a DIV on the parent page that has the icons..
How do I do that ?
This is what I have so far..
<script language="javascript">
$(function () {
$('a.tip').on(' click', function () {
var $this = $(this),
container = $('#x')
prevHTML = container.html(),
req = {};
if ( $this.hasClass('go') ) {
$this.find('img').attr('src', 'images/go.gif');
$this.removeClass('go');
req = $.ajax({
url: 'go.php?go=' + $this.attr('id'),
type: 'get',
success: function ( data ) {
container.html(x);
$this.removeClass('go');
}
});
} else {
$this.find('img').attr('src', 'images/stop.gif')
.end().addClass('go');
req = $.ajax({
url: 'go.php?stop=' + $this.attr('id'),
type: 'get',
success: function ( data ) {
container.html( x );
$this.removeClass('go');
}
});
}
});
});
</script>
<a href='#' class='tip' id='4a' onclick=\"load('#');return false;\">
<img src='images/go.gif'>
</a>
<a href='#' class='tip' id='6a' onclick=\"load('#');return false;\">
<img src='images/go.gif'>
</a>
<a href='#' class='tip' id='8a' onclick=\"load('#');return false;\">
<img src='images/go.gif'>
</a>
go.php contains:
<?php
$s = (isset($_REQUEST['go'])) ? "GO".$_REQUEST['go'] : "STOP".$_REQUEST['stop'];
$myFile = "TESTTEST.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $s;
fwrite( $fh, $stringData );
fclose( $fh );
echo $s;
?>
UPDATE: This appears to work :
<script language="javascript">
$(function () {
$('a.tip').on(' click', function () {
var $this = $(this),
container = $('#x')
prevHTML = container.html(),
req = {};
if ($this.hasClass('go')) {
$this.find('img').attr('src', 'images/ok.gif');
$this.removeClass('go');
req = $.ajax({
url: 'go.php?go=' + $this.attr('id'),
type: 'get',
success: function (data) {
container.html(data);
$this.removeClass('go');
}
});
} else {
$this.find('img').attr('src', 'images/error.gif')
.end().addClass('go');
req = $.ajax({
url: 'go.php?stop=' + $this.attr('id'),
type: 'get',
success: function (data) {
container.html(data);
}
});
}
});
});
</script>
Upvotes: 1
Views: 82
Reputation: 337743
You just need to change this line:
container.html(x);
To this:
container.html(data);
data
is the variable containing the text returned from the AJAX request.
Upvotes: 2