PHP Post Method Error - php

I am using Post method in my website. In my index.html file is the following code:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Heater Login</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<form method="post" action="login.php" class="login">
<p>
<label for="login">Username:</label>
<input type="text" name="login" id="login" value="John Appleseed">
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" value="4815162342">
</p>
<p class="login-submit">
<button type="submit" class="login-button">Login</button>
</p>
</form>
</body>
</html>
And in my login.php file is the following code:
<?php
echo($_POST['login']);
echo($_POST['password']);
?>
When I open html file, enter my credentials, click submit button it goes to php file end there's nothing.

Can you change this line of code with the new one Hope it will work
<button type="submit" class="login-button">Login</button>
<input type="submit" name="submit" value="Login">
And last I want that you copy whole code from the stackoverflow and use this code.Some times space may create a problem.

This problem occurs when you directly open from its file location try opening file from server
like if you're using xampp open localhost and then try.

Related

Getting HTML File to connect to files which in turn connect to a mysql database

I need the HTML File below to connect to different PHP Files as these connect to different tables in a database. I have used JAVA Script to submit an action when the button is pressed but no matter what, the file will only connect to the Prepared.php file and never connect to the Prepared2.php file. Ihave tried labeling differently and many other concepts but cannot seem to get the file to work.
Please find code below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> BBC Database search </title>
</head>
<body>
<center>
<h1> BBC Database search </h1>
<script type="text/javascript">
function submitAction(act) {
document.sample.action = act;
document.sample.submit();
}
</script>
<form action="Prepared.php" method="post">
Search Brands: <input type="text" name="term"
onClick="submitAction('Prepared.php')"/><br />
<input type="submit" value="Submit" />
<form action="Prepared2.php" method="post">
Search Available Clips: <input type="text" name="clips"
onClick="submitAction('Prepared2.php')" /><br />
<input type="submit" value="Submit" />
</center>
</form>
The js code is totally unnecessary, and wrong, remove it and the onsubmit.
Then close the first <form> tag with a </form> Currently you have the second form within the first and thats not legal
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> BBC Database search </title>
</head>
<body>
<center>
<h1> BBC Database search </h1>
<form action="Prepared.php" method="post">
Search Brands: <input type="text" name="term" />
<br />
<input type="submit" value="Submit" />
</form>
<form action="Prepared2.php" method="post">
Search Available Clips: <input type="text" name="clips" />
<br />
<input type="submit" value="Submit" />
</form>

html form post gives empty array

I am trying to post data from html form (html code is inside index.php file) into add_student.php file which is supposed to just print $_POST array using print_r function. But there is always empty array on result.
index.php
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<link rel="stylesheet" href="css/font-awesome.css">
</head>
<body>
<div class="contact col-md-12" id="get_in_touch">
<div class="col-md-10 col-md-offset-1">
<div class="contact_title">
<h2>
Add a student
</h2>
</div>
<div class="contact_container">
<div class="contact_form col-md-8">
<form action="add_student.php" method="post">
<input name="student_name" type="text" placeholder="FULL NAME" class="input1"/>
<input name="student_email" type="text" placeholder="E-MAIL" class="input1"/>
<input name="password" type="text" placeholder="PASSWORD" class="input1"/>
<input name="major" type="text" placeholder="MAJOR" class="input1"/>
<input name="group" type="text" placeholder="GROUP NUMBER" class="input1"/>
<input name="submit" type="submit" value="ADD STUDENT"/>
</form>
</div>
</div>
</div>
</div>
</body>
add_student.php:
<?php
print_r($_POST);
?>
Why I always get empty array?
It seems that problem is in PHPStorm localhost port. When you just enter in browser the address of index.php it works perfectly. However when I run code from IDE it returns empty array.
Perhaps it will work if you use an include as the following:
include_once add_student.php;
And then use index.php as the form action. IF you later want to proceed to another page, add a header <url> at the bottom of the included script.
Make sure you check if the submit button has been pressed in the add_student.php script isset($_POST['submit'])
Data might get lost in the travel from to page

Do I have to write PHP code in my HTML documents AND include it on EVERY page or can I use an external file just like css/js?

Is this the way to write your PHP? I would like to know if I can use an external file to keep things seperated, as I like to be organised. Also do I need to store my entire content in the XAMPP folder in Windows(C:) or just the PHP files? Haven't been able to find a decent answer on these questions. Help greatly appreciated.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="pagestyling.css">
</head>
<body>
<?php
include('/path/to/file.php');
?>
//scripts go here
</body>
</html>
EDIT EXAMPLE:
<?php
//form validation code here
?>
<div id="content">
<form class="applyform" method="POST" action="">
<h3>Please fill in your application below.</h3>
<p>
<label>In-game username:</label>
<input name="username" type="text"></input>
<h6>*Including CAPITALISED letters (if you have it)</h6>
</p>
<p>
<label>Irl Nickname:</label>
<input name="nickname" type="text"></input>
<h6>(So we know what to call you)</h6>
</p>
<p>
<label>Game:</label>
<select name="selectgame">
<option value="Heroes of Atlan">Heroes of Atlan
<option value="Knights and Dragons">Knights and Dragons
</select>
<h6>(What game do you want to join us in?)</h6>
</p>
<p>
<label>Email Address:</label>
<input name="emailaddress" type="text"></input>
<h6>*Used for GroupMe and confirmation email.</h6>
</p>
<p>
<label>Level:</label>
<input name="charlevel" type="text"></input>
<h6>(Your main character level)</h6>
</p>
<p>
<label>Arena rank:</label>
<input name="arenarank" type="text"></input>
<h6>(E.g. Heroes of Atlan: (1) 3358 or K&ampD: RIBBON E)</h6>
</p>
<p>
<label>Referral code:</label>
<input name="refcode" type="text"></input>
<h6>(Knights and Dragons only, e.g. "XU5-CP3-SK9")</h6>
</p>
<h5>Please check that the information that you have entered is correct before proceeding.</h5>
<input id="submitbutton" name="applySubmit" type="submit" value="Submit"></input>
</form>
</div> <!-- end of content -->
make a php file like this
include_file.php
<?php
//some php code
?>
some html here
now you can include it in every page you require. like this
<?php
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="pagestyling.css">
</head>
<body>
<?php
include("include_file.php");
?>
</body>
</html>
you can also use include, require, require_once or include_once as per your requirement.
and learn tutorial
Usualy you would write for exempe the header in the seperate header.php file and then include it in your main file:
<?php include('header.php'); ?>

