How to print input with php code - php

I just start learning php and html and I wanted to make something that gets input and print out the output with php code. However, with the code I have, it lets me input, but not printing anything when I click submit. Why?
<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit value="Submit" />
</form>
<?php
if (isset($_POST['submit'])) { //to check if the form was submitted
$username= $_POST['username'];
echo $username;
}
?>
</body>
<html>

In the below line in your code
<input type="submit" name="submit value="Submit" />
the word name = "submit does not have a closing double colon

Your HTML is missing a quotation mark on the line where you have
<input type="submit" name="submit value="Submit" />
it should be
<input type="submit" name="submit" value="Submit" />

you missed the quotes in button name
<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit" value="Submit" />// you missed here
</form>
<?php
if (isset($_POST['submit'])) { //to check if the form was submitted
$username= $_POST['username'];
echo $username;
}
?>
</body>
<html>

There is a syntax error you have missed the double quotes name="submit"
<?php
if (isset($_POST['submit'])) { //to check if the form was submitted
// For printing whole post data
echo '<pre>'; print_r($_POST);
$username= $_POST['username'];
echo $username;
}
?>
You can also use var_dump for printing out.
Hope this will help you.

are you sure you run the page via php interpreter?
in local you can use xampp or wampp(product by windows)
in mac you can use mampp

Related

Php code gets a blank page

this code is giving back a blank page in browser. Can you spot any mistake? permissions and url checked. The phpcodechecker.com says it's allright
<?php
# Check for POST login data, else set initial values
if (isset($_POST["user"])) {
$user=$_POST['user'];
$pass=hash('sha256',$_POST['pass']);
}
else {
$user="";
$pass="";
}
# Check Login Data
#
# Password is hashed (SHA256). In this case it is 'admin'.
if($user == "admin"
&& $pass == "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")
{
# Load content from local storage
include("../localstorage/content.html");
}
else
{
# Show login form. Request for username and password
echo
'<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>'
}
?>
(I downloaded it from http://www.canbike.org/information-technology/2014/02/05/php-simple-password-protection-with-session-timeout.html and i'm concious of its lack of safety)
use echo statement to print the html code as follows.
echo '<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>';
and remove the in between php tags, use only outer php tags at the beginning and end.
Shortcut open tag used but not allowed on third line up from bottom?
<?
I am late for this question but anyway i want to answer this question.
First of all, if you are new in PHP then You should change error_reporting line in your php.ini as follows:
error_reporting=E_ALL
because this setting display Parse Error instead of Black Page if there is any parse error.
Solution of your question:
There is a Syntax Error in last 'echo' statement of your script which is generating Parse Error.
You can remove this syntax error by adding semicolon(;) in last echo statement as follow:
else{
echo
'<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>';
}
?>
You can use docstring to write html in php files.
<<<EOD
<html>
<body>
<form method="POST" action="">
Username: <input type="text" name="user"><br/>
Password: <input type="password" name="pass"><br/>
<input type="submit" name="submit" value="Login">
</form>
</body>
</html>
EOD;

Problems with $_POST

I'm having problems passing data from a form, I can't figure out where I've gone wrong. I have modeled my page on one that works but mine does not function.
I want to check I have the general structure right. I have stripped it right down and it still wont work.
<?php
define('INCLUDE_CHECK',true);
include 'php/functions.php';
error_reporting(E_ALL);
ini_set('display_errors',1);
logThis('ready!');
if (isset($_POST['submit'])) {
logThis('success');
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>
This is the whole page now, I have commented everything else out.
logThis() works i get the 'ready!' in the log but no 'success' message when ever i press the submit button.
Sorry for my bad english!
In PHP, $_POST expect the name of input field, not the id.
So you need to set names to your fields:
<input type="hidden" id="villainClass" name="villainClass" value="" />
<input type="hidden" id="heroClass" name="heroClass" value="" />
Modify your HTML code
You can get posted data only by the name of input field
<?php
if(isset($_POST["username"]))
{
$username = $_POST["username"];
echo $username;
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="text" name="username"/>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>

PHP $_POST error Please Help me I am learning PHP

I am learning PHP. Here is the source code.
<?php
$text = $_POST['text'];
echo $text;
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
Here is the result. I don't know where is the problem.
Notice: Undefined index: text in C:\xampp\htdocs\faisal\index.php on line 2
It means there's nothing in $_POST['text'] -- and there won't be, until after the form is submitted. You need to use isset() to check:
<?php
if(isset($_POST['text'])) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
When you first go to the page your special variable "$_POST" is empty, that is why you are getting an error. You need to check to see if anything is in there.
<?php
$text = '';
if(isset($_POST['text']))
{
$text = $_POST['text'];
}
echo 'The value of text is: '. $text;
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
$_POST['text'] is only populated when the form is submitted. So when the page is first load it does not exist and you get that error. To compensate you need to check to see f the form is submitted before executing the rest of your PHP:
<?php
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
You pprobably have to detemine if the form has been submitted or not.
<?php
if (isset($_POST['text'])) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
Alternativly you can use $_SERVER['REQUEST_METHOD'].
if ($_SERVER['REQUEST_METHOD'] == 'POST') {...
We have to check whether user clicked on submit button or not, if yes then we have to set $test variable. If we will not use isset() method, we'll always get error.
<?php
if(isset($_POST['submit']))
{
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit">
</form>

Nothing happens on Sign In page

I am trying to make a login page for my website, but when I press the login button, nothing happens. Help Please!
Here is the code i'm using for the login page (This is just a snippet, yes I do have the basic HTML tags)
<center><form action="login.php" method="post"></form></center>
<br>
<center><input type="text" name="username"></center>
<br>
<center><input type="password" name="password"></center>
<br>
<center><input type="submit" name="button" value="Login"></center>
This is login.php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
include ("connect.php");
if($username && $password)
{
//info is provided
}
else
{
echo "You did not provide the proper information needed for login.";
include ("signin.html");
}
?>
this is connect.php
<?php
$connect = mysql_connect("*****", "*****", "*****");
mysql_select_db("******");
?>
It's because you have a the closing </form> tag immediately after the opening tag effectively closing the form and making the rest of it invalid and inoperable. Move it to the end of your form and it will work.
Try Updated code
<center><form action="login.php" method="post">
<br>
<center><input type="text" name="username"></center>
<br>
<center><input type="password" name="password"></center>
<br>
<center><input type="submit" name="button" value="Login"></center>
</form></center>
Kindly make changes in your template.It should be like this...
<center><form action="login.php" method="post">
<br>
<center><input type="text" name="username"></center>
<br>
<center><input type="password" name="password"></center>
<br>
<center><input type="submit" name="button" value="Login"></center>
</form></center>

How do I multiply values in PHP?

I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>

Categories