简体   繁体   English

window.location.href 不适用于表单提交按钮

[英]window.location.href doesn't work with a form submit button

I'm trying to create a voting compass where I'm attempting to submit a form and insert the answers to a database, and then take the user to a results page.我正在尝试创建一个投票指南针,我试图在其中提交表单并将答案插入数据库,然后将用户带到结果页面。 When I have a submit button, I'm able to successfully store the answers but the page just refreshes rather than taking them to the results page.当我有一个提交按钮时,我能够成功存储答案,但页面只是刷新而不是将它们带到结果页面。

However, if I change the submit button to the following:但是,如果我将提交按钮更改为以下内容:

<img src="../pics/process.png" style="border:none; " onclick="Sort();">

I can successfully take the user to the results page, but the answers to the form are not stored to my database.我可以成功地将用户带到结果页面,但表单的答案没有存储到我的数据库中。

I'm stuck and have no idea how to approach this, any help would be appreciated.我被卡住了,不知道如何解决这个问题,任何帮助将不胜感激。

Here's the code for generating the results这是生成结果的代码

`<script type="text/javascript">
    function Sort() {
        //australia
        var party1= 0;
        var party2= 0;

        if (compass.q1[0].checked == true) {
            party1 += 1;
        } else if (compass.q1[1].checked == true) {
            party2 += 1;
        } else {
            alert("Please answer question 1.");
            return;
        }

        if (compass.q2[0].checked == true) {

            party1 += 1;
        } else if (compass.q2[1].checked == true) {
            party2 += 1;
        } else {
            alert("Please answer question 2.");
            return;
        }

        window.location.href = "./australiacompassresult.php?" + party1+ "&" + party2;
    } 

` `

Here is my form header and submit button when I can submit the form successfully but fail to load the results page这是我的表单 header 和提交按钮,当我可以成功提交表单但无法加载结果页面时

<form id="compassform" name="compass" method="post">
<input type="submit" value="Submit">

Here is the php code to insert the data from the form to my database <?php这是 php 代码,用于将表单中的数据插入我的数据库<?php

    $link = mysqli_connect("localhost","username","password", "database");

        if (mysqli_connect_error()) {
    
            die ("There was an error connecting to the database");
    
        }    
            $query = "INSERT INTO `users` (`q1`, `q2`, `q3`, `q4`, `q5`, `q6`, `q7`, `q8`) VALUES ('".mysqli_real_escape_string($link, $_POST['q1'])."', '".mysqli_real_escape_string($link, $_POST['q2'])."','".mysqli_real_escape_string($link, $_POST['q3'])."','".mysqli_real_escape_string($link, $_POST['q4'])."','".mysqli_real_escape_string($link, $_POST['q5'])."','".mysqli_real_escape_string($link, $_POST['q7'])."','".mysqli_real_escape_string($link, $_POST['q7'])."','".mysqli_real_escape_string($link, $_POST['q8'])."')";
            
            if (mysqli_query($link, $query)) {
                
                echo "";
            }

?> ?>

First, attach a submit event listener to the form.首先,将submit事件侦听器附加到表单。 After this call retrieve the event object and call the preventDefault() method to prevent the default behavior of the form submission.在此调用之后检索event object 并调用preventDefault()方法以防止表单提交的默认行为。 After this call the window.location.href to guide the user to the relevant page.之后调用window.location.href引导用户到相关页面。

eg:例如:

document.getElementById("form").addEventListener("submit",(e) => {
        e.preventDefault()
        window.location.href = "./australiacompassresult.php?" + party1+ "&" + party2;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM