javascript 提交表格给自己

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7381897/
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-25 23:55:52  来源:igfitidea点击:

submit form to self

phpjavascripthtml

提问by max_

At the moment, I have a form with an input (text) and a button to submit the form.

目前,我有一个带有输入(文本)的表单和一个用于提交表单的按钮。

The form is posted to 'submit.php'. I would like the form posted to same page as the form.

表单被发布到'submit.php'。我希望将表格发布到与表格相同的页面。

How can I do this?

我怎样才能做到这一点?

<form action="submit.php" method="POST">
    <input type="text" class="emailField" placeholder="email address" name="email" />
    <button type="submit">
        <div class="submit" />
    </button>
</form>

submit.php

提交.php

<?php
    mysql_connect('localhost', '', '') or die('unable to connect');
    mysql_select_db('') or die('unable to select db');
    $query = mysql_query("");
    if (mysql_num_rows($query) > 0) {
        exit("You have already subscribed!");
    }
    else {
        mysql_query("") or die(mysql_error());
        exit("You have successfully subscribed!");
    }

?>

回答by Treffynnon

I think you have a couple of options here.

我认为你在这里有几个选择。

Submit the form onto itself

将表单提交到自身

<?php
    if('POST' == $_SERVER['REQUEST_METHOD']) {
        // process the form here
    }
?>
<form action="" method="POST">
    <input type="text" class="emailField" placeholder="email address" name="email" />
    <button type="submit">
        <div class="submit" />
    </button>
</form>

Redirect back to the form

重定向回表单

Add something like this in submit.phpwhere you want to show the form.

submit.php要显示表单的位置添加类似的内容。

<?php
    header('Location: http://example.org/form.php');
    exit;
?>