简体   繁体   English

提交后留在页面上

[英]Staying on page after submitting

just a question -- 就一个问题 -

How do i actually submit a form and still stay on the same page whilst the script is being executed? 如何在脚本执行时实际提交表单并仍然保持在同一页面上?

I have a simple form that goes like this: 我有一个简单的形式,如下所示:

<form action="process.php" method="post" id="form">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" required="required" />
    <label for="email">Email</label>
    <input type="email" name="email" id="email" required="required" />
    <label for="subject">Subject</label>
    <input type="text" name="subject" id="subject" required="required" />
    <label for="message">Message</label>
    <input type="text" name="message" id="message" required="required" />
    <input type="submit" id="button" value="Submit"/>
</form>

But everytime i submit the form it just goes to process.php. 但每次我提交表格时,它都会转到process.php。 I don't want that, i want it to stay on the same page, maybe even have a jquery popup saying "thank you" or something. 我不希望这样,我希望它保持在同一页面,甚至可能有一个jquery弹出窗口说“谢谢”或其他东西。

How do i actually stay on the same page whilst the script is being run? 在脚本运行时,我如何实际停留在同一页面上?

  1. Put your form processing logic on the same page as the form 将表单处理逻辑放在与表单相同的页面上
  2. Use Ajax 使用Ajax
  3. Redirect back to that page when process.php is done processing the form 当process.php处理完表单时,重定向回到该页面

You have to use ajax in this case. 在这种情况下你必须使用ajax。

Basically, you need to post all data using ajax. 基本上,您需要使用ajax发布所有数据。 In server side, you will need to get all parameters using $_POST['name'] like you normally do in server scripting. 在服务器端,您需要使用$ _POST ['name']获取所有参数,就像通常在服务器脚本中一样。

FORM 形成

<form action="process.php" method="post" id="form">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" required="required" />
    <label for="email">Email</label>
    <input type="email" name="email" id="email" required="required" />
    <label for="subject">Subject</label>
    <input type="text" name="subject" id="subject" required="required" />
    <label for="message">Message</label>
    <input type="text" name="message" id="message" required="required" />
    <input type="button" id="button" value="Submit"/>
</form>

<div id="dialog-message" title="Thank You">
    <p>Your message here</p>
</div>

​

JQUERY JQUERY

$(document).ready(function(){

    $('#dialog-message').dialog({
        modal: true,
        autoOpen: false,
        buttons: {
            Ok: function() {
                $(this).dialog("close");
            }
        }
    });



    $('#button').on('click', function() {
        var name = $('#name').val();
        var email = $('#email').val();
        var subject = $('#subject').val();
        var message = $('#message').val();
        var dataString = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message;
        $.ajax({
            type: "POST",
            url: "process.php",
            data: dataString,
            success: function() {
                alert('');
                $('#dialog-message').dialog('open');
            }
        });
    });
});​

See the example here 请参阅此处的示例

Please note, dont forget to put jquery reference and also jquery ui reference 请注意,不要忘记把jquery引用和jquery ui引用

I think John Conde gave the best answer. 我认为John Conde给出了最好的答案。

I would either make it so the php file that displays the form also handles the $_POST or if you can't do it that way then do it in Ajax with a code similar to this: 我要么这样做,所以显示表单的php文件也处理$ _POST,或者你不能这样做,然后在Ajax中用类似的代码执行:

$("#form").bind("submit", function() { 

var nameTxt = $("#name").val();
var emailTxt = $("#email").val();
$.post('process.php', 
    {
        name: nameTxt,
        email: emailTxt
    }, 
    function(data) {
                if (data == 'ko')
                    alert('Could not submit...');
                else {
                    alert('Thank you for your message.');
                }
            });
return false;
}

What this does is that it "blocks" the regular submit of the form when you click the submit button, retrieve input values and sends them to the process.php script. 这样做是当它单击提交按钮时“阻止”表单的常规提交,检索输入值并将它们发送到process.php脚本。

It assumes that your "process.php" does an 它假设你的“process.php”做了

 echo "ko"; 

if there is an error. 如果有错误。 You can do some form cleanup after the form has been successfully sent by reseting the inputs: 通过重置输入成功发送表单后,您可以执行一些表单清理:

$("#name").val('');
$("#email").val('');

Assuming you're using jquery: 假设你正在使用jquery:

$(document).ready(function(){
  $('#form').submit(function(e){
    // do some ajax request to process.php here...

    // stop the form doing its default action of submitting directly to process.php
    e.preventDefault();
  });
});

try something like this 尝试这样的事情

      action="<?php print $_SERVER["PHP_SELF"]; ?>" (or) action=""

EDIT: 编辑:

         <?php
         if(isset($_POST['Submit'])){
             //do your validation or something here
             header("location:Process.php");

             }

I targeted an iframe to solve the problem, and it worked perfectly. 我以iframe为目标来解决问题,而且效果很好。

 <form name="contact" role="form" method="post" action="contact.php" target="my_iframe"> Feild 1: <input name="feild1" id="feild1" type="text" placeholder="Lorem ipsum dolor"><br/><br/> Feild 2: <input name="feild2" id="feild2" type="text" placeholder="Lorem ipsum dolor"><br/><br/> Feild 3: <textarea name="feild3" id="feild3" rows="5" type="text" placeholder="Lorem ipsum dolor, sit amet."></textarea><br/><br/> <input type="submit"> </form> <!-- target iframe to prevent redirection to 'contact.php' --> <iframe name="my_iframe" width="1" height="1" style="border:none"></iframe> 

Literally: 从字面上看:

<form action="process.php" method="post" id="form" target="_blank">
                                                   ^^^^^^^^^^^^^^^^

This will make the browser stay on the same page, even if the form is submitted. 这将使浏览器保持在同一页面上,即使表单已提交。

But you will be more likely looking for AJAX instead, some Introduction . 但是你会更有可能寻找AJAX, 一些简介

要做到这一点,你需要jquery ajax,请看这里$ .post

You can do something like this (with jQuery, untested) 你可以这样做(使用jQuery,未经测试)

$(function() {
    $("#form").submit(e) {
        // Prevent regular form submission
        e.preventDefault();

        // Submit the form via AJAX
        var $form = $("#form");
        $.post(
            $form.attr("action"),
            $form.serialize(),
            function() { alert("Done!"); }
        );
   }
}

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

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