Embedded PHP ignoring conditional statements [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 6 years ago.
I am trying to conditionally create a fieldset in the top right of my website that will either prompt the user to login, or show their username and give them the option to logout.
I am using embedded php within my html document in order to achieve this. Unfortunately, both fieldsets are being displayed on my webpage. Can someone please explain to me what I am doing wrong? Thank you!
<div id="loginForm">
<?php
session_start();
if(isset($_SESSION['username'])){ ?>
<form action="endsession.php" method="post">
<fieldset width="100">
<legend>Welcome</legend>
<h3></h3>
<input type="submit" value="Logout"></input>
</fieldset>
</form>
<?php } else{ ?>
<form action="connection.php" method="post">
<fieldset width="100">
<legend>Login</legend>
<input type="hidden" name="submitted" id="submitted" value="1"/>
<label for="username">Username:</label>
<input id="username" name="username" type="text" ><br><br>
<label for="password">Password:</label>
<input id="password" name="password" type="password"/><br><br>
<input type="submit" value="Sign in"></input>
</fieldset>
</form>
<?php } ?>
</div>

Put session_start(); at the top of your page before any other code; just after PHP start tag
<?php
session_start();
<div id="loginForm">
....
and so on...
Hope it fixes the problem.

Related

How to insert PHP value into HTML form? [duplicate]

This question already has answers here:
PHP not rendering in HTML
(1 answer)
PHP script not working in HTML file
(6 answers)
PHP inside HTML doesn't work
(5 answers)
Closed 1 year ago.
My code is
<form action = "insert.php" method = "POST">
<div class="title-block">
<input type="text" name="rater_full_name" />
</div>
<div class="title-block">
<input type="text" name="rater_company_name" required />
</div>
<div class="title-block">
<input type="text" name="rater_ref" />
</div>
<div class="title-para">
<textarea rows="5" name="rater_suggestion"></textarea>
</div <div align="left"><div class="btn-block">
<input type="hidden" name="PID" value="<?php echo $PID; ?>"> <button type="submit" name="submit" value="submit">Submit Feedback</button>
</div>
</form>
It's not storing the variable $PID value, it is storing complete code inside quotations("") Please suggest best way to do so..
Thanks in advance.
Save the file as .php rather than .html

localhost does not load php [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 5 years ago.
I am trying to load my start page (php) on localhost with xampp. As long as I keep the file an html document (saved as start.html) it is showing up but as soon as I save it as a php file (in order to execute the $_SESSION['id'] it fails to load the file (basically an infinite loading process but no errors showing up. Here is my code. Any help is highly appreciated. Thanks
<?php
session_start();?>
<html>
<head>
<title>
I am a login project
</title>
<meta charset="utf-8">
<link rel="stylesheet" href='style.css' type="text/css">
</head>
<body>
<form method="post" action="login.php">
<input type="text" placeholder="uid" name="Username">
<input type="password" placeholder="Password" name="pwd">
<button type="submit" name="submit">LOGIN</button>
</form>
<br>
<?php
if(isset($_SESSION['id'])){
echo $_SESSION['id'];
}
?>
<form method="post" action="signup.php">
<input type="text" placeholder="first" name="first">
<input type="text" placeholder="last" name="last">
<input type="text" placeholder="uid" name="Username">
<input type="password" placeholder="Password" name="pwd">
<button type="submit" name="submit">SIGN_UP</button>
</form>
<br>
<form method="post" action="logout.php">
<button type="submit" name='logout'>LOGOUT</button>
</form>
</body>
</html>
session start should happen before any output. there is a space before your opening php tag

Some Error in Php code [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
first file is index.php
<?php include('loginform.php');?>
second file is loginform.php
<?php
<html>
<body>
<form action="" method="post">
<label>Username</label>
<input type="text" name="username"><br>
<label>Password></label>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="login">
</form>
Register here
</body>
</html>
?>
third file is register.php
<html>
<body>
<form action="" method="post">
<label>FirstName</label>
<input type="text" name="firstname"><br>
<label>LastName</label>
<input type="text" name="lastname"><br>
<label>Email</label>
<input type="email" name="email"><br>
<label>UserName</label>
<input type="text" name="username"><br>
<label>Password</label>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>
I have three files in the project first file is the index file which is shown whenever the user opens the website It contains two things login form and a link
ie register here.So i have wrapped all this in one single php file loginform.php
and included it in my index.php file so when I execute this in my localhost web server it shows a error like this
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\cms\loginform.php on line 2
How can I tackle this problem please tell me?
The loginform.php file isn't a PHP source file, it's just an HTML file. Remove the enclosing <?php and ?> from it and you should be OK.

how to submit a form to itself by using post method in php? [duplicate]

This question already has answers here:
How do I make a PHP form that submits to self?
(6 answers)
Closed 6 years ago.
I am trying to submit a form by post method on itself. but every time the form submits and page is refreshed. it doesn't show values.
<?php echo $_POST['fname']; ?>
<form method="POST" action='#.php'>
<input type="text" name="fname" id="fname" />
<button id="check" name="check" type="submit">GO</button>
</form>
What's the point, i am missing?
Try this :
<?php echo $_POST['fname']; ?>
<form method="POST" action=""> <!-- not single quote -->
<input type="text" name="fname" id="fname" />
<input type="submit" name="value" >
</form>
Try this...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<pre>";
print_r($_POST);
}
?>
<form name="test" action="" method="post"><input type="text" name="firstName" /><input type="submit" name="Submit" /></form>
I would recommend to write more cleaner way. like this:
<?php
if(isset($_POST['fname']) && !empty($_POST['fname']) ){
echo $_POST['fname'];
}
?>
<form method="POST" action=""> <!-- not single quote -->
<input type="text" name="fname" id="fname" />
<input type="submit" name="value" >
</form>

method post in <a href> method post [duplicate]

This question already has answers here:
Make a link use POST instead of GET
(11 answers)
Closed 7 years ago.
Is there a way to replicate this in <a href="blah.php">?
<form action="http://localhost/php/suburb_added.php" method="post">
<b>Add a New Suburb</b>
<p>Name:
<input type="text" name="suburb" size="30" value="" />
<input type="submit" id="submit" value="Submit" name="submit" />
</p>
</form>
in suburb_added.php... i have this to capture
if (!empty($_POST['suburb']))
To a table form....
<td align="left"><a href="suburb_added.php"><?php echo $row['id'];?></td>
how to create the items below from a table? The goal is when I click the result from <?php echo $row['id'];?>, I should be able to get the value of "id" and process it in suburb_added.php using similar to if (!empty($_POST['suburb']))
<form action="http://localhost/php/suburb_added.php" method="post">
<input type="text" name="suburb" size="30" value="" />
what do you whant i don't understand?? I can help you
<?php
if(!isset($_POST['submit'])){
?>
<form action="" method="post" name="submit">
<b>Add a New Suburb</b>
<p>Name:
<input type="text" name="suburb" size="30" value="" />
<input type="submit" id="submit" value="Submit" name="submit" />
</p>
</form>
<?php
} else {
//paste here your code from http://localhost/php/suburb_added.php
echo "you doing post in this page.";
}
?>
<!--Try using header
like this:-->
if($_POST)
{
header('location:login-form.php');
}
else
{
echo "";
}
Change the PHP script so it uses $_REQUEST instead of $_POST. This variable combines the contents of $_POST and $_GET. Then you can have a link like
<?php echo $row['id'] ?>

Categories