javascript 提交后不刷新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25831642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:06:43  来源:igfitidea点击:

Don't refresh page after submit

javascriptphpforms

提问by Cricco95

I have a site http://gamepoll.netand I want that when I click Create Poll, the page doesn't refresh. This is the HTML code:

我有一个网站http://gamepoll.net,我希望当我单击“创建投票”时,页面不会刷新。这是 HTML 代码:

<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>League of Legends Straw Poll</title> 
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <h1 class="text-center">Game Straw Poll</h1>
            </div>
        </div>
        <br>
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <form name="submitForm" id="submitForm" action="" method="post">
                            <div class="row">   
                                <div class="col-md-12">
                                    <select class="form-control" id="theme" name="game" onchange="ChangeBackground();">
                                        <option selected disabled>Select your Game...</option>
                                        <option value="League_of_Legends">League of Legends</option>
                                        <option value="Heartstone">Hearthstone</option>
                                    </select>
                                </div>
                            </div>
                            <br>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="input-group">
                                        <input type="text" class="form-control" name="question" id="question" placeholder="Start typing your question...">
                                        <span class="input-group-addon">
                                            <i class="glyphicon glyphicon-question-sign"></i>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <br>
                            <div class="row">
                                <div class="form-group form-group-options col-md-12 col-sm-12 col-xs-12">
                                    <div class="input-group input-group-option col-md-12 col-sm-12 col-xs-12">
                                        <input type="text" name="option[]" id="option" class="form-control" placeholder="Options...">
                                        <span class="input-group-addon input-group-addon-remove">
                                            <span class="glyphicon glyphicon-remove"></span>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="checkbox">
                                        <label>
                                            <input type="checkbox" id="choice" name="choice" value="Yes">Allow multiple choice
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <button type="submit|button" class="btn btn-primary btn-lg pull-left" name="submit_button" id="submit_button" onclick="SubmitForm();" data-toggle="modal" data-target="#myModal">Create Poll</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Poll created</h4>
                </div>
                <div class="modal-body">
                    <p>Share it: <a href="http://gamepoll.net/<?php echo $rand_value; ?>">http://gamepoll.net/<?php echo $rand_value; ?></a></p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
                    <button type="button" class="btn btn-primary">Invia</button>
                </div>
            </div>
        </div>
    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.js"></script>
    <script type='text/javascript' src='js/addfield.js'></script>
    <script type='text/javascript' src='js/changebackground.js'></script>
    <script type='text/javascript' src='norefresh.js'></script>
</body>
</html>

JS Code

JS代码

function SubmitForm(e) {
e.preventDefault();
var game = $("#game").val();
var question = $("#question").val();
var option = $("#option").val();
$.post("submit.php", { game: game, question: question, option: option },
}

submit.php Code

提交.php代码

<?php
//Include configuration file
include('includes/config.php');

//Define variables
$question=$_POST['question'];
$game=$_POST['game'];
$option=$_POST['option'];
$choice=$_POST['choice'];

//Generate random number
$rand_value=rand();

//Create temporary folder
mkdir($rand_value);

//Copy page of Ask Poll
copy('page.php', $rand_value . '/page.php');
rename($rand_value . '/page.php', $rand_value . '/index.php');

//Add data into database
mysql_connect($db_host, $db_username, $db_password) or die ("Errore di connessione!");
mysql_select_db($db_name) or die ("Impossibile selezionare database!");
$sql1="CREATE TABLE `" . $rand_value . "` (Question VARCHAR(200), Options VARCHAR(200), Choice INT(11))";
mysql_query($sql1) or die ("Impossibile eseguire la query!");

//Count number of Options available
$count=count($option);

    for ($i=0; $i<($count-1); $i++)
    {
        ${$sql . $i}="INSERT INTO `" . $rand_value . "` (Question, Options, Choice) VALUES ('$question', '$option[$i]', '$choice')";
        mysql_query(${$sql . $i});
    }   
?>

All works perfectly but the only problem is that when i click Create Poll button, the browser redirect me to submit.php blank page

一切正常,但唯一的问题是,当我单击“创建投票”按钮时,浏览器将我重定向到 submit.php 空白页面

UPDATE

更新

This code doesn't work

此代码不起作用

function SubmitForm(e) {
e.preventDefault();
var game = $("#game").val();
var question = $("#question").val();
var option = $("#option").val();
$.post("submit.php", { game: game, question: question, option: option }
return false;
}

Can you write me an AJAX function as the last comment says?

你能像最后一条评论所说的那样给我写一个 AJAX 函数吗?

回答by Wezy

place return false;at the end of your SubmitFormfunction and loose the comma at the end of the last sentence

放置return false;在你的结尾SubmitForm功能,并在最后一句的结尾松逗号

function SubmitForm(e) {
  e.preventDefault();
  var game = $("#game").val();
  var question = $("#question").val();
  var option = $("#option").val();
  $.post("submit.php", { game: game, question: question, option: option }
  return false;
}

回答by DJ'

Either place return false;at the end of the function SubmitForm or change onclick="SubmitForm();"to onclick="SubmitForm(); return false;"

放在return false;函数 SubmitForm 的末尾或更改onclick="SubmitForm();"onclick="SubmitForm(); return false;"

回答by Pooya Behravesh

You can just add onsubmit="return false;" to your form:

你可以添加 onsubmit="return false;" 到您的表格:

<form onsubmit="return false;">
*****
****
</form>

回答by Cricco95

use Ajax function if you want that the page doesn't refresh on click the "Create Poll" button. It loads the part of the page without refresh the whole page.

如果您希望页面在单击“创建投票”按钮时不刷新,请使用 Ajax 功能。它加载页面的一部分而不刷新整个页面。