php


Form submission is not displaying or executing


Recently I built a form using HTML, CSS, and JavaScript. I then entered the PHP required to send the data that is inputted in the form, to my database. After I finished, what I thought was necessary for it to work, I ended up with a blank page when I executed the code. The form and everything disappeared. This is my PHP code:
<?php
require("$_SERVER[DOCUMENT_ROOT]/connect.php");
$email = $username = $type = $question = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(!empty($_POST))
{
if(isset($_POST["email"], $_POST["username"], $_POST["type"], $_POST["question"])
{
$email = test_input($_POST["email"]);
$username = test_input($_POST["username"]);
$type = test_input($_POST["type"]);
$question = test_input($_POST["question"]);
$premium = ($_POST["premium"]);
$member = $_POST["member"];
$terms = $_POST["terms"];
if ($member != "NO")
{
$member = "YES";
}
}
if(!empty($email) && !empty($username) && !empty($type) && !empty($question) && !empty($terms))
{
$insert = $db->prepare("INSERT INTO QuestionSubmission (Email, Username, Type, Question, Member, Premium, Date) VALUES (?, ?, ?, ?, ?, ?, NOW())");
$insert->bind_param("ssssss", $email, $username, $type, $question, $member, $premium);
if($insert->execute())
{
header("Location: landing.php");
die();
}
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
The "member" part is a check box where the user can optionally select to become a member. It is unchecked initially, and i've set a value of "NO" for that. Also, there is a hidden check box that is already checked with a value of NO...this is the "premium" check box. Lastly, there is a check box for agreeing to the terms. This is initially unchecked, but the user has to check it so it won't be empty and for the form to process.
Could you please explain what I have to do in order for my form to work properly?
Also, the " require("$_SERVER[DOCUMENT_ROOT]/connect.php"); " part is where my connection to the database code is located. This code and the form code is located in the same page.
Replace require("$_SERVER[DOCUMENT_ROOT]/connect.php"); with require_once $_SERVER[DOCUMENT_ROOT]."/connect.php");, you're not using a variable - the $_SERVER can't be used like you're using it. This is why you're getting a "blank page of death", if you check your error_log you'd see that it has a syntax-error because of it.
Furthermore, you're checking if(!empty($_POST)) - this could really be any POST-form. You should remove this code
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(!empty($_POST))
{
as you're checking if the inputs are set just below the above code.
As a final note, when you're using die();, you should use exit; instead. They really do the same thing, but usage of die() is more for error-checking, like "Script can't run any further - die now!", while exit; is more like "I would like to stop the script from running now, I'm done - thanks!".
Normally it’s easier to include the processing code inside the form page. That way, if you encounter errors, you can:
allow the code to fall through to the form again
persist old values by using the value="…" attribute
Roughly it looks like this:
<?php
$email=$username=''; // etc
if(isset($_POST['submit'])) { // or whatever you name the submit button
// validate data
if(!$errors) {
// process
// new location
}
// retain values from above
}
?>
<form>
<label>Name: <input type="text" name="username" value="<?php print $username; ?>"></label>
<label>Email: <input type="text" name="email" value="<?php print $email; ?>"></label>
<!-- etc -->
<button type="submit" name="submit">Send Message</button>
</form>
For ease and management, you can put the PHP code above into a separate file to be included.
The problem your sample above is that although you do redirect to a new page on success, you don’t go anywhere otherwise.
If you want to do it with a separate processing script, you will need to conclude by redirecting back to the original form on failure. However, you will find that persisting old data is more difficult that way.

Related Links

how to find page number of a website and put it in a variable [closed]
What is killing this process and how to stop it from doing so
Generate unique string using a string as a 'key' in PHP
How To Build Query with QueryBuilder?
Drupal 7 - module.install file schema error
How to pass string value to ajax in PHP & Mysql
Large Json returns null
Strategery - InfiniteScroll Not Working
Yii use of Component and behaviour
how pass value from select option to other select option in other form
PHP multiple selectboxes in while loop
Proper way to access database in PHP classes
What generates the Symfony/SensioLabs Documentation PDFs
unable to send mail with php from localhost [duplicate]
how to select latest record from table by using Group by statements
PHP mySQL Query ignores decimal values

Categories

HOME
ionic2
events
keyboard
transactions
plaid
braintree
ant
dronekit-python
dji-sdk
ui-automation
native-base
scheduler
polymer-2.x
msmq
amazon-emr
clish
max-msp-jitter
exe
jest
gitkraken
fido-u2f
gollum-wiki
batch-rename
animated-gif
substance
locks
intune
android-collapsingtoolbar
creation
busboy
pdfminer
stat
dbcc
type-inference
return-type
brightscript
phppgadmin
annotatorjs
boost-compute
nsdata
bbc-micro
preferenceactivity
odesk
boost-proto
largenumber
dsa
espresso
phpredis
erlog
shell-extensions
jcheckbox
phonegap-facebook-plugin
rubber
window-resize
storing-data
android-2.3-gingerbread
git-reset
sqlbindparameter
cidr
oxygene
jchartfx
jms-serializer
django-sites
strdup
progress-db
virtual-channel
quickgraph
form-for
ldif
sunstudio
django-filebrowser
tridion-2011
gitx
getproperty
background-repeat
will-paginate
nosetests
source-code-protection
downcasting
database-cloning
version-control-migration

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile