I just started learning PHP, and I wanted to see if I could make a form where the information would be stored in a database. To do this I am using phpMyAdmin. The problem occurs when I press the "Submit" button. I get an error that states the site is not working due to an HTTPS 405 error. Any help or guidance would be appreciated. Thankyou.
HTML
<!DOCTYPE html>
<html>
<!-- Head -->
<head>
<title></title>
</head>
<!-- Body -->
<body>
<h1 id="title">Info Form</h1>
<form action="info.php" method="POST">
<section id="firstName">
First Name:
<input type="text" placeholder="First Name" name="userFirstName" required>
<br><br>
</section>
<section id="lastName">
Last Name:
<input type="text" placeholder="Last Name" name="userLastName" required>
<br><br>
</section>
<section id="genderChoice">
Male:
<input type="radio" name="userGender" value="m" required>
Female:
<input type="radio" name="userGender" value="f" required>
<br><br>
</section>
<section id="submit">
<input type="submit" value="Submit">
</section>
</form>
</body>
</html>
PHP
$userFirstName = $_POST['userFirstName']; $userLastName = $_POST['userLastName']; $userGender = $_POST['userGender'];
$host = "127.0.0.1"; $dpUsername = "root"; $dpPassword = ""; $dpname = "form";
$conn = new mysqli($host, $dpUsername, $dpPassword, $dpname);
$INSERT = "INSERT Into info (userFirstName, userLastName, userGender) values($userFirstName, $userLastName, $userGender)";
this is a page that displays a list of creatives, and the form offers search functionality to search by job title:
if(isset($_POST['creatives-submit'])){
$job = $_POST['job-title'];
$data = \Db::Common($fms5->DBH)->getWhere("creatives", "creatives_active", "Yes"," AND creatives_job LIKE '%".$job."%'")->orderBy('creatives_name', 'asc');
}
<form method="post" name="creative-search">
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
is there anything that's obviously wrong my my code?
try changing if(isset($_POST['creatives-submit'])) to if(isset($_POST['job-title']) && !empty($_POST["job-title"])) as the form is posting the job-title value and this is the value you actually care about. (Since creatives-submit will always = Submit)
also change
<input class="form-control" type="textbox" name="job-title" id="job-title" placeholder="Search by job title" />
to <input class="form-control" type="text" name="job-title" id="job-title" placeholder="Search by job title" required/>
this means the form can't be submitted unless the job-title field has a value and had the correct type of text
Below is a modification of your code that just returns what the user searched for (Since I don't have it connected to a database)
<?php
if(isset($_POST['job-title']) && !empty($_POST["job-title"])){
$job = $_POST['job-title'];
?>
<p>You Searched For <?php echo $job;?></p>
<?php
}
?>
And the form
<!-- Search Form -->
<form method="post" name="creative-search">
<input class="form-control" required="required" type="text" name="job-title" id="job-title" placeholder="Search by job title" />
<input class="form-control" type="submit" name="creatives-submit" id="creatives-submit" style="display: none;" />
</form>
I am uploading a file from my local directory to a MySQL Databases' MEDIUMBLOB field using file_get_contents and I am receiving the following error:
Warning: file_get_contents(test.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\Craisins\forms\new_scenario.php on line 265
Yet the file is definitely there since I pick it with input type="file"
This seemed to be working yesterday and now suddenly today it's no longer working correctly and I'm not sure why.
Here is the full file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<!--Normal CSS Sheets-->
<link rel="stylesheet" type="text/css" href="../includes/style.css">
<link rel="stylesheet" type="text/css" href="../includes/style_form.css">
<!--BSM Select CSS Sheet-->
<link rel="stylesheet" type="text/css" href="../includes/bsmselect/css/jquery.bsmselect.css">
<!--jQuery-->
<script src="../includes/jquery-2.1.4.min.js"></script>
<!--jQuery for BSMSelect-->
<script src="../includes/bsmselect/js/jquery.bsmselect.js"></script>
<!--jQuery selector for BSMSelect-->
<script>
$(document).ready(function() {
$("select[multiple]").bsmSelect();
});
</script>
<style type="text/css">
<!--
.style1
{
font-size: 12px;
font-weight: bold;
}
textarea
{
resize: none;
}
-->
<?php include ("../includes/formSuccess.php");
include ("../includes/sql.php");
require_once "../WindowsAzure/WindowsAzure.php";
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Common\ServiceException;
?>
</style>
<title>****</title>
</head>
<body>
<div id="pageContainer">
<!--This section is for the upper half of the page. From the "Title" bar up.-->
<?php include ("../includes/formNav.php");?>
<!--End of the Header Section-->
<br>
<!--This section is where we add the main content for the page.-->
<div id="contentArea">
<div id="mainContentAdmin">
<h1 style="text-align:center">Add New Scenario</h1>
<?php
// display form if user has not clicked submit
if (!isset($_POST["btn_submit"]))
{
?>
<!--This will be the form that will hold the information of the entire page.-->
<form class="elegant-aero" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<!--Scenario Title-->
<label>
<span>Title:</span>
<input type="text" name="title" placeholder="Enter the full scenario name here">
</label>
<!--Scenario Description-->
<label>
<span>Description:</span>
<textarea name="desc" placeholder="Enter the scenario description here"></textarea>
</label>
<!--Scenario Times-->
<label id="time">
<span>Estimated Times:</span>
<!--Scenario Execution Time-->
<b>Execution:</b>
<input type="number" name="execHr" min="0" placeholder="Hours">
<input type="number" name="execMin" min="1" placeholder="Minutes">
<br><br>
<!--Scenario Debriefing Time-->
<b>Debriefing:</b>
<input type="number" name="debriHr" min="0" placeholder="Hours">
<input type="number" name="debriMin" min="0" placeholder="Minutes">
</label>
<br>
<!--Origin-->
<label>
<span>Origin:</span>
<input type="text" name="origin" placeholder="Enter the origin here (if any)">
</label>
<!--Target Users-->
<label>
<span>Target Users:</span>
<select name="users[]" multiple="multiple" title="Please select user type...">
<?php
selectMySQL("valid_trainee_type","trainee_type");
?>
</select>
</label>
<br>
<!--Capabilities-->
<label>
<span style="">Capabilities Required:</span>
<select name="capabilities[]" multiple="multiple" title="Please select capabilities...">
<?php
selectMySQL("valid_capability","name");
?>
</select>
</label>
<br>
<!--Prerequisites-->
<label>
<span style="">Prerequisite Knowledge:</span>
<select name="prerequisites[]" multiple="multiple" title="Please select prerequisites...">
<?php
selectMySQL("valid_prerequisite","prerequisite");
?>
</select>
</label>
<br>
<!--Equipment-->
<label>
<span>Equipment Required:</span>
<select name="equipment[]" multiple="multiple" title="Please select equipment...">
<?php
selectMySQL("valid_equipment","equipment");
?>
</select>
</label>
<!--Parameters-->
<label>
<span>Parameters to Monitor:</span>
<select name="parameters[]" multiple="multiple" title="Please select parameters...">
<?php
selectMySQL("valid_parameter","parameter");
?>
</select>
</label>
<br>
<!--Learning Objectives-->
<label>
<span>Learning Objectives:</span>
<textarea name="object" placeholder="Please list the learning objectives for the procedure..."></textarea>
</label>
<!--Setting-->
<label>
<span>Setting:</span>
<textarea name="set" placeholder="Please describe the ideal setting for the procedure..."></textarea>
</label>
<!--Preparation-->
<label>
<span>Preparation Required:</span>
<textarea name="prep" placeholder="Please list any steps that are required in order to perform this procedure..."></textarea>
</label>
<!--Participants-->
<label>
<span>Participants Required:</span>
<textarea name="part" placeholder="Please list the participants that will be required in order to perform this procedure..."></textarea>
</label>
<!--Patient Information-->
<label>
<span id="patient">Patient Information:</span>
<br>
<!--Patient Name-->
<b>Name</b>
<input type="text" name="patName" style="width:63%;" placeholder="Enter the patient's full name here">
<!--Patient Vitals-->
<b>Vitals</b>
<select name="patSex" style="width:15%;" title="Male or Female?">
<option value="male">male</option>
<option value="female">female</option>
</select>
<b>Age</b>
<input name="patAge" type="text" style="width:5%;">
<b>Height</b>
<input type="text" name="patHt" style="width:6%;" placeholder="(in)">
<b>Weight</b>
<input type="text" name="patWt" style="width:6%;" placeholder="(lbs)">
<span style="width: 30%; border-right:0; margin-right:0; padding-right: 5px;">Other Info</span>
<textarea style="width:63%;" name="patInfo" placeholder="Any other medical details?"></textarea>
</label>
<!--Scoring-->
<label>
<span>Scoring:</span>
<textarea name="score" placeholder="Please detail the scoring rubric for this scenario..."></textarea>
</label>
<!--Debrief-->
<label>
<span>Debrief:</span>
<textarea name="debrief" placeholder="Please detail the debriefing process for this scenario..."></textarea>
</label>
<!--Scenario File-->
<label>
<span>Scenario Details:</span>
<br>
<input type="file" name="fileScenDetails" value="Upload File">
<br>
<textarea name="textScenDetails" placeholder="Please any other scenario details here..."></textarea>
</label>
<!--Submit Button-->
<label>
<span> </span>
<input type="submit" name="btn_submit" class="button" value="Add Scenario"/>
</label>
</p>
</form>
<?php
} //end if
else
{
//Setup the MySQL server
$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "****";
//Create connection to the MySQL server
$conn = new mysqli($servername, $username, $password, $dbname);
//Check connection
if($conn -> connect_error)
{
die("Connection failed: " . $conn -> connect_error);
} //end if
//Organize Execution Time
$exec_time = "00:".$_POST['execHr'].":".$_POST['execMin'];
//Organize Debriefing Time
$debrif_time = "00:".$_POST['debriHr'].":".$_POST['debriMin'];
//Organize BLOB
$blob = addslashes(file_get_contents($_POST['fileScenDetails']));
$sql="INSERT INTO scenario (title, description, est_scenario_time, est_debriefing_time,
origin, objectives, setting, preparation, participants,
patient_name, patient_sex, patient_age, patient_height,
patient_weight, patient_info, scoring, debrief,
scenario_file, scenario_text)
VALUES ('".$_POST['title']."','".$_POST['desc']."', '$exec_time', '$debrif_time',
'".$_POST['origin']."','".$_POST['object']."',
'".$_POST['set']."','".$_POST['prep']."',
'".$_POST['part']."','".$_POST['patName']."',
'".$_POST['patSex']."','".$_POST['patAge']."',
'".$_POST['patHt']."','".$_POST['patWt']."',
'".$_POST['patInfo']."','".$_POST['score']."',
'".$_POST['debrief']."','$blob',
'".$_POST['textScenDetails']."');";
if($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
} //end else
$conn->close();
/*
//Insert Target Users (Trainee Types)
insert_Scen_Dev('scenario','title', 'title','users',
'valid_trainee_type','trainee_type', 'scenario_trainee_type',
'scenario_id', 'valid_trainee_type_id');
//Insert Capabilities
insert_Scen_Dev('scenario','title', 'title','capabilities',
'valid_capability','name', 'scenario_capabilities',
'scenario_id', 'valid_capability_id');
//Insert Prerequisites
insert_Scen_Dev('scenario','title', 'title','prerequisites',
'valid_prerequisite','prerequisite', 'scenario_prerequisites',
'scenario_id', 'valid_prerequisite_id');
//Insert Equipment
insert_Scen_Dev('scenario','title', 'title','equipment',
'valid_equipment','equipment', 'scenario_equipment',
'scenario_id', 'valid_equipment_id');
//Insert Parameters
insert_Scen_Dev('scenario','title', 'title','parameters',
'valid_parameter','parameter', 'scenario_parameters',
'scenario_id', 'valid_parameter_id');
*/
echo "<div class='sqlSuccess'>A new Scenario has been added!</div>";
} //end else
?>
</div>
</div>
<!--End of the Content Section-->
<!--This section is for the lower half of the page. From the copyright bar down.-->
<div id="footer">
<div id="footerCenter">
Copyright ©
<script language="javascript" type="text/javascript">
var today = new Date();
var year = today.getFullYear();
document.write(year);
</script>
</div>
</div>
<!--End of the Footer Section-->
</div>
</body>
There are several problems with this code. At first, file won't upload unless you specify a correct enctype attribute on a form element:
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Also the file address is available under $_FILES['fileScenDetails']['tmp_name'], not under $_POST['fileScenDetails'], so:
$blob = addslashes(file_get_contents($_FILES['fileScenDetails']['tmp_name']));
Note, that you should check if file upload was successfull, as otherwise your code will trigger warning (eg. when user won't specify file). Add something like this:
if(isset($_FILES['fileScenDetails']['error']) && UPLOAD_ERR_OK == $_FILES['fileScenDetails']['error']) {
// do your stuff here
} else {
// display 'no-file' error to the user
}
Also, you should definitely check if the specified file was really uploaded, to avoid stealing your code with malformed requests. Check is_uploaded_file function here: http://php.net/manual/en/function.is-uploaded-file.php
There are also some possible SQL injections and other security issues.
I have a form that you can see below. When the submit button is pressed, i want a script called "Orders.php" to send all the data to a database. Also, when the button is pressed, i want the user to be redirected to another page. The problem is that now when the php file is in the action property, the user sees the php code..
Any ideas on how to fix this?
HTML
<form id="TheForm" action="orders.php" method="post">
<div id="Row">
<input type="text" id="Name" placeholder="*Förnamn" required >
<input type="text" id="Surname" placeholder="*Efternamn" required >
</div>
<div id="Row">
<input type="email" id="FirstEmail" placeholder="*e-postadress" autocomplete="on" required >
<input type="email" id="SecondEmail" placeholder="*Verifiera e-postadress" autocomplete="off" required >
</div>
<div id="Row">
<input type="text" id="Town" placeholder="*Ort" required >
</div>
<div id="Row">
<input type="text" id="Address" placeholder="*Adress" required >
</div>
<div id="Row">
<input type="text" id="PostCode" placeholder="*Postnummer" required >
</div>
<div id="Row">
<input type="text" id="MobileNumber" placeholder="*Mobilnummer" required >
<input type="text" id="TelephoneNumber" placeholder="Telefonnummer" >
</div>
<textarea id="Comment" placeholder="Förslag på hur vi skulle kunna förbättra oss!"></textarea>
<input type="submit" id="Submit" onclick="SlutSidan.html" value="Skicka">
</form>
Orders.php
<?php
$connect = mysql_connect(“server_name”, “admin_name”, “password”);
if (!connect)
{
die('Connection Failed: ' . mysql_error());
}
mysql_select_db(“database_name”, $connect);
Thanks a lot for the help!
You can find your form data in the $_POST variable like $_POST['name']
and to forward it to another page you can use the following function
header( "Location: url/to/mySecondPage.php" );
My suggestion
change your form
Now it will take your user where you want him to go
<form id="TheForm" action="users/final/destination.php" method="post">
<!-- add this hidden input -->
<input type="hidden" name="actionScript" value="orders" />
...
</form>
change users/final/destination.php
This should be on the top of the file
<?php
// Check if this file has been requested through your form
if (isset($_POST['actionScript'])) {
// you may want to encapsulate this in a function
ob_start();
require 'orders.php';
$debug_info = ob_get_clean();
}
// done, now do everything you wanted in usersFinalDestination.php
no header redirect required
you may want to log your debug info for errors
you can encapsulate your orders.php script in a function to avoid variable collisions
Okay, first here is your HTML file:
<!-- HTML FILE -->
YOU MUST HAVE "name" attribute in "input" tag to use its value in php file.
<form id="TheForm" action="orders.php" method="post">
<div id="Row">
<input type="text" id="Name" name="Name">
<input type="text" id="Surname" name="Surname" >
</div>
</form>
Now here is your PHP file (just be sure to put "<?php" at starting and "?>" at end of php file ):
orders.php
<?php
$connect = mysql_connect(“server_name”, “admin_name”, “password”);
if (!connect)
{
die('Connection Failed: ' . mysql_error());
}
mysql_select_db(“database_name”, $connect);
$_POST["Name"];//to access input field value with name="Name"
$_POST["Surname"];// to access input field value with name="Surname"
//Once you are done with saving data to database you can redirect to another page using:
header("Location: redirect.php");
?>
I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.
Here is the code on the index.html page where the form is
<form id="subscribe-form" action="send.php" method="post">
<p id="status"></p>
<div>
<label for="title">Title:</label>
<select class="uniform" name="title" id="title">
<option>Please Choose</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Ms</option>
</select>
</div>
<div>
<label for="firstName">First name:</label>
<input type="text" id="firstName" name="firstName" />
</div>
<div>
<label for="surname">Surname:</label>
<input type="text" id="surname" name="surname" />
</div>
<div>
<label for="email">Email:</label>
<input type="text" id="email" name="email" />
</div>
<div>
<label for="phone">Contact Number:</label>
<input type="text" id="phone" name="phone" />
</div>
<div>
<label for="title">How did you hear about us?</label>
<select class="uniform" name="refer" id="refer">
<option>Please Choose</option>
<option>Google</option>
<option>Yahoo</option>
<option>Word of Mouth</option>
<option>Others</option>
</select>
</div>
<div>
<input type="checkbox" name="news_updates" value="1" />
I'd like to hear about the latest news and events updates</div>
<div>
<input class="button" type="submit" value=""/>
</div>
</form>
Here is the code for send.php
<?php
include ('connection.php');
$sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
VALUES
('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";
if (!mysql_query($sql, $connected))
{
die('Error: ' . mysql_error());
}
mysql_close($connected);
?>
I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database .
I found this tutorial which was kind of helpful -
http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/
and this is the form on my unsubscribe.html page.
<form id="unsubscribe_form" action="delete.php" method="post">
<div>
<label for="email_remove">Email:</label>
<input type="text" id="email_remove" name="email_remove" />
</div>
<div>
<input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
</div>
</form>
but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank.
So I am guessing I can't have two "post" method maybe??
If someone could guide me in the right direction that would be much appreciate. Thanks.
I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.
Example: in your subscribe.php
you should have :
<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />
in send.php
you must do:
if(!isset($_POST['subscribe'])
{
header('location: subscribe.html');
}
You must use isset for your pages.
If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.
Your delete.php script should be:
<?php
require ('connection.php'); // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";}
?>
your delete.php seems OK to me.
Can add the following to Line 2
echo "";
print_r($_POST);
and post array in comments?