How to make a "Submit" in javascript?

Asked

Viewed 9,781 times

2

I am making a very simple game in html and javascript, the idea is that the user put the result of each operation, and if it is right, appear an Alert for it. I wanted to use table because I want to exercise this question. The program is almost ready, but I can’t get the user to give a "Ubmit" so I can check if it’s right. I even tried to use a Ubmit there, but I ended up doing a trick that did not work. I googled and found nothing related. From now on... J.G..

function olosko() {
  var x = document.getElementById("tabela").rows[0].cells[4];
  var y = document.getElementById("tabela").rows[1].cells[4];
  var z = document.getElementById("tabela").rows[2].cells[4];
  var r = document.getElementById("tabela").rows[3].cells[4];
  x.innerHTML = "<input type='text'>";
  y.innerHTML = "<input type='text'>";
  z.innerHTML = "<input type='text'>";
  r.innerHTML = "<input type='text'>";
  var a = document.getElementById("tabela").rows[0].cells[0];
  var b = document.getElementById("tabela").rows[0].cells[2];
  var c = document.getElementById("tabela").rows[1].cells[0];
  var d = document.getElementById("tabela").rows[1].cells[2];
  var e = document.getElementById("tabela").rows[2].cells[0];
  var f = document.getElementById("tabela").rows[2].cells[2];
  var g = document.getElementById("tabela").rows[3].cells[0];
  var h = document.getElementById("tabela").rows[3].cells[2];

  a.innerHTML = parseInt(Math.random()*10);
  b.innerHTML = parseInt(Math.random()*10);
  c.innerHTML = parseInt(Math.random()*10);
  d.innerHTML = parseInt(Math.random()*10);
  e.innerHTML = parseInt(Math.random()*10);
  f.innerHTML = parseInt(Math.random()*10);
  g.innerHTML = parseInt(Math.random()*10);
  h.innerHTML = parseInt(Math.random()*10);

  if(a+b==x && c-d==y && e/f==z && g*h==r){
    alert("VC GANHOU!");
  }
}
<button onclick="olosko()">ESTOU PRONTO!</button>
<table id="tabela" border=1 width=400 height=400>
  <tr>
    <td></td>
    <td>+</td>
    <td></td>
    <td>=</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>-</td>
    <td></td>
    <td>=</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>รท</td>
    <td></td>
    <td>=</td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td>X</td>
    <td></td>
    <td>=</td>
    <td><div id="olosko()"></div></td>
  </tr>
</table>
<submit><button>ACABEI!</button></submit>

  • I don’t think it’s clear what this Ubmit is going to do, is it going to be a button ? or a message with a Ubmit ? when it will occur, after filling all fields ?

  • Well, this button will be for the user to click after filling the table. He will click this to check if the answers are correct, if they are, an 'Alert' saying this, otherwise an Alert will also show up saying it is wrong.

  • to submit data you need a form. The HTML you posted has no form.

3 answers

4

You can use pure Javascript to solve your problem by being very simple.

Example:

document.getElementById("myForm").submit();

Button:

<a href="#" onclick="document.getElementById('myForm').submit()">Submit Form</a>
  • Eduardo Binotto, could you see in my code if the condition is correct? Because even with this Ubmit you sent me, I can’t do it. I think my condition is wrong, so if you could look, I’d appreciate it!

  • 1

    Put a form outside your table, that is, put your table inside a shape so you can give the Submit

  • I have just done so, the result is the same. The condition is that it seems to be the problem. Because I think I would have to create a function so that variables receive the value placed by the user, but I don’t know how to do that. Sorry to bother you, it’s just I need it by midnight.

  • I may know why you want to give a Ubmit. Maybe we have another way out of your problem.

  • Actually I don’t want Submit specifically. I just want the program to tell whether the user got it right or not. The only possible way I thought was in Submit. If there is another way, it would be very good!

  • You can do another function called corrigirRespostas() like your olosko() and call her on the button ACABEI! and within this function corrigirRespostas() you do your validation you are already doing at the end of the function olosko()

Show 2 more comments

1

I made some modifications to your code, I used pure Javascript like you.

var x;
var a, b;

function olosko() {
  a = document.getElementById("tabela").rows[0].cells[0];
  b = document.getElementById("tabela").rows[0].cells[2];
  x = document.getElementById("tabela").rows[0].cells[4];
  x.innerHTML = "<input id='val' type='text' />";

  a.innerHTML = parseInt(Math.random() * 10);
  b.innerHTML = parseInt(Math.random() * 10);
}

In this part of Javascript, I first declared the variables "a, b and x" as global to be used in two functions. Follow the Low lines for explanation:

a = document.getElementById("tabela").rows[0].cells[0];
a.innerHTML = parseInt(Math.random() * 10);

You used the same variable to create a column in your table, and to generate a random number, so far so good, but you failed to assign the generated random number to some variable, not doing so, the variable "a" will receive an object, which will be "Htmltablecellelement". See now what I did in the HTML part.

<button onclick="olosko()">ESTOU PRONTO!</button>
<table id="tabela" border=1 width=auto height=auto>
    <tr>
        <td id="a"></td>
        <td>+</td>
        <td id="b"></td>
        <td>=</td>
        <td><div id="olosko()"></div></td>
    </tr>
</table>
<button onclick="enviar()">ACABEI!</button>

I created an "id" for the columns that receive the random number, then by Javascript, pick the numbers to create a condition and an action on the "FINISHED!" , to call the send() function which will assign the random numbers to a variable.

function enviar() {
    a = parseInt(document.getElementById("a").innerHTML);
    b = parseInt(document.getElementById("b").innerHTML);
    x = document.getElementById("val").value;

    if (a + b == x) {
        alert("VC GANHOU!");
    } else {
        alert("VC PERDEU!");
    }
}

Take a good look at this line:

a = parseInt(document.getElementById("a").innerHTML);

It is here, that the random number will be assigned the variables, and will replace the objects that were previously assigned, the "parseint" had to be used because the random number was a "String" so it was necessary to make a conversion to Number, different from the assignment of the variable "x" just above. About the condition has not what to explain is the simplest.

I hope it helps, I started studying Javascript soon, any doubt I’m available.

0

With the use of Jquery is more reliable , so it runs smoothly in all browsers.

  $("#nome-do-meu-form").submit(function() {
                //alguma ação aqui ex :
                $("#btn").prop('disabled', true);
  });
  • Avoid using jQuery inadvertently. All Javascript is blocking and jQuery makes a number of unnecessary validations and considerations in this case.

  • i don’t think the way he did his script won’t run good in all browsers

  • It will not run because it is creating elements by string with innerHTML. If you use createElement() and getElementById() to select the form no mistake.

  • As I am a first-year IT student at CEFET (high school/technical), my knowledge is VERY limited. So I have difficulty applying the ideas I have programming, it happened once I have an idea of a program in C and qnd I realized, had almost 500 lines of code and did not work hehe. But thank you, this little game I made for my sister who has difficulty in math, so she could learn in a cool way. I haven’t had a chance to test your code yet because I’m on trial period, but as soon as I can, I’ll be!

  • good initiative @J.Gaidzinski

Browser other questions tagged

You are not signed in. Login or sign up in order to post.