Output command or pass output in php

0

Hello.

I want to return the success message if a name is entered in the array. And I want to display the names in that array.

I do not want to use alert (). Document.write does not work well on functions, especially with events or timing functions. I have already tested timers and events, but other things appear on the screen and not what I want. Already with alert () it works. Is there another output command?

Or maybe, I can use php to display the success message and array names. How do I move from javascript to php?

Here are two javascript functions for this.

<script>
    j = 0;
    i = 0;

    nomes = new Array();

    function inserirNome() {
        nome = prompt("informe o nome");

        nomes[i] = nome;

        alert("Nome inserido");
        i++;
    }

    function verNomes() {

        while (j < i) {
            alert(nomes[j]);

            j++
        }

        if (i == 0) {
            alert("Não tem nome inserido");
        }

    }
</script>

<a onclick="inserirNome()"> Inserir um nome</a>

<a onclick="verNomes()"> Ver os nomes inseridos</a>

    
asked by anonymous 16.11.2015 / 15:59

1 answer

0

Try this, it's also a good way to print returns on the document body.

var nomes = [];
var div = document.getElementById('mensagem');

function inserirNome() {
  var nome = prompt('Insira o nome');
  if (nome) {
    if (nomes.push(nome)) {
      div.innerHTML = ('inserido: ' + nome);
    }
  }
}

function verNomes() {
  var nick = '',
    x = 0;
  nomes.forEach(function(nome) {
    nick += 'Nome' + x + ':' + nome + "<br/>";
    x++;
  });
  if (nick == '') {
    div.innerHTML = 'Nenhum nome encontrado';
    return;
  }
  div.innerHTML = nick;
}
<div id="mensagem"></div>
<a href="#" onclick="inserirNome()"> Inserir um nome</a>
<a href="#" onclick="verNomes()"> Ver os nomes inseridos</a>
    
16.11.2015 / 16:53