This code works fine in PHP 5.3, but I can't figure out what to do to make it working in PHP 5.5.9 on a LAMP server. I have tried searching around, but haven't found any solution. Any ideas of to fix it?
So; the users sees the from correctly, without the fact it always says the mail is sent, even befor they hit the submit button. Second, the mail that it should send in the end, will not be sent, nothing happens.
<body>
<?php
//Variabals and validastions
$firstnameErr = $secondnameErr = $surenameErr = $emailErr = $dateErr = $gateErr = $pnumErr = "";
$firstname = $secondname = $surename = $email = $date = $gate = $pnum = "";
$Err = "1";
$date = date("d/m/Y");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$firstnameErr = "Firstname is needed";
$Err = "2";
} else {
$firstname = test_input($_POST["firstname"]);
if (!preg_match("/^[a-åA-Å ]*$/",$firstname)) {
$firstnameErr = "Only letter and spaces";
$Err = "2";
}
}
if (empty($_POST["secondname"])) {
$secondname = " ";
} else {
$secondname = test_input($_POST["secondname"]);
if (!preg_match("/^[a-åA-Å ]*$/",$secondname)) {
$secondnameErr = "Only letter and spaces";
$Err = "2";
}
}
if (empty($_POST["surename"])) {
$surenameErr = "Surename is needed";
$Err = "2";
} else {
$surename = test_input($_POST["surename"]);
if (!preg_match("/^[a-åA-Å ]*$/",$surename)) {
$surenameErr = "Only letter and spaces";
$Err = "2";
}
}
if (empty($_POST["email"])) {
$emailErr = "E-postadresse is needed";
$Err = "2";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Not valid email";
$Err = "2";
}
}
if (empty ($_POST["date"])) {
$dateErr = "Fødselsdate is needed";
$Err = "2";
} else {
$date = test_input ($_POST["date"]);
}
if (empty($_POST["gate"])) {
$gateErr = "Gateadresse is needed";
$Err = "2";
} else {
$gate = test_input($_POST["gate"]);
if (!preg_match("/^[a-åA-Å0-9. ]*$/",$gate)) {
$gateErr = "Invalid characters, only letters, numbers and space";
$Err = "2";
}
}
if (empty($_POST["pnum"])) {
$pnumErr = "Post number is needed";
$Err = "2";
} else {
$pnum = test_input($_POST["pnum"]);
if (!preg_match("/^\d{4}$/",$pnum)) {
$pnumErr = "Invalid post number";
$Err = "2";
}
}
if ($Err == "1") {
$Err = "";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!--Form-->
<div>
<div>
<img src="https://wiki.piratpartiet.no/images/1/18/Ole.png" alt="uPir logo" width="400px">
<h1 style="text-align:center">Application form</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table style="width:100%">
<tr>
<td>
Firstname: <br>
<input name="firstname" type="text" value="<?php echo $firstname;?>">
<span class="error">* <br>
<?php echo $firstnameErr;?></span>
<br><br>
</td>
<td>
Secondname:<br>
<input name="secondname" type="text" value="<?php echo $secondname;?>">
<span class="error"><br>
<?php echo $secondnameErr;?></span>
<br><br>
</td>
</tr>
<tr>
<td>
Surename:<br>
<input name="surename" type="text" value="<?php echo $surename;?>">
<span class="error">* <br>
<?php echo $surenameErr;?></span>
<br><br>
</td>
</tr>
<tr>
<td>
E-mail:<br>
<input name="email" type="mail" value="<?php echo $email;?>">
<span class="error">* <br>
<?php echo $emailErr;?></span>
<br><br>
</td>
<td>
Brithdate (dd.mm.yyyy):<br>
<input name="date" type="date" min="1900-01-01" max="2015-12-31" value="<?php echo $date;?>">
<span class="error">* <br>
<?php echo $dateErr;?></span>
<br><br>
</td>
</tr>
<tr>
<td>
Adress:<br>
<input name="gate" type="text" value="<?php echo $gate;?>">
<span class="error">* <br>
<?php echo $gateErr;?></span>
<br><br>
</td>
<td>
Post number:<br>
<input name="pnum" type="number" value="<?php echo $pnum;?>">
<span class="error">* <br>
<?php echo $pnumErr;?></span>
<br><br>
</td>
</tr>
</table>
<?php
//Controll and mailsender
if (empty($Err["Err"])) {
$to = 'email@adress.com';
$subject = 'subject';
$message = "$firstname $secondname $surename ønsker å bli medlem.
E-post: $email
Fødselsdate: $date
Gateadresse: $gate
Postnummer: $pnum
Sendt: $date
--massages info here--";
mail($to, $subject, $message);
echo '<p class="sucsess">Messages sent</p>';
}
?>
<p style="text-align:center"><input type="submit" name="sumbit" value="Register" id="submit"></p>
</div>
</form>
</div>
</body>
The issue is that on 5.5.9 it dosn't send the info mail (to me) and the user gets up the info that the mail is sent before they have hit the sumbit button, and even if it's wrongly filed out.
The issue where in the last test if (empty($Err["Err"])) {
it should have been if (empty($Err)) {
.
Problem solved.
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
-> action=""
if (empty($Err["Err"])) {
-> $Err might still be "1" (no array) wich will throw a warningBut please tell us what goes wrong first! Turn on displaying errors/warnings at the beginning of the code:
ini_set('error_reporting', E_ALL);
ini_set("display_errors", "1");
My first guess would be that while your PHP 5.3 server is configured to send email, your PHP 5.5.9 is not. Simple test - call mail()
with hardcoded values.
even before they hit the submit button
I'm not sure what you mean by that bit, but maybe your doing something with AJAX. Look in your browser's console log to see all your HTTP requests and responses.