I have the below php page, which is a holding page with a contact form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<!--<link rel="icon" href="../../favicon.ico">-->
<title>Coming Soon</title>
<!-- Bootstrap -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<link href="assets/css/bootstrap-theme.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<!-- siimple style -->
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>TEST</h1>
<h2 class="subtitle">We're working hard to improve our website and we'll be ready to launch soon
<h2 class="subtitle">Feel free to contact us below with any enquiries</h2>
<p>
<?php
$name = $_POST["contactname"];
?>
<form class="Contact" method="post" action="">
<label class="alignleft">Name</label>
<input type="text" name="contactname" id="contactname" placeholder="Enter Name">
<label class="alignleft">Email</label>
<input name="Email" placeholder="Email Address">
<label class="alignleft">Enquiry</label>
<textarea name="Enquiry" placeholder="Your enquiry"></textarea><br>
<input id="submit" name="submit" type="submit" value="Submit!" class="btn btn-theme">
</form>
<div class="social">
<a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a>
<a href="#"><i class="fa fa-twitter" aria-hidden="true"></i></a>
<!--<a href="#"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
<a href="#"><i class="fa fa-linkedin" aria-hidden="true"></i></a>-->
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
Within the php section I am trying to read the values of the input boxes, however even with only
$_POST["contactname"];
the website returns a 500 Internal Server Error. If i remove that line and replace with a simple:
echo "Test";
Then the site works and displays “Test”
If there something I am missing with the assigning of the variable from the input box?
4
Answers
You should validate it first
Firstable , you need to check if $_POST is defined , and not empty :
Second , i think you need to configure you’r server to show errors and not to redirect you to 500 error:
Just modify your php.ini with this line:
You should make sure the key of the POST array is set and then validate your values first before you show them, and be sure to handle errors and invalid values!
Also enable PHP error output which will greatly help you debug issues like this:
And
display_errors = on
in your php.ini to make PHP show parse errors.I would recommend using a second file to process the form instead of having it all on one page. Not only does it make your code look cleaner but I find it can also help with debugging. So it will look something like this:
index.php:
form_process.php:
I would also follow the recommendations from other users about displaying PHP errors.