Beginner: IF statement/PHP/HTML issue

I do apologise in advance for my lack of knowledge in PHP and HTML. I have scoured the internet for 3/4 days trying to create what is probably a simple system. I need the user to enter a digit before the next page is loaded. I have an IF statement on my page.
1.php
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<div id="container">
<div id="authorise">
<form action="2.php" method="post">
<!--Product Comment Box--><br>
<p>Please enter your 4<br> digit authorisation code:<br> <br>
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
</div>
</div>
2.php
<?php
if ($_POST['digits'] === '210392')
{echo 'https://www.facebook.com/'
?>
But I need a form first which the user would input the code '210392' and press submit. Then the if statement can take place. I know how to do a form but dont know how to assign that form a variable name 'digits'. All help is appreciated.
A basic form:
<form action="your_file.php" method="post">
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
Then in your_file.php:
<?php
if ($_POST['digits'] === '210392') {
// etc.
$_POST is a superglobal array that has all data POSTed to your script. digits in this case is an index in that array. It comes from the name of the input field.
<form action="check.php" method="POST">
<input type="text" name="digits" />
<input type="submit" value="click here" />
</form>
//in check.php
if (isset($_POST['digits']) and $_POST['digits'] == 12345){
header("Location: http://google.com");
}
else {
echo " error !!";
}
study html from w3school
http://www.w3schools.com/html/html_forms.asp
and php
http://www.w3schools.com/PhP/php_forms.asp
<form action="https://scm-intranet.tees.ac.uk/users/l1071039/bobbin/admin.php" method="post">
Password : <input type="password" name="pwd" />
<input type="submit" name="send">
</form>
// admin.php
<?php
if( isset($_POST['pwd']) ){
$pwd = $_POST['pwd'];
}
?>
You first need to create an input with the very important name attribute which you'll use later.
<form method="post">
<input type="password" name="mypassword" />
<button type="submit">Go</button>
</form>
PHP:
//Post method access with $_POST variable:
if ($_POST['mypassword'] == 'my-password') { //Oh, you should probably use SSH and hash the password
//Hooray!
}
Instead of echo, use header.
<?php
if ($_POST['digits'] === '210392')
{
header (loaction:https://www.facebook.com/);
}
?>

php/jquery-mobile: Second page not running validation on two page form

I'm new to php and am trying to make a very simple two page form. Any help or guidance would be very appreciated!
Problem:
The second page does not run error validation when coming to it from the first page.
Both pages run error validation correctly URL's entered directly
Setup:
Page 1 one is HTML. It POSTS to Page2.
Page 2 is php and the data from Page 1 is stored in an input field
Live example:
http://www.linplusg.com/page1.html
In IE the Page 2 URL after coming from Page 1 looks to be incorrect:
http://www.linplusg.com/page1.html#/page2.php
In FF and Chrome the URL looks fine but I think there's a flicker/refresh happening.
Does something else happen besides just opening the page when doing a POST to a page? Does something value get stored the new fields? Am I doing the form action wrong? Dazed and confused...
Thank you so much for any help or suggestions. I've been searching around for answers all night and my brain feels like jello. It seems like I'm missing something simple?!
Page1 Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
</script>
<script>
$(document).ready(function(){
$("#initialForm").validate();
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-role="content">
<form id="initialForm" method="post" action="page2.php">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput3">
Zip Code
</label>
<input name="zip" id="zip" maxlength=5 value="" type="text" class="required" />
</fieldset>
</div>
<input type="submit" data-theme="e" value="Submit" />
</form>
</div>
</div>
</body>
</html>
Page 2 code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
</script>
<script>
$(document).ready(function(){
$("#fullForm").validate();
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-role="content">
<form id="fullForm" method="get" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<input name="zip" id="zip" value="<?php echo $_POST["zip"]; ?>" type="" />
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput3">
First Name
</label>
<input name="first_name" id="first_name" placeholder="" value="" type="text" class="required"/>
</fieldset>
</div>
<input type="submit" data-theme="e" value="Submit" />
</form>
</div>
</div>
</body>
</html>
To prevent this you need to specify data-ajax="false" in the form element in page1.
<form id="initialForm" method="post" action="page2.php" data-ajax="false">
see http://jquerymobile.com/test/docs/forms/forms-sample.html for more information.
Without the PHP code, I can just guess, but maybe it is because in the second form you have a method="get" instead of method="post", and in PHP you check the $_POST variable?
You could preserve the ajax transitions and still have your javascript execute on your second page by moving your script inside the div with the data-role "page".
See this answer here: https://stackoverflow.com/a/6428127/2066267

Categories