Frage

I have tried for a very long time now to figure out, why this form is not working. It is not even displaying the error message if you don't type anything.

I hope someone here can point out the problem.

The form is XHTML Strict validated.

<?php

$err = array();

if (isset($_POST['submit'])) {

/* Do the validation */
$uploader_name = $_POST['uploader_name'];
$uploader_mail = $_POST['uploader_mail'];

if (empty($uploader_name)) {
$err[] = "Name is empty.";
}

if (empty($uploader_mail)) {
$err[] = "Mail is empty.";
}

/* If everything is ok, proceed */
if (empty($err)) {

//Do MySQL insert

}

}


echo "<h1>Submit</h1>";

if(!empty($err)) {
echo "<span style='color:red;'>";
foreach ($err as $e) {echo "* $e<br />"; }
echo "</span><br />";
}

echo "
<div>
<form action='' method='post' enctype='text/plain'>
<fieldset>
<legend>Your details</legend>
<label for='uploader_name'>Your name / nickname</label><br />
<input type='text' id='uploader_name' value='$uploader_name' /><br /><br />

<label for='uploader_mail'>Your mail (will not be visible)</label><br />
<input type='text' id='uploader_mail' value='$uploader_mail' /><br /><br />
</fieldset>

<p><input type='submit' id='submit' value='Submit' /></p>
</form>
</div>
";

?>
War es hilfreich?

Lösung

Fields are sent to server using name atr, not id. Add (or replace) ids with names, e.g.:

<input type='submit' name='submit' value='Submit' />

will produce $_POST['submit'] == 'Submit'

UPD: add, not replace. Values are sent via name, but on the other hand <label />'s are connected with form elements using id's.

UPD2: And remove enctype attr from <form>.

Andere Tipps

I recomment not to use empty, but isset. Empty accepts to much things as empty. Your check should be like this:

if (isset($_POST['foo']) || $_POST['foo'] !== '') {
    $errors[] = 'You need to fill in the foo input';
}

Some other tips:

  • use single quotes in PHP and double in HTML
  • keep quotes out of the string, with the concatenation operator

An example:

<?php
if (isset($_POST['form_send'])) {
    /* all validation stuff */
}
?>
<form action="post">
  <!-- ... -->
  <input type="text" value="<?php echo $uploader_mail ?>"><br />
  <!-- ... -->
</form>

Or

<?php
$name = 'World';
// not...
$hello = "Hello World";
// ...but
$hello = 'Hello '.$name;

And at least, to answer your question. PHP looks for the name attribute, not the id attribute.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top