PHP 表单与同一页面上的进程?

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

PHP Form with process on same page?

phpforms

提问by Ahmed Deloitte

I've created a form on my page and from the tutorials im following it says I have to have a second page with all the php processing in, is it not possible to keep the php on the same page and when the user hits submit the form is sent?

我已经在我的页面上创建了一个表单,并且从我遵循的教程中说我必须有一个包含所有 php 处理的第二页,是否不可能将 php 保留在同一页面上,并且当用户点击提交时表格发送?

Why isnt this working on my process page? It doesnt echo anything :S

为什么这在我的流程页面上不起作用?它没有回显任何内容:S

<?php 
$kop = mysql_real_escape_string($_POST['severity']);
$loc = mysql_real_escape_string($_POST['location']};
$summary = mysql_real_escape_string($_POST['summary']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);

echo $kop;
echo $loc; 
echo $summary; 
echo $name; 
echo $email;
?>

回答by Wouter van Tilburg

You can check in the same file if the submit button is pressed. (http://pastebin.com/8FC1fFaf)

如果按下提交按钮,您可以签入同一个文件。( http://pastebin.com/8FC1fFaf)

if (isset($_POST['submit'])) 
{ 
    // Process Form
}
else
{
    // Show Form
}

Regarding form checking, you can save the user input, and if one of their data is unvalid you can echo the old data back and show the form again.

关于表单检查,您可以保存用户输入,如果其中一个数据无效,您可以回显旧数据并再次显示表单。

edit: As PeeHaa stated, you need to leave the action blank though ;)

编辑:正如 PeeHaa 所说,您需要将操作留空;)

回答by Gabriel Spiteri

Yes it is possible. As adviced by Pekka, it's not really suggested to do so. But here is how it can be done.

对的,这是可能的。正如 Pekka 所建议的那样,并不真正建议这样做。但这是如何做到的。

Simply using the isset method to check if the form has been posted. So say in your form you have the follwing input:

只需使用isset方法检查表单是否已发布。所以在你的表格中说你有以下输入:

<input type="text" name="age" />

At the top of your php script you can use the following to know if the form has been submitted and thus process it.

在 php 脚本的顶部,您可以使用以下内容来了解​​表单是否已提交并进行处理。

<?php if(isset($_POST["age"])) { 
    //do processing
} ?>

Hope this helps ;)

希望这可以帮助 ;)

回答by Aaron Newton

is it not possible to keep the php on the same page and when the user hits submit the form is sent?

是否无法将 php 保持在同一页面上,并且当用户点击提交时发送表单?

It sounds like you are describing AJAX. Example: http://www.elated.com/res/File/articles/development/javascript/jquery/slick-ajax-contact-form-jquery-php/

听起来您是在描述 AJAX。示例:http: //www.elated.com/res/File/articles/development/javascript/jquery/slick-ajax-contact-form-jquery-php/

This is using the jQuery framework - there are a number of frameworks for doing this (such as YUI) which could do this equally as well. You could write this yourself to learn how it all works, but IHMO this would be reinventing the wheel. Here is a decent tutorial on creating AJAX forms with jQuery:

这是使用 jQuery 框架 - 有许多框架可以做到这一点(例如 YUI),它们也可以同样做到这一点。你可以自己写这个来了解它是如何工作的,但 IHMO 这将是重新发明轮子。这是一个关于使用 jQuery 创建 AJAX 表单的不错的教程:

http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/

http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/

回答by PeeHaa

It is possible to let the same script process the form.

可以让相同的脚本处理表单。

If you want to do this just leave the action blank.

如果您想这样做,只需将操作留空即可。

However If you want to process the form without the page being reloaded you have to use Javascript.

但是,如果您想在不重新加载页面的情况下处理表单,则必须使用 Javascript。

回答by beta

<form name="myfrom" action="" method="post">
 Username: <input type="text" name="user" id="username" />
 <input type="submit" name="submit_form" value="Submit" />
</form> 

<?php if($_POST['submit_form'] == "Submit"){
       echo "do something";
       }
?>