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

发布于 2024-11-16 07:02:25 字数 499 浏览 0 评论 0原文

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

为什么这在我的流程页面上不起作用?它没有回显任何内容: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;
?>

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?

Why isnt this working on my process page? It doesnt echo anything :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;
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断舍离 2024-11-23 07:02:25

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

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

关于表单检查,可以保存用户输入,如果有他们的数据无效,您可以回显旧数据并再次显示表单。

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

You can check in the same file if the submit button is pressed. (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 ;)

Oo萌小芽oO 2024-11-23 07:02:25

是的,这是可能的。正如 Pekka 所建议的,实际上并不建议这样做。但这是如何做到的。

只需使用 isset 方法来检查表单是否已发布。因此,假设您的表单中有以下输入:

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

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

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

希望这有帮助;)

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

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

<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 if(isset($_POST["age"])) { 
    //do processing
} ?>

Hope this helps ;)

安人多梦 2024-11-23 07:02:25

可以让同一个脚本处理表单。

如果您想执行此操作,只需将操作留空即可。

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

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.

吻安 2024-11-23 07:02:25

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

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

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

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

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

It sounds like you are describing AJAX. Example: 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:

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

满意归宿 2024-11-23 07:02:25
<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";
       }
?>
<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";
       }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文