Danconia
Danconia

Reputation: 563

Updating HTML from PHP function

I am having problems updating a SPAN in my HTML with the result from a PHP file. In the HTML below the is a link that calls the php file that will update the SPAN above it with the new value.

HTML

        <div class="link-votes">
            <span id="v<?php echo $link_id; ?>"><?php echo $votes; ?></span>
        </div>
        <a href="#" class="vote" id="<?php echo $link_id; ?>" name="up" title="Up vote"></a>

PHP

if($_POST['id']) {
    $up_value=$row['vote_count'];
    $("#v"+id).html(html) = $up_value;
}

If I echo $up_value the right integer is printed. But I cannot update the value in the SPAN.\

Thanks,

Upvotes: 0

Views: 119

Answers (3)

we.mamat
we.mamat

Reputation: 712

as the comments to your question show, you're mixing php & jquery code.

your php code should look something like this:

if(isset($_POST['id'])) {
  $up_value=$row['vote_count'];
  echo "<script>$('#v".id."').html('".$up_value."');</script>";
}

Upvotes: 1

Danconia
Danconia

Reputation: 563

Thanks all, your advice helped me figure it out. What I did for other newbies reference was

echo $up_value; 

at the end of the php file and then in my jquery:

success: function(html) {$("#v"+id).html(html);}

Upvotes: 1

heapoverflow
heapoverflow

Reputation: 1052

You are mixing Javascript code with PHP code. You are using the html() function which looks like it might be from the JQuery library. You might want to echo() in PHP and then use Javascript to work on the return value of that echo() statement. It should work as the echo() will be done before the Javascript takes effect, as is the required flow of execution.

Upvotes: 3

Related Questions