0

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,

2
  • 4
    You're mixing PHP and Javascript code. How you could ever get that to run is beyond me... your PHP snippet is utterly invalid. Commented Jun 26, 2012 at 3:53
  • In the PHP section it looks like you're trying to do jQuery from PHP (server side versus client side). On the client side using jQuery you likely want to perform an AJAX callback to a PHP endpoint that responds JSON as an example, where the client side jQuery then would parse the JSON and make the appropriate dom changes. Commented Jun 26, 2012 at 3:53

3 Answers 3

3

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.

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

1 Comment

Alternately, he might want to do a bit of reading on what jQuery and PHP are, and how the both work before starting to paste mixed up code together.
1

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);}

Comments

1

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>";
}

1 Comment

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);}

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.