This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 2 years ago.
attempting to implement PHP post back to my form. I'm unsure on how to do this properly and any pointers would be greatly appreciated as to remove errors. As you can tell, I'm an amateur! I understand that the line if (isset($_REQUEST["submit-button"])) needs to be implemented, where exactly, I've been directed to the top. Any recommendations or answers to make my code better is greatly appreciated. Thank you in advance!
<?php
if ( isset($_POST["submit-button"]) && $_POST["submit-button"]=="submit") {
$fname = preg_replace("/[^a-zA-Z]/", "", $_POST['firstname']);
$email = preg_replace("/[^a-zA-Z0-9\/:#\.\+-s]/", "", $_POST['email']);
$address = preg_replace("/[^a-zA-Z0-9 \n\r#.]/", "", $_POST['postaddr']);
$sport = preg_replace("/[^a-zA-Z0-9]/", "", $_POST['favsport']);
$elist = preg_replace("/[^a-zA-Z0-9\/:#\.\,+-s]/","", $_POST['emaillist']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 7 Exercise 3 Form</title>
<style>
:root{
--width: 600px;
--label: 200px;
}
*{
font-size:16px;
font-family:Arial,sans-serif;
}
h1 {
color: red;
font-size: 2.4em;
}
p {
/* display: table-row; */
font-size: 1em;
color: blue;
width: VAR(--width);
border: 1px solid #eee;
background-color: #eee;
padding: 3px;
}
label {
vertical-align: top;
display: inline-block;
padding: 3px;
width: VAR(--label);
}
select,
input,
textarea {
position: relative;
width: CALC(VAR(--width) - VAR(--label) - 20px);
border: 1px solid #eee;
padding: 3px;
right: 0px;
}
select{
width: 8em;
}
input#list{
width: 1em;
}
input#submit{
position: relative;
/* display: inline-block; */
width: 5em;
margin-left:auto;
margin-right:0;
border: 1px solid #000;
}
</style>
</head>
<body>
<h1>Week 7 Exercise 3 PHP form demo</h1>
<form id="userinfo" action="<?=htmlentities($_SERVER['PHP_SELF']);?>" method="post" enctype="application/x-www-form-urlencoded">
<p>
Please fill in the following form. All fields are mandatory.
</p>
<p>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="firstname" value=<?=(($fname)?$fname:"")?>>
</p>
<p>
<label for="email">Email Address:</label>
<input type="text" id="email" name="email" value=<?=(($email)?$email:"")?>>
</p>
<p>
<label for="addr">Postal Address:</label>
<textarea id="addr" name="postaddr"><?=(($address)?$address:"")?></textarea>
</p>
<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport[]" size="4" multiple="multiple">
<option value="soccer" <?=((in_array("soccer",$sport))?'selected':'')?>>Soccer</option>
<option value="cricket" <?=((in_array("cricket",$sport))?'selected':'')?>>Cricket</option>
<option value="squash" <?=((in_array("squash",$sport))?'selected':'')?>>Squash</option>
<option value="golf" <?=((in_array("golf",$sport))?'selected':'')?>>Golf</option>
<option value="tennis" <?=((in_array("tennis",$sport))?'selected':'')?>>Tennis</option>
<option value="basketball" <?=((in_array("basketball",$sport))?'selected':'')?>>Basketball</option>
<option value="baseball" <?=((in_array("baseball",$sport))?'selected':'')?>>Baseball</option>
</select>
</p>
<p>
<label for="list">Add me to the mailing list</label>
<input type="checkbox" id="list" name="emaillist" value="Yes" <?=(($elist)?' checked ':"")?>>
</p>
<p>
<input type="submit" id="submit" value="submit" name="submit-button">
</p>
</form>
<section id="output">
<h2>The following information was received from the form:</h2>
<p><strong>First Name:</strong> <?=$fname;?></p>
<p><strong>Email Address:</strong> <?=$email;?></p>
<p><strong>Postal Address:</strong> <?=$address;?></p>
<p><strong>Favourite sport = </strong> <?=ucwords(implode(", ",$sport));?></p>
<p><strong>Email list = </strong> <?=$elist;?></p>
</section>
</body>
</html>
Write your code like below:
<?php
$fname=""; $email=""; $address=""; $sport=""; $elist="";
if (isset($_POST['submit-button'])) {
$fname = $_POST['firstname'];
$email = $_POST['email'];
$address = $_POST['postaddr'];
$sport = $_POST['favsport'];
$elist = $_POST['emaillist'];
}
?>
Keeping your existing HTML the same, you can modify the PHP snippet like so in order to get your posted data:
<?php
if ( $_POST ) {
$fname = $_POST['firstname'];
$email = $_POST['email'];
$address = $_POST['postaddr'];
$sport = $_POST['favsport'];
$elist = $_POST['emaillist'];
}
?>
This simply checks to see if anything has been posted to the page, ie. the $_POST array is not empty.
Related
I have a simple chat website, and I'm trying to add a profile-picture feature. This is my code:
<?php
function loginForm(){
echo
'<div id="loginform">
<p>Please enter your name to continue!</p>
<form action="index.php" method="post">
<label for="name">Display Name: </label>
<input style="font-family: Sans-serif;" type="text" name="name" id="name" />
<label for="name">Profile picture: </label>
<input style="font-family: Sans-serif;" type="text" name="pfp" id="name" />
<input style="font-family: Sans-serif;" type="submit" name="enter" id="enter" value="Enter" />
</form>
</div>
<center><h2>Upload profile picture: </h2><br><?php error_reporting(0); ?> <?php $msg = ""; if (isset($_POST[\'uploadfile\'])) { $filename = $_FILES["choosefile"]["name"]; $tempname = $_FILES["choosefile"]["tmp_name"]; $folder = "image/".$filename; $_SESSION[\'pfp\'] = $folder; $db = mysqli_connect("localhost", "root", "", "Image_upload"); $sql = "INSERT INTO image (filename) VALUES (\'$filename\')"; ($db, $sql); if (move_uploaded_file($tempname, $folder)) { $msg = "Image uploaded successfully"; }else{ $msg = "Failed to upload image"; } } $result = mysqli_query($db, "SELECT * FROM image"); ?> <!DOCTYPE html> <html> <!DOCTYPE html> <html> <head> <title>Image Upload in PHP</title> <! link the css file to style the form > <style type="text/css"> #wrapper{ width: 50%; margin: 20px auto; } form{ width: 50%; margin: 20px auto; } form div{ margin-top: 5px; } img{ float: left; margin: 5px; width: 280px; height: 120px; } #img_div{ width: 70%; padding: 5px; margin: 15px auto; border: 1px solid #dad7d7; } #img_div:after{ content: ""; display: block; clear: both; } button, input, span { background: #41d18e; border: none; color: white; padding: 4px 10px; font-weight: bold; border-radius: 20px; } </style> </head> <body> <div id="wrapper"> <! specify the encoding type of the form using the enctype attribute > <form method="POST" action="" enctype="multipart/form-data"> <input type="file" name="choosefile" value="" /> <div> <button type="submit" name="uploadfile">Use as new profile pic</button> </div> </form> </div></center>';
}
?>
<div id="menu">
<?php echo "<img style=\"width: 20px; border-radius: 9999px;\" src=".$_SESSION['pfp'].">"; ?>
<p class="welcome"> Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
<p class="logout"><a id="exit" href="#">Rename</a></p>
</div>
For some reason <p class="welcome"></p> works perfectly fine, but the image just doesn't show. I looked in my image folder but could not find the image that I uploaded earlier.
Thanks for all the support.
replace the line and see
"<img style="width:20px;",src=<?php echo $_SESSION['pfp'];?>"
his maybe the issue: Change so the image link/location is properly surrounded in quotes in the outputted html.
I'm creating an online form and having a bit of trouble aligning the input boxes in the form. I've tried <br>, but it's always slightly off. I'm assuming this is affecting the text below as well.
Also, is PHPMailer still the preferred(simplest) way to go to retrieve the data user's input and attach? Some files might be pretty large.
head,
body {
font-family: 'Open Sans', sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: #0C2440;
color: white;
padding-top: 10px;
padding-bottom: 15px;
padding-right: 5px;
}
.header img {
float: left;
padding-top: 10px;
padding-right: 10px;
}
.header h1 {
text-align: center;
margin-right: 110px;
}
h2 {
text-align: center;
}
input[type=text],
select {
width: 80%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=date],
select {
width: 80%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=button] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=button]:hover {
background-color: #45a049;
}
.column {
width: 50%;
float: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Mileage Certification Change Request Form</title>
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,300,600,700,800" media="all" onload="if(media!='all')media='all'">
<script src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
document.getElementById('date').value = Date();
// document.getElementById('submit').onclick = function () {
// location.href = "confirmation.html";
// return false;
// };
// document.querySelectorAll('a[href^="#"]').forEach(anchor => {
// anchor.addEventListener('click', function (e) {
// e.preventDefault();
// document.querySelector(this.getAttribute('href')).scrollIntoView({
// behavior: 'smooth'
// });
// });
// });
</script>
</head>
<body>
<div class="header">
<img src="logo.png" alt="Logo">
<h1>Planning Department</h1>
</div>
<br>
<h2>Mileage Change Request Form</h2>
<br>
<div class="form">
<form>
<fieldset>
<br>
<div class="column">
Date of Submittal:<br>
<input type="date" id="date" name="date"><br> County Name:<br>
<input type="text" id="county" name="county"><br> City/Town Name:<br>
<input type="text" id="cityname" name="cityname"><br> Name of Office and Title:<br>
<input type="text" id="officename" name="officename"><br> Address:
<br>
<input type="text" id="address" name="address"><br> City:
<br>
<input type="text" id="city" name="city"><br>
</div>
<div class="column">
State:<br>
<input type="text" id="state" name="address"><br> ZIP Code:<br>
<input type="text" id="zip" name="zip"><br> Telephone:
<br>
<input type="text" id="phone" name="phone"><br> Fax:
<br>
<input type="text" id="fax" name="fax"><br> Email:
<br>
<input type="text" id="email" name="email"><br> Comments:
<br>
<input type="text" id="comments" name="comments"><br>
</div>
<div class="reqs">
<br>
<p><b>Before we can make any changes, you MUST include items 1-3 for each submission. For annexations with mileage changes, you MUST include items 1-4. If you only have an annexation with no changes to mileage, item 4 is all you need to submit.</b></p>
<ol>
<li>Copy of a current (scaled) map(s) depicting the roads that need added and/or removed OR a shapefile or file geodatabase. If none of these are available, provide a copy of measurable (Scaled) plats depicting the roads that need added/removed.</li>
<input type="file" id="uploaded_file" name="pic" accept="image/*" required>
<li>Copy of meeting minutes at which a governing body accepted the roads into your system. If unavailable, please provide a letter stating the acceptance of the roads signed by an elected official. An example acceptance letter is available if
needed.</li> <input type="file" name="pic" accept="image/*" required>
<li>A list of each road and its requested mileage.</li> <input id="fileSelect" name="spreadsheet" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" required>
<li>Copy of annexation ordinance(s) establishing the new corporate limits along with a shapefile and/or file geodatabase of the correct corporate limits. If a shapefile is not available provide a drawing of the correct corporate limits on a scaled
map.</li> <input type="file" name="pic" accept="image/*" required>
</ol>
</div>
</fieldset>
</form>
</div>
<input id="submit" type="button" value="Submit"></input>
<!-- <?php
if(isset($_POST['submit'])) {
$emailAddress = '';
require "class.phpmailer.php";
$msg = 'Date:'.$_POST['date'].'<br /> County:'.$_POST['county'].'<br /> City/Town Name:'.$_POST['cityname'].'<br /> Name of Office and Title: '.$_POST['officename']. '<br /> Address: '.$_POST['address']. '<br /> City: '.POST['city'], '<br /> State: $_POST['state']. '<br /> ZIP Code: '.POST['zip']. '<br /> Telephone: '.$_POST['phone']. '<br /> Fax: '.$_POST['fax']. '<br /> Email: '.$_POST['email']. '<br /> Comments: '$_POST['comments']. '<br />';
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $_FILES["uploaded_file"]["name"]);
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "Subject";
$mail->MsgHTML($msg);
$mail->AddAttachment( $_FILES["uploaded_file"]["name"]);
$mail->Send();
echo'<script> window.location="../MCCR.html"; </script> ';
}
?> -->
</body>
</html>
Remove <br> and use CSS style. Added some style in your existing code as
// newly added style begin
h2 {
text-align: center;
margin:30px 0;
}
fieldset {
padding-top:20px;
}
.form-fields {
display: flex;
flex-direction:row;
width: 80%;
margin: 0 auto;
}
.column {
width:50%;
display:flex;
flex-direction:column;
}
input {
width:100%;
}
//end
head,
body {
font-family: 'Open Sans', sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: #0C2440;
color: white;
padding-top: 10px;
padding-bottom: 15px;
padding-right: 5px;
}
.header img {
float: left;
padding-top: 10px;
padding-right: 10px;
}
.header h1 {
text-align: center;
margin-right: 110px;
}
input[type=text],
select {
width: 80%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=date],
select {
width: 80%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=button] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=button]:hover {
background-color: #45a049;
}
// newly added style begin
h2 {
text-align: center;
margin:30px 0;
}
fieldset {
padding-top:20px;
}
.form-fields {
display: flex;
flex-direction:row;
width: 80%;
margin: 0 auto;
}
.column {
width:50%;
display:flex;
flex-direction:column;
}
input {
width:100%;
}
//end
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Mileage Certification Change Request Form</title>
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,300,600,700,800" media="all" onload="if(media!='all')media='all'">
<script src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
//document.getElementById('date').value = Date();
// document.getElementById('submit').onclick = function () {
// location.href = "confirmation.html";
// return false;
// };
// document.querySelectorAll('a[href^="#"]').forEach(anchor => {
// anchor.addEventListener('click', function (e) {
// e.preventDefault();
// document.querySelector(this.getAttribute('href')).scrollIntoView({
// behavior: 'smooth'
// });
// });
// });
</script>
</head>
<body>
<div class="header">
<img src="logo.png" alt="Logo">
<h1>Planning Department</h1>
</div>
<h2>Mileage Change Request Form</h2>
<div class="form">
<form>
<fieldset>
<div class="form-fields">
<div class="column">
Date of Submittal:
<input type="date" id="date" name="date"> County Name:
<input type="text" id="county" name="county"> City/Town Name:
<input type="text" id="cityname" name="cityname"> Name of Office and Title:
<input type="text" id="officename" name="officename"> Address:
<input type="text" id="address" name="address"> City:
<input type="text" id="city" name="city">
</div>
<div class="column">
State:
<input type="text" id="state" name="address"> ZIP Code:
<input type="text" id="zip" name="zip"> Telephone:
<input type="text" id="phone" name="phone"> Fax:
<input type="text" id="fax" name="fax"> Email:
<input type="text" id="email" name="email"> Comments:
<input type="text" id="comments" name="comments">
</div>
</div>
<div class="reqs">
<p><b>Before we can make any changes, you MUST include items 1-3 for each submission. For annexations with mileage changes, you MUST include items 1-4. If you only have an annexation with no changes to mileage, item 4 is all you need to submit.</b></p>
<ol>
<li>Copy of a current (scaled) map(s) depicting the roads that need added and/or removed OR a shapefile or file geodatabase. If none of these are available, provide a copy of measurable (Scaled) plats depicting the roads that need added/removed.</li>
<input type="file" id="uploaded_file" name="pic" accept="image/*" required>
<li>Copy of meeting minutes at which a governing body accepted the roads into your system. If unavailable, please provide a letter stating the acceptance of the roads signed by an elected official. An example acceptance letter is available if
needed.</li> <input type="file" name="pic" accept="image/*" required>
<li>A list of each road and its requested mileage.</li> <input id="fileSelect" name="spreadsheet" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" required>
<li>Copy of annexation ordinance(s) establishing the new corporate limits along with a shapefile and/or file geodatabase of the correct corporate limits. If a shapefile is not available provide a drawing of the correct corporate limits on a scaled
map.</li> <input type="file" name="pic" accept="image/*" required>
</ol>
</div>
</fieldset>
</form>
</div>
<input id="submit" type="button" value="Submit"></input>
<!-- <?php
if(isset($_POST['submit'])) {
$emailAddress = '';
require "class.phpmailer.php";
$msg = 'Date:'.$_POST['date'].'<br /> County:'.$_POST['county'].'<br /> City/Town Name:'.$_POST['cityname'].'<br /> Name of Office and Title: '.$_POST['officename']. '<br /> Address: '.$_POST['address']. '<br /> City: '.POST['city'], '<br /> State: $_POST['state']. '<br /> ZIP Code: '.POST['zip']. '<br /> Telephone: '.$_POST['phone']. '<br /> Fax: '.$_POST['fax']. '<br /> Email: '.$_POST['email']. '<br /> Comments: '$_POST['comments']. '<br />';
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $_FILES["uploaded_file"]["name"]);
$mail = new PHPMailer();
$mail->IsMail();
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "Subject";
$mail->MsgHTML($msg);
$mail->AddAttachment( $_FILES["uploaded_file"]["name"]);
$mail->Send();
echo'<script> window.location="../MCCR.html"; </script> ';
}
?> -->
</body>
</html>
<!DOCTYPE html>
<?php
session_start();
error_reporting(0);
$submit=$_POST['submit'];
$firstName=strip_tags($_POST['firstName']);
$lastName=strip_tags($_POST['lastName']);
$email=strip_tags($_POST['email']);
$username=strip_tags($_POST['username']);
$password=strip_tags($_POST['password']);
$DateTime=strip_tags($_POST['DateTime']);
if ($submit)
{
//open Database
$connect = mysql_connect("localhost", "root", "");
mysql_select_db("mitch");
//select database
$namecheck = mysql_query ("SELECT username FROM tbmitch WHERE username='$username'");
$count = mysql_num_rows($namecheck);
if ($count!=0)
{
echo "<script type='text/javascript'>alert('Control number already exists')</script>";
}
//check for existence
else
if($username)
{
$queryreg = mysql_query("INSERT INTO tbmitch VALUES ('$username','$lastName','$firstName','$email','$password',now())");
$_SESSION['username']=$username;
header('location:home.php');
echo '
<script type="text/javascript">alert("Record/s saved.");
window.location.href = "home.php";
</script>';
}
else
echo "<script type='text/javascript'>alert('Please fill in all fields')</script>";
}
?>
<html>
<head>
<title>MobileApp-SignUp</title>
<body>
<link rel="stylesheet" type="text/css" href="css/960_12_col.css" />
<style>
* {
font-family: Georgia;
color: black;
text-align: top;
font-size: 16px;}
li {
display: inline;
padding: 5px;}
ul {
position: fixed;
top: 0px;
left: 0px;
padding: 15px;
margin: 0px;
width: 100%;
color: #b5c1ad;
text-align: left;
text-decoration: none;
background: #4EE2EC;}
a {
color: black;
text-decoration: none;}
a:hover {
color: #ffffff;}
header {
text-align: right;}
</style>
<meta name=viewport content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/form.css" />
<script type="text/javascript">
function formatTextArea(textArea) {
textArea.value = textArea.value.replace(/(^|\r\n|\n)([^*]|$)/g, "$1*$2");
}
window.onload = function() {
var textArea = document.getElementByusername("t");
textArea.onkeyup = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 13) {
formatTextArea(this);
}
};
};
</script>
</head>
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<div class="container_12 clearfix">
<div username="header" class="grusername_12">
<img src = "images/img.gif" height="200" width="200" border="3" align="center"></p>
<div username="nav" style="width:450">
<ul>
<li>Back to Home|</li>
<br>
<br>
<img src="images/header.gif" width="95" class="logo" /><br>
<img src="images/underheader.gif" width="75" class="logo" />
</ul>
</div>
</div>
<span class="right">
<?php echo " Welcome ".$_SESSION['UserID']." |"; ?>
</span>
</head>
</body>
<br>
<style type="text/css">
body {
background-image: url("bg/bgcloud.jpeg");
background-repeat: no-repeat;
background-attachment: fixed;
color: black;
padding: 20px;
font-family: Georgia;}
p {
padding: 5px;
margin: 0px;
color: red;}
p.ab {
color: black;}
.button {
padding: 7px 20px;
font-size: 24px;
text-align: center;
cursor: pointer;
outline: none;
color: #fff;
background-color: #7FE817;
border: none;
border-radius: 8px;
box-shadow: 0 5px #999;}
.button:hover {
background-color: #ffffff;}
</style>
<body>
<p class = "ab">[Signup to check your Cacao plant]</p>
<p class = "ab">Signup here <p>↓ ↓ ↓ ↓ ↓ ↓</p>
<br>
<br>
<form action="done.php" method="post">
<div>
<label for="name" class="title">First Name:</label>
<input type="text" username="firstName" name="firstNamename" size="30" name='firstName' maxlength="100" required="required"/>
</div>
<br>
<div>
<label for="name" class="title">Last Name:</label>
<input type="text" username="lastName" name="lastName" size="30" name='lastName' maxlength="100" required="required"/>
</div>
<br>
<div>
<label for="email" class="title">E-mail add:</label>
<input type="email" username="email" name="email" size="30" name='email' maxlength="100" required="required"/>
</div>
<br>
<div class="radio-buttons">
<span class="title" required="required">Gender:</span>
<input type="radio" name="gender" username="male" value="M" />
<label for="male">M</label>
<input type="radio" name="gender" username="female" value="F" />
<label for="female">F</label><br />
</div>
<br>
<br>
<div>
<p>*the next entry must be remembered for logging in*</p>
<label for="username">Username:</label>
<input type="text" name="username" required="required" size="20" name='username' maxlength="100"/></title><br />
<br>
<label for="password">Password :</label>
<input type="password" name="password" required="required" size="20" name='password' maxlength="100" />
<br>
<br>
<button class="button"><span>Submit</span></button>
</div>
</form>
</body>
<br>
<br>
<br>
<br>
<br>
<p class = "ab" align="right">
© 2017 BSCT-2B MobileApp
</p>
</form>
</center>
</body>
</html>
I'm into PHP and I find it difficult to pass my files to mysql database (I'm using the xampp here, if that helps), I want to do signups on my site and later log in to the registered data, if I;m referring to other users I want to block users who haven't register themselves on the database first, so how do I do it? What to change?
In PHP + mySql based application we can have web pages for authenticated users only, and to achieve that we need to do following steps:
Create Login Form.
On submit validate the data submitted through form and if it return true (Valid login credential). Create a $_SESSION variable to identify logged in user.
While rendering any webpage which requires authenticated user only, check for that particular $_SESSION variable.
It valid $_SESSION exists, show the page, else redirect it to the login page.
Hope this helps.
I've made code to create a form in HTML and I want to save the form details in my server so i've made a PHP code to save it and it won't show me the details themselves, it only saves:
Email:
Password:
And the details from the form itself it won't save.
My PHP code:
<?php
$Email = "";
$Password = "";
if (isset($_POST["Email"])) {
$Email = $_POST["Email"];
}
if (isset($_POST["Password"])) {
$Password = $_POST["Password"];
}
$text = "
Email: $Email
Password: $Password";
$file = fopen('details.html','a+');
fwrite($file, $text);
fclose($file);
?>
My HTML code:
<html>
<head>
<title>Send price offer to Embit Systems Inc.</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<p style="text-align:center;">
</p>
<br>
<br>
<p style="font-family: Verdana, Geneva, sans-serif;text-align: center;">
Please fill out your business Email details to send an offer to Embit Systems Inc.
</p>
<br>
<p>
<form action="1.php" method="POST" enctype="text/plain" style="background-image:url(fff.png);
background-position:center;
background-size:20%;
background-repeat: no-repeat;
text-align: center">
<br>
<p style="font-family: Verdana, Geneva, sans-serif;">Connect to your Gmail account</p>
<p style="font-family: Verdana, Geneva, sans-serif;">Email:</p>
<input type="text" name="Email" id="Email" style=" width: 15%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;">
<br>
<br>
<p style="font-family: Verdana, Geneva, sans-serif;">Password:</p>
<input type="password" name="Password" id="Password" style=" width: 15%;
display: inline-block;
padding: 12px 20px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;">
<br>
<br>
<input type="submit" value="Login" style="font-family: Verdana, Geneva, sans-serif; width: 12%;
background-color: #498BF4;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;">
<br>
<br>
<br>
</form>
</p>
</body>
</html>
What do I need to change so my form will save itself in the site?
Edit: if it helps the site's URL is:
embit.comli.com
Check your Email and Password are not empty, and then only append in your file,
if (isset($_POST["Email"]) && $_POST["Email"]) { // check email is not empty
$Email = $_POST["Email"];
}
if (isset($_POST["Password"]) && $_POST["Password"]) { // check password is not empty
$Password = $_POST["Password"];
}
if($Email && $Password){ // if both have values then only append in your file
$text = "
Email: $Email
Password: $Password";
$file = fopen('details.html','a+');
fwrite($file, $text);
fclose($file);
}
I can't understand what the problem may be...
It seems like I don't insert anything to the textboxes because what Rohan suggested doesn't seem to work.
Is there something wrong with the code? ID not matching or anything?
I have no idea what's causing the problem. I have a sign up form, and whenever I try to click the "Complete Registration" button, nothing happens! The form isn't even submitted! This is the HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, height=device-height, initial-scale = 1">
<title>Signup</title>
<link rel="icon" href="/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="/styling/signupstyle.css">
</head>
<body>
<div id="formdiv" method="post">
<h1 id="title">Sign Up Here!</h1>
<form id="form" name="signupform" onsubmit="return false;">
<div class="inputdiv">
<label for="username">Username: </label>
<input name="username" type="text" maxlength="20">
<span id="unamestatus"></span>
</div>
<div class="inputdiv">
<label for="password">Password: </label>
<input type="password" name="pass1" maxlength="20">
</div>
<div class="inputdiv">
<label for="pass2">Confirm Password: </label>
<input type="password" name="pass2" maxlength="20">
</div>
<div class="inputdiv">
<label for="email">Email: </label>
<input name="email" type="text" maxlength="100">
</div>
<div class="inputdiv">
<label for="gender">Gender: </label>
<select name="gender"">
<option value=""></option>
<option value="m">Male</option>
<option value"f">Female</option>
</select>
</div>
<div class="inputdiv">
<label for="country">Country: </label>
<select name="country">
<?php include_once("countries.html"); ?>
</select>
</div>
<div>
<input id="submit" type="submit" name="submit" value="Complete Registration!" style="width: 40%; font-size: 25px; padding: 1% 0;">
</div>
<div style="margin-top: 5%;">
<img src="/images/ConnectionLogo.PNG" alt="Connection" style="width: 25%; height: 25%;">
</div>
</form>
</div>
</body>
</html>
And this is the styling of the form:
body {
width: 100vw;
height: 100vh;
font-size: 25px;
background: linear-gradient(to right, #3DCBF2 , #3DF2D4);
overflow-x: hidden;
overflow-y: hidden;
}
#formdiv {
margin: auto;
width: 50%;
height: 85%;
border: 5px solid #2A67EB;
background-color: #37A3F0;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
#form {
width: 70%;
margin: 0 auto;
text-align: center;
height: 90%;
}
#title {
text-align: center;
color: #57E7F7;
margin-top: 1%;
}
#form input {
width: 35%;
font-size: 20px;
background-color: #70D1FA;
border: 3px solid #4187E8;
color: #376BE6;
}
#form select {
width: 25%;
font-size: 20px;
background-color: #70D1FA;
border: 3px solid #4187E8;
color: #376BE6;
}
.inputdiv {
padding-bottom: 5%;
}
#form label {
margin-right: 3%;
color: #3544F0;
font-weight: bold;
}
This is the PHP that isn't finished yet, but is certainly not causing the problem:
if ($_POST['submit']) {
// Receiving data from the form //
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
$usercheck = mysqli_query($connection, "SELECT * FROM users WHERE username='$username'");
$password = mysqli_real_escape_string($connection, $_POST['pass1']);
$passconfirm = mysqli_real_escape_string($connection, $_POST['pass2']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$emailcheck = filter_var($email, FILTER_VALIDATE_EMAIL);
$emailcheck2 = mysqli_query($connnection, "SELECT * FROM users WHERE email='$email'");
$gender = mysqli_real_escape_string($connection, $_POST['gender']);
$country = mysqli_real_escape_string($connection, $_POST['country']);
// Now for the check functions //
if ($usercheck != 0) {
die("Sorry, your username is already in use! Redirecting...");
};
if ($password != $passwordconfirm) {
die("The passwords do not match! Redirecting...");
};
if ($emailcheck != 0) {
die("That email is already in use! Redirecting...");
};
};
Because you explicitly cancel the submit action. You say onsubmit="return false;". What are you expecting it to do when you explicitly tell it to do nothing?
Also, sidenote, you have a few HTML issues:
<select name="gender"">
<option value=""></option>
<option value="m">Male</option>
<option value"f">Female</option>
should be:
<select name="gender">
<option value=""></option>
<option value="m">Male</option>
<option value="f">Female</option>