PHP Form with process on same page?

2020-07-18 02:37发布

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;
?>

标签: php forms
5条回答
▲ chillily
2楼-- · 2020-07-18 03:13
<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";
       }
?>
查看更多
迷人小祖宗
3楼-- · 2020-07-18 03:20

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 ;)

查看更多
The star\"
4楼-- · 2020-07-18 03:20

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 ;)

查看更多
ゆ 、 Hurt°
5楼-- · 2020-07-18 03:23

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.

查看更多
\"骚年 ilove
6楼-- · 2020-07-18 03:23

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/

查看更多
登录 后发表回答