PHP POST method not working? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am a complete beginner and am trying to make a form for a project at university. I am just learning php and I am trying to test whether or not I can take the values from my php form and output it before I move on to linking it to my database. This is my full code for my login page, which btw I used bootstrap for some of the frame work. So I have tried to use the POST method and echo out first name and last name to see if it works, and it just wont echo out when I press the submit button. This is really frustrating me. Don't worry about the style, I just want to get the functionality out of the way first, and yes the page is called index.php .
<!doctype html>
<html lang='en'>
<head>
<title>Create Employee Account</title>
<!-- Required meta tags -->
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>
<!-- Bootstrap CSS -->
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css' >
<link rel='stylesheet' href='no_dashboard_css_template.css'>
<link rel='stylesheet' href= ''>
</head>
<body>
<?php
include 'dbconnect.php';
if(isset($POST['submit']))
{
$firstName=$_POST['firstname'];
$lastName=$_POST['lastname'];
$email=$_POST['email'];
$password1=$_POST['password1'];
$password2=$_POST['password2'];
}
echo $firstName;
echo $lastName;
?>
<!-- NAVIGATION BAR -->
<nav class='navbar navbar-expand-lg navbar-light bg-light'>
<img id='logo' src='logo.png'>
<button class='navbar-toggler' type='button' data-toggle='collapse' data-target='#navbarSupportedContent' aria-controls='navbarSupportedContent' aria-expanded='false' aria-label='Toggle navigation'>
<span class='navbar-toggler-icon'></span>
</button>
<div class='collapse navbar-collapse' id='navbarSupportedContent'>
<ul class='navbar-nav mr-auto'>
<li class='nav-item active'>
<a class='nav-link' href='#'>Home/Login<span class='sr-only'>(current)</span></a>
</li>
</ul>
</div>
</nav>
<!-- Main CONTAINER part of the page WHERE MOST PAGE CONTENT WILL GO -->
<div class='container-fluid'>
<div class='row'>
<!-- EMPLOYEE SIGN UP AREA -->
<div class='col-lg-4'>
<h3>Potential Employee Sign Up</h3>
<!-- EMPLOYEE SIGN UP FORM -->
<form action='index.php' method='POST' >
<!-- Title (Mr Mrs etc. -->
<label class='mr-sm-2' for='inlineFormCustomSelectPref'>Title</label>
<select name='title' class='custom-select mb-2 mr-sm-2 mb-sm-0' id='inlineFormCustomSelectPref'>
<option selected>Choose...</option>
<option value='1'>Mr</option>
<option value='2'>Ms</option>
<option value='3'>Mrs</option>
</select>
<br>
<!-- NAMES -->
<div class='row'>
<!-- First Name -->
<div class='col'>
<input type='text' class='form-control' name='firstname' placeholder='First name'>
</div>
<!-- Last Name -->
<div class='col'>
<input type='text' class='form-control' name='lastname' placeholder='Last name'>
</div>
</div>
<!-- Email and Password-->
<div class='form-group'>
<label for='exampleInputEmail1'>Email address</label>
<input type='text' class='form-control' id='exampleInputEmail1' name='email' placeholder='Enter email'>
</div>
<div class='form-group'>
<label for='exampleInputPassword1'>Password:</label>
<input type='password' class='form-control' name='password1' id='exampleInputPassword1' placeholder='Password'>
<label for='exampleInputPassword1'>Enter your Password again:</label>
<input type='password' class='form-control' name='password2' id='exampleInputPassword1' placeholder='Password'>
</div>
<!-- Terms and Conditions-->
<div class='form-check'>
<label class='form-check-label'>
<input type='checkbox' class='form-check-input'>
I have agreed and read the Terms and conditions
</label>
</div>
<!-- Education -->
<!-- University -->
<div class='form-group'>
<label for='exampleFormControlInput1'>University</label>
<input type='text' class='form-control' name='uni' id='exampleFormControlInput1' placeholder='Please Type your University'>
</div>
<!-- Degree Type -->
<label class='mr-sm-2' for='inlineFormCustomSelectPref'>Degree Type</label>
<select class='custom-select mb-2 mr-sm-2 mb-sm-0' name='degtype' id='inlineFormCustomSelectPref'>
<option selected>Choose...</option>
<option value='1'>BEng</option>
<option value='2'>MEng</option>
<option value='3'>MSc</option>
</select>
<br>
<!-- End Button to submit -->
<button type='submit' class='btn btn-primary' name='submit' >Sign Up!</button>
</form>
</div>
</div>
</div>
<!-- EMPLOYEE SIGN UP AREA END -->
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src='https://code.jquery.com/jquery-3.2.1.slim.min.js' ></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js' ></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js' ></script>
</body>
</html>

Your mistake is on this line
if(isset($POST['submit']))
Use it
if(isset($Ů€POST['submit']))

Related

Displaying an error message in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to display an error message to the user if the credentials entered by them are incorrect(do not return a match with anything in my database), such as in a login form.
What is the easiest way to do that?
Here's what I have till now-
<?php
require 'includes/common.php';
?>
<!DOCTYPE html>
<!--
Login page of Lifestyle Store
-->
<html>
<head>
<title>Log In</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!---- External css file index.css placed in the folder css is linked-->
<link href="css/index.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<?php
include 'includes/header.php';
?>
<div class="row login">
<div class="col-xs-4"></div>
<div class="col-xs-4">
<div class="panel panel-primary">
<div class="panel-body">
<p class="text-warning">Login to make a purchase-</p>
<form action="login_submit.php" method="POST">
<div class="form-group">
<label for="email">E-mail:</label>
<input type="text" class="form-control" name="email" value="Email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="text" class="form-control" name="password" value="Password">
</div>
</form>
<button class="btn btn-primary" type="submit" name="submit">Log-in</button>
</div>
<div class="panel-footer">Don't have an account? <a class="a2" href="signup.html">Register!</a></div>
</div>
</div>
<div class="col-xs-4"></div>
</div>
<?php
include 'includes/footer.php';
?>
</body>
If you are using mysql to search the database for matches, then use something like below
$email = $mysqli->escape_string($_POST['email']);
$password = $mysqli->escape_string($_POST['password']):
$result = $mysqli->query("SELECT * FROM users WHERE email='$email' AND password='$password'");
//check whether the user exists and redirecting if not exists
if ($result-> num_rows == 0){
$_SESSION['messege']= "You have entered Either Wrong Username or Password ";
header("location: error.php");
}

Moving comment box on the webpage

My question is rather simple. But I am seriously stack as I have never done moving commentary box and nor applying it before.
Here is the code of a comment box I got from a free source:
<form method='post'>
<div class="name">NAME: <input type='text' name='name' id='name' /><br /></div>
Comment:<br />
<textarea name='comment' id='comment'></textarea><br />
<input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?>' />
<input type='submit' value='Submit' />
</form>
And now I am trying to position it so it fits nicely on my page. I managed to move it on the page by putting the whole piece of code in a div, but when it comes to Name and comment fields even I am not sure what to do. Would be great to see some insights.
I would recommend using Bootstrap to make a clean design of your form.
Add Bootstrap to your Head of your HTML page:
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
Then you can modify your form code to use Bootstrap containers, form-groups and form-control classes to structure and style your code. See here for more details. form-group group label's and input fields together removing the need to create custom css or use html breaks to change the structure of the page. form-control adds Bootstrap styling to the input fields.
I created an example from your code sample above using Bootstrap:
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Example Form</h1>
<form>
<div class="form-group">
<label for="nameInput">Name</label> <input class="form-control" id="nameInput" placeholder="Name" type="text">
</div>
<div class="form-group">
<label for="commentInput">Comment</label>
<textarea class="form-control" id="commentInput"></textarea>
</div>
<input id='articleid' name='articleid' type='hidden' value='<? echo $_GET["id"]; ?>'>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
See code pen to view the form - http://codepen.io/jamesg1/pen/ALAZKg

$_POST not working when JQuery is included

So, I'm having an issue where my $_POST variable does not work whenever I have JQuery included on the page.
If I comment out the JQuery link it works fine. I have asked a similar question before, but did not get a full answer and have used a workaround which does not work with this page.
My full code is as followed:
<?php
if(isset($_POST['home-register-submit']))
{
echo '<script>alert("Works!");</script>';
}
?>
<div id='home-register'>
<div id='home-register-title'><h4>Aanmelden</h4></div>
<hr>
<form method="POST">
<label>Voornaam:</label> <div id='home-register-input'><input type="text" name='home-register-fname'></div><br />
<label>Achternaam:</label> <div id='home-register-input'><input type='text' name='home-register-lname'></div><br />
<label>Leeftijd:</label> <div id='home-register-input'><input type='' name='home-register-age'></div><br />
<label>Telefoon:</label> <div id='home-register-input'><input type='tel' name='home-register-phone'></div><br />
<label>Email:</label> <div id='home-register-input'><input type='email' name='home-register-email'></div><br />
<button class="btn btn-default btn-sm" type='submit' name='home-register-submit'>Versturen</button>
</form>
</div>
<!-- jQuery -->
<script src="js/jquery.js"></script>
Fixed it by excluding a file called clean-blog.min.js which apparently blocked my page from using $_POST.

Form fields have space in them

My form fields have space in them, if I select and delete this space my prefilled text shows back up. I don't know what to make of this and have been trouble shooting all day. Any ideas? Here is my code...
<?php
include '_include/php/header.php';
#set variables to be used in the header
$page_title = "Assesment";
?>
<div class="page" id="subheader">
<div class="container">
<!-- Title Page -->
<div class="row">
<div class="span12">
<div class="title-page">
<h2 class="title">Four simple steps...</h2>
<h3 class="title-description">To dictate what you need
in a website</h3>
</div>
</div>
</div><!-- End Title Page -->
<!-- submenu -->
<?php
include '_include/php/submenu.php'; ?>
</div>
</div><!-- End sub header -->
<!-- main Section -->
<div class="page" id="contact">
<div class="container">
<!-- Title Page -->
<div class="row">
<div class="span12">
<div class="title-page">
<h2 class="title">Assesment - Step One</h2>
<h3 class="title-description">Status: <span class=
"color-text"><?php echo $assessresult; ?></span></h3>
</div>
</div>
</div><!-- End Title Page -->
<!-- main Form -->
<div class="row">
<div class="span9">
<form action="assessment_submitted.php" class="contact-form" id="contact-form"
method="post" name="contact-form">
<h3 class="color-text">What do you wish to get out of
your website?</h3>
<p class="contact-message">
<textarea required cols="10" id="client-goals" name=
"client-goals" placeholder=
"What do you wish to acheive by having a website? ie. creating a web presence, expanding sales by creating an online market, networking, ect."
rows="5">
</textarea></p>
<h3 class="color-text">What key fatures are you looking
for?</h3>
<p class="contact-message">
<textarea required cols="10" id="client-needs" name=
"client-needs" placeholder=
"What key features are you looking to have within your website? ie. storefront, social media intigrtion, a blogging platform, content managment system, ect."
rows="5">
</textarea></p>
<script type = "text/javascript" >
var i = 1;
function addUrl() {
if (i < 3) {
i++;
var p = document.createElement('p');
p.innerHTML =
'<hr><textarea required id="client-needs" placeholder="Add URL #' + i +
'" name="example' + i +
'" rows="1" cols="10"><\/textarea><input type="button" value="Remove added URL" onclick="removeUrl(this)">';
document.getElementById('example').appendChild(p);
} //END if
} //END addUrl function
function removeUrl(textarea) {
textarea.parentNode.parentNode.removeChild(textarea.parentNode);
i--;
} //END removeUrl function
</script>
<h3 class="color-text">Do you have and examples of what
you want?</h3>
<p class="contact-message" id="example">
<textarea cols="10" id="examples" name="example1"
placeholder="Do you know of any website like the one you want? ie. google.com, p5services.com, youtube.com"
rows="1">
</textarea></p><br>
<input onclick="addUrl()" type="button" value=
"Add url example">
<h3 class="color-text">Do you have a budget in
mind?</h3>
<p class="contact-message">
<textarea required cols="10" id="client-budget" name=
"client-budget" placeholder=
"Enter a budget, approximate or exact. This will help us decide if the scope of your project is in line with what you are interested in spending"
rows="2">
</textarea></p>
<h3 class="color-text">Do you have a timeline or
deadline?</h3>
<p class="contact-message">
<!-- <textarea id="client-deadline" placeholder="When do you need this done by?" name="client-deadline" rows="2" cols="10"></textarea> -->
<!-- <input id="client-deadline" name="client-deadline"
type="date"></p> -->
<!-- add a call to action for further services later -->
<h3 class="color-text">Any other comments?</h3>
<p class="contact-message">
<textarea cols="10" id="client-comments" name=
"client-comments" placeholder=
"Don't let us over look anything!" rows="5">
</textarea></p>
<p class="contact-submit"><input name=
"submit_assessment" type="submit" value=
"Save New Record"></p>
<div id="response"></div>
</form>
</div>
<div class="span3">
<div class="contact-details">
<h3><span class="font-icon-info"></span> Assesment
Objective</h3>
<p>The assesment section is used to define your needs
and help us decided what tool we need to impliment in
order to help you reach your goals</p>
<h4 class="color-text">After submitting this you will
recive a quote!</h4>
</div>
</div>
</div><!-- End main Form -->
</div>
</div><!-- End main Section -->
<?php
include '_include/php/footer.php'; ?>
The textarea element takes placeholder text in a different form. You need to put it between the tags like so:
<textarea>My text area placeholder text goes here</textarea>
The space that you are seeing then deleting to see your placeholder is caused by the line break between your opening and closing textarea tags

Adding "widgets" live to a site

I'm currently in the process of creating a website with various widgets that I want to be able to add/remove on the fly.
I'm using jQuery to achieve this(planning to at least).
The simplest widget is the login widget. Which looks like this:
<div id="widget1">
<div class="close"></div>
<div id="widget1_title"></div>
<div id="widget1_content">
<form method='post' action='index.php'>
<div id="widget1_username">
Username<input type='text' maxlength='16' name='user' value='' />
</div>
<div id="widget1_password">
Password<input type='password' maxlength='16' name='pass' value='' />
</div>
<div id="widget1_submit">
<input type='submit' value='LOGIN' />
</div>
</div>
</div>
This is stored in a file called login_widget.html. Since a login-widget is something that should be preloaded on the site(without the user having to bring it up) I have my index.php file looking like this:
<body>
<div id="container">
<div id="content">
<div id="sidebar">
<div id="login-sidebar"></div>
</div>
<div id="header">
<div></div>
<div></div>
<div id="header-logo"></div>
</div>
<div id="main-content">
<div id="widget1">
<div class="close"></div>
<div id="widget1_title"></div>
<div id="widget1_content">
<form method='post' action='index.php'>
<div id="widget1_username">
Username<input type='text' maxlength='16' name='user' value='' />
</div>
<div id="widget1_password">
Password<input type='password' maxlength='16' name='pass' value='' />
</div>
<div id="widget1_submit">
<input type='submit' value='LOGIN' />
</div>
</div>
</div>
</div>
<div id="footer">
</div>
</div>
</div>
</body>
(Originally it's in a <?php include_once('login_widget.html'); ?> ).
I then have a button set up that I press to load in the code from the file.
Here's the jQuery code:
$(document).ready(function(){
$(".close").click(function(){
$(this).parent().remove();
});
$("#login-sidebar").click(function(){
$.ajax({
url: 'login_widget.html'
}).done(function(data) {
$('#main-content').append(data);
});
});
});
And now to the problem. This works in that it posts the html-code and it shows on the site, but the problem is that the "close button" doesn't work anymore. So I can't remove the new created html-elements using the class named "close".
I'm very new to jQuery, but I figure this is one way of doing it, but I'm missing something?
Thanks in advance!
Either build your widgets as modules that bind their own events when they are created to remove themselves, or use event delegation. Event delegation would be the simplest given the very basic code that you are using thus far.
$("#main-content").on("click",".close",function(){
$(this).parent().remove();
})

Categories