Redirecting to a page after submitting form in HTML

前端 未结 3 1714
轮回少年
轮回少年 2020-12-30 03:14

I\'m fairly new to coding in HTML. After hours of searching the internet for a way to do this, I failed and so I\'m here. I was setting up a CSRF Proof of concept page here,

相关标签:
3条回答
  • 2020-12-30 03:30

    For anyone else having the same problem, I figured it out myself.

        <html>
          <body>
            <form target="_blank" action="https://website.com/action.php" method="POST">
              <input type="hidden" name="fullname" value="Sam" />
              <input type="hidden" name="city" value="Dubai&#32;" />
              <input onclick="window.location.href = 'https://website.com/my-account';" type="submit" value="Submit request" />
            </form>
          </body>
        </html>

    All I had to do was add the target="_blank" attribute to inline on form to open the response in a new page and redirect the other page using onclick on the submit button.

    0 讨论(0)
  • 2020-12-30 03:30

    What you could do is, a validation of the values, for example:

    if the value of the input of fullanme is greater than some value length and if the value of the input of address is greater than some value length then redirect to a new page, otherwise shows an error for the input.

    // We access to the inputs by their id's
    let fullname = document.getElementById("fullname");
    let address = document.getElementById("address");
    
    // Error messages
    let errorElement = document.getElementById("name_error");
    let errorElementAddress = document.getElementById("address_error");
    
    // Form
    let contactForm = document.getElementById("form");
    
    // Event listener
    contactForm.addEventListener("submit", function (e) {
      let messageName = [];
      let messageAddress = [];
      
        if (fullname.value === "" || fullname.value === null) {
        messageName.push("* This field is required");
      }
    
      if (address.value === "" || address.value === null) {
        messageAddress.push("* This field is required");
      }
    
      // Statement to shows the errors
      if (messageName.length || messageAddress.length > 0) {
        e.preventDefault();
        errorElement.innerText = messageName;
        errorElementAddress.innerText = messageAddress;
      }
      
       // if the values length is filled and it's greater than 2 then redirect to this page
        if (
        (fullname.value.length > 2,
        address.value.length > 2)
      ) {
        e.preventDefault();
        window.location.assign("https://www.google.com");
      }
    
    });
    .error {
      color: #000;
    }
    
    .input-container {
      display: flex;
      flex-direction: column;
      margin: 1rem auto;
    }
    <html>
      <body>
        <form id="form" method="POST">
        <div class="input-container">
        <label>Full name:</label>
          <input type="text" id="fullname" name="fullname">
          <div class="error" id="name_error"></div>
          </div>
          
          <div class="input-container">
          <label>Address:</label>
          <input type="text" id="address" name="address">
          <div class="error" id="address_error"></div>
          </div>
          <button type="submit" id="submit_button" value="Submit request" >Submit</button>
        </form>
      </body>
    </html>

    0 讨论(0)
  • 2020-12-30 03:31

    You need to use the jQuery AJAX or XMLHttpRequest() for post the data to the server. After data posting you can redirect your page to another page by window.location.href.

    Example:

     var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          window.location.href = 'https://website.com/my-account';
        }
      };
      xhttp.open("POST", "demo_post.asp", true);
      xhttp.send();
    
    0 讨论(0)
提交回复
热议问题