1

I have the following javascript code:

game.bind('gameover', function(seconds) {
    setTimeout(function() {
      var rank = game.getRank(seconds);
      scorecard.find('.time').text(seconds + ' sec');
      scorecard.find('.rank').text(rank[0]);
      scorecard.find('.byline').text(rank[1]);
      ...more code

In the following div this data is displayed (so the js script is working):

<div class="inner">
  <h3>Nice job! Your time:</h3>
  <h1 class="wobble time"><!-- 15 seconds --></h1>
  <h2 class="rank"><!-- Friendly Mallard --></h2>
  <h3 class="byline"><!-- (That's pretty good) --></h3>
</div>

Now I want to put the results also in a form so I can post it to the a database (mysql). So I've added a form and put it in the same div:

<form id="form" action="updatescore.php" method="post">
  <input type="text" id="time" name="time" value="" />
  <input type="text" id="rank" name="rank" value="" />
  <input type="text" id="byline" name="byline" value="" />
</form>

Now in the JS I've copied the lines (e.g. scorecard.find('.time').text(seconds + ' sec');) and changed it to look for the id, so . becomes # and passed it in the js like:

scorecard.find('.time').text(seconds + ' sec');
scorecard.find('#time').text(seconds + ' sec');
etc

So this should work I think, but the form inputs stay empty. What am I doing wrong?

Kind regards,

Maurice

2 Answers 2

3

If you are just looking to move js variables into your database via updatescore.php you don't need to use a form at all! You can do it with AJAX.

  var scoreData = {
       time : time,
       rank : rank[0],
       byline : rank[1]
  };
  $.post('updatescore.php', scoreData); //done!
Sign up to request clarification or add additional context in comments.

Comments

2

Use val() instead of text() for setting the values

2 Comments

Thanks I'll try this if the solution from Fresheyeball doesn't work
This worked. Though the other solution also works, I still need a form since I also have php data to process

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.