Формировать данные с одной HTML-страницы на другую - PullRequest
0 голосов
/ 03 марта 2019

Мне нужно передать данные формы с одной HTML-страницы (test.html) на другую (test1.html) с помощью Java Script.PHP не может быть использован, так как мы еще этого не изучали и не имеем права это делать.Я могу передать и распечатать данные, но они печатаются на той же странице (test.html), тогда как я хочу напечатать их на test1.html.Можете ли вы помочь мне найти проблему?Я приложил и HTML-код, и Javascript.

<body>
    <form onsubmit="results();" action="test1.html" method="get">
        What is your name?
        <input id="f1" type="text" name="name" required /><br />
        What is your Father Name?
        <input id="f2" type="text" name="fname" required />
        Gender:
        <select name="gender">
            <option id="m" value="Male" selected>Male</option>
            <option id="f" value="Female">Female</option>
            <option id="o" value="Others">Other</option>
        </select>
        Date of Birth*: <br />
        <input type="date" id="b1" name="bday" required><br /><br />
        <input id="submit" type="submit" />
    </form>
    <script src="./response.js"></script>
</body>

Java Script:

function results() {
    var name = document.getElementById('f1').value;
    var fname = document.getElementById('f2').value;
    if (document.getElementById('m').selected) {
        gender = document.getElementById('m').value;
    }
    else if (document.getElementById('f').selected) {
        gender = document.getElementById('f').value;
    }
    else {
        gender = document.getElementById('o').value;
    }
    var dob = document.getElementById('b1').value;
    document.write("Here is the Summary of Your Results");
    document.write("Your Name Is: ");
    document.write(name + "<br/>");
    document.write("Your Father Name Is: ");
    document.write(fname + "<br/>");
    document.write("Your Gender Is: ");
    document.write(gender + "</br>");
    document.write("Your Date of Birth Is: ");
    document.write(dob + "</br>");

1 Ответ

0 голосов
/ 03 марта 2019

Вы должны удалить событие onsubmit из формы, и в файле html1.html вы можете использовать URLSearchParams, что просто для получения параметров запроса из URL

const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...