Jump to content

problematic isset($_POST['submit'])...


Eggzorcist

Recommended Posts

I'm not sure why this code isn't working... I've done this many times before and this time it isn't working at all...

 

Here's the code I'm using:

<?php

if(isset($_POST['submit'])){
if(filter_var($_POST['value1'], FILTER_VALIDATE_EMAIL)){
	echo "Value is a valid email address.";
	} else {
	echo "Value is NOT a valid email address.";	
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<input name="value1" type="text" id="value1" size="40" />
<input id="submit" name="submit" value="Validate Value" type="button" />
</form>
</body>
</html>

 

I'm wondering whether anything has changed, but I don't see how this couldn't work.

 

Thanks for any additional information!

 

Link to comment
https://forums.phpfreaks.com/topic/222652-problematic-isset_postsubmit/
Share on other sites

1. Don't use isset($_POST['submit']) to check if a form has been submitted/posted, use this

 

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
   //submitted
}

 

Why? Because if you press the enter key on a field to submit a form on some versions of Internet Explorer and other browsers, the submit button does not get sent as a posted variable, so your script would not detect the form was posted.

 

2. Don't use action="<?php echo $_SERVER['PHP_SELF'] ?>" on a form because it is vulnerable to XSS attacks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.