PDA

View Full Version : Problem using $_POST and $_GET



phoenixshade
03-07-2007, 05:17 AM
OK, I'm new to php, so this is probably an idiotic oversight, but I swear I've gone over this with a fine tooth comb and can't see what's wrong.

I have a form that submits data to the server with method="post". However, when I hit submit, the superglobal $_POST appears to be empty. I even tried changing to method="get" / $_GET, and not only did I still have the same problem, but the form data didn't even appear in the url!?!

Here's the code. The idea is that it reads the form.html file line-by-line. If form data has been submitted, it is supposed to add a value="value" attribute to any input element. It then displays that line.

register.php

<?php
$lines = file('form.html');

foreach($lines as $line) {
if ($_POST['action']=='sent') {
if (preg_match('/input id=".+?"/', $line, $match)) {
$inputID = substr($match[0],10,strlen($match[0])-11);
preg_match('/type=".+?"/', $line, $match);
$inputType = substr($match[0],6,strlen($match[0])-7);
$insertPoint = strpos($line,'type="');
$insertString = "value=\"$_POST[$inputID]\" ";
$line = substr($line,0,$insertPoint) . $insertString . substr($line,$insertPoint-strlen($line));
}
}
echo $line;
}
?>
form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Essential Inspirations - Register with www.essential-inspirations.com</title>
<style type="text/css" media="screen, print, projection">
* {
margin: 0;
padding: 0;
}
#wrap {
width: 80%;
margin: 0 auto;
border: solid 2px #530;
}
</style>
</head>
<body>
<div id="wrap">
<form id="register" action="register.php" method="post">
First Name: <input id="fname" type="text" /><br />
Last Name: <input id="lname" type="text" /><br />
Username: <input id="username" type="text" /><br />
Password: <input id="password" type="password" /><br />
Verify Password: <input id="pwverify" type="password" /><br />
e-Mail Address: <input id="email" type="text" /><br />
<input type="hidden" id="action" value="sent" />
<input type="submit" value="submit" /><br />
</form>
</div>
</body>
</html>
Thanks in advance for your help.

chump2877
03-07-2007, 05:24 AM
Instead of id, use name:


<form id="register" action="register.php" method="post">
First Name: <input name="fname" type="text" /><br />
Last Name: <input name="lname" type="text" /><br />
Username: <input name="username" type="text" /><br />
Password: <input name="password" type="password" /><br />
Verify Password: <input name="pwverify" type="password" /><br />
e-Mail Address: <input name="email" type="text" /><br />
<input type="hidden" name="action" value="sent" />
<input type="submit" value="submit" /><br />
</form>

phoenixshade
03-07-2007, 05:44 AM
That did it. Thanks for the quick reply.

I made an incorrect assumption that the name attribute is invalid in XHTML Strict. I guess I needed the reminder to check my assumptions!

_Aerospace_Eng_
03-07-2007, 09:09 AM
That did it. Thanks for the quick reply.

I made an incorrect assumption that the name attribute is invalid in XHTML Strict. I guess I needed the reminder to check my assumptions!

Its only invalid on some elements. The form element is one of them but for inputs its acceptable.