Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda