Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
How to pass Form value from one function to other function and display

I have three file index.html,success.html and index.js and I want to validate form than redirect accordingly and prind form data on success page? anable to access variable from one function to other As code my code here from

function check() {
  x = document.forms.myform.fname.value;
  if (x == "") {
    alert("First Name should be filled out!");
    return false;
  } else {
    alert("Login Successful");
    window.location.href = "success.html";
    return false;
  }
}
// onload name function from success.html
function name() {
  document.getElementById('fn').innerHTML = x;
}
<body>
  <h1>Welcome to Form Validation</h1>
  <form method="post" action="success.html" name="myform" onsubmit="return(check())">
    <label>First Name</label>
    <input type="text" placeholder="First Name" name="fname">
    <button type="submit">Submit</button>
  </form>
  <script src="./index.js"></script>
</body>

success.html

function check() {
  x = document.forms.myform.fname.value;
  if (x == "") {
    alert("First Name should be filled out!");
    return false;
  } else {
    alert("Login Successful");
    window.location.href = "success.html";
    return false;
  }
}
// onload name function from success.html
function name() {
  document.getElementById('fn').innerHTML = x;
}
<head>
  <script src="./index.js"></script> 
</head>
<body onload="name()">
  <h1>Your form is submitted successfull.</h1>
  <p>Thanks</p>
  <div id="fn">Name Here</div>
  <hr width="50%">
</body>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The easiest way to do that without having a server side component is to utilize GET instead of POST in your form. Also, take away the window.location line - you want the form to submit naturally

function check() {
    if (document.forms.myform.fname.value == "") {
        alert("First Name should be filled out!");
        return false;
    } 
   return true;
}

<form method="get" action="success.html" name="myform" onsubmit="return(check())">
        <label>First Name</label>
        <input type="text" placeholder="First Name" name="fname">
        <button type="submit">Submit</button>
    </form>

And on the receiving page access the page URL with window.location.href...

var url_string = "http://www.example.com/success.html?fname=John"; //window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("fname");
console.log(c);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda