Joshua Feliciano Joshua Feliciano - 7 months ago 10
Javascript Question

Updating 'credits' using PHP and JavaScript

So I'm working on a website that involves credits. I've been researching the Internet for about 30 minutes now, trying to figure a solution for updating credits. By updating credits I mean this.

The webpage loads credits from the MySQL database, the player then uses a portion of some credits (Keep in mind I have subtracted them once used), but they don't update when subtracted.

So I used a JavaScript method that sets the credits to a LI in HTML. I use PHP inside of the JavaScript code. HOWEVER, it retrieves an amount of credits, but it's not the update amount of credits. It's the previous or it stays frozen on the previous amount of credits.

So here is the code you'll need to know the most:

This supposedly 'updates' the LI using JavaScript and PHP:

function finalL(){
document.getElementById("updatecredits").innerHTML = "credits: " +
<?php

$db = mysqli_connect('localhost', 'root', 'iGenX772', 'userdata');

$query = mysqli_fetch_array(mysqli_query($db, "SELECT credits FROM users WHERE username='" . mysqli_real_escape_string($db, $_SESSION['user'] ['username']) . "'"));
echo $query['credits'];

mysqli_close($db);
?>;
}


First called when the PHP page loads:

<li><a id="updatecredits">Credits:
<?php

$db = mysqli_connect('localhost', 'root', 'iGenX772', 'userdata');

$query = mysqli_fetch_array(mysqli_query($db, "SELECT credits FROM users WHERE username='" . mysqli_real_escape_string($db, $_SESSION['user']['username']) . "'"));
echo $query['credits'];

mysqli_close($db);

?></a></li>


I activate the function through another part which is probably unnecessary for any use for this issue. BUT I am subtracting the credits BEFORE this function is called.

I use the same method in the function as I do in the List. If anyone has a solution to this issue, I would love it!

~mrgreen33gamer | Joshua

EXTRA:

<div id="logreg">
<ul>
<li><a>Welcome, <?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?></a></li>
<li><a><span id="updatecredits">Credits:
<?php

$db = mysqli_connect('localhost', 'root', 'iGenX772', 'userdata');

$query = mysqli_fetch_array(mysqli_query($db, "SELECT credits FROM users WHERE username='" . mysqli_real_escape_string($db, $_SESSION['user']['username']) . "'"));
echo $query['credits'];

mysqli_close($db);

?></span></a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>

Answer

You should call the PHP using Ajax and put the PHP code into a separate file. In this case I assume the PHP code is in loadCredits.php

Javascript

<script type="text/javascript">
function finalL(){          
  xhttp= new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("updatecredits").innerHTML = "Credits: " + xhttp.responseText;
    }
  };
  xhttp.open("GET", "loadCredits.php?t=" + Math.random(), true);
  xhttp.send();
}
</script>

PHP

<?php
echo (5);
?>

HTML

<ul>
<li><a>Welcome, </a></li>
<li><a><span id="updatecredits" onClick="finalL()">Credits:
</span></a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
Comments