How do I open a new PHP page with posted data from a javascript file

I have updated my form:

<form method = "post" name= "account" action= "validated.php" target="_blank">
...
<submit value= "submit" onclick= "validate()" >Submit</submit>

It validates correctly from the validate.js file but still does not open a new page for validated.php

In validated.php just to start:

<p>Welcome <?php echo $_GET["fname"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?> and your birthday is: <?php echo $_GET["bdate"]; ?>
</p>

I think the issue is in the html. Any ideas?

20 thoughts on “How do I open a new PHP page with posted data from a javascript file”

  1. Your code makes no sense at all.

    I am answering the question that had XMLHttpRequest in it

    1. There exists no tag called submit
    2. You have $_GET but you POST your xmlhttprequest

    Since you want to open a new page in PHP, just have this (default is GET)

    <form name="account" action= "validated.php">
      ...
      <input type="submit" value="Submit" />
    </form>
    

    BUT if you really want to ajax, you need to change your $_GET to $_POST and do something like this

    window.addEventListener("load",function() {
      document.getElementById("account").addEventListener("submit",function(e) {
        e.preventDefault();
        let formData = new FormData();
        formData.append("fname", this.fname.value);
        formData.append("email", this.email.value); // assuming field names here
        formData.append("bdate",this.bdate.value);
        console.log(formData);
        const xhttp = new XMLHttpRequest();
        xhttp.onload=function(response) { console.log(response) }; // or some other process
        xhttp.open("POST", 'validated.php', true);
        xhttp.send(formData);
      });
    });
    

    using a form with an ID

    <form id="account">
    ...
      <input type="submit" value="Submit" />
    </form>
    
    Reply

Leave a Comment