Though I enter data and hit Submit it always echoes the else part always. I know it isn't the type of question to be asked on Stackoverflow but...
<html>
<head>
<title>Sticky Form</title>
</head>
<body>
<form method="POST" action=<?php echo $_SERVER['PHP_SELF'] ?>>
<label for="Name">Name</label>
<input type="text" name="FName">
<input type="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['FName'];
echo "$f_name";
}
else
{
echo "Not set!";
}
?>
</body>
</html>
Change this:
<input type="submit">
to
<input type="submit" name="submit">
P.S: key name in global arrays comes from users input ($POST,$_GET,$_COOKIE), if you want to change its key, you need to change that element's name!
Related
I have a problem with the following php code. I am trying to submit password, while submission is successful the echo part of php to be displayed on webpage but am not getting any error or response.
<html>
<head>
<title>POST METHOD</title>
</head>
<body>
<form action="login.php" method="post">
Please enter your password:<br>
<input type="password" name="pwd" value="password"><br><br>
<input type="submit" name="Submit">
</form>
</body>
</html>
<?php
$password='password';
if(isset($_POST['password']) &&!empty($_POST['password'])){
echo 'submtted and filled';
}
?>
You are using wrong name for post: you need to use pwd instead of password
if(isset($_POST['pwd']) && !empty($_POST['pwd'])){
echo 'submtted and filled';
}
Hi you need to use name in post metho, to get value of any text/other fields. But in your code you are using type(password) that's why your password not coming in server side. You can use below code, I hope it may works
<html>
<head>
<title>POST METHOD</title>
</head>
<body>
<form action="login.php" method="post">
Please enter your password:<br>
<input type="password" name="pwd" value="password"><br><br>
<input type="submit" name="Submit">
</form>
</body>
<?php
if(isset($_POST['pwd']) && $_POST['pwd'] !='')
{
echo 'submtted and filled';
}
else
{
echo 'Something went wrong please try again';
}
?>
I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question
Thanks in advance for your help. I've searched a lot before posting this but I end up more confused than when I started :)
I'm trying to have one page contain the form fields and after pressing submit, the resulting story with user's form field entries inserted into the story.
It would be great to have the text from the form fields remain so that the user doesn't need to retype everything if they need to change a word or two.
I really appreciate your help. Hopefully this will help many people at once.
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php if (isset($_POST['action']) && $_POST['action'] == "show"): ?>
<p>Hello, I am a <?php echo $_POST['adj'] ?> computer that owns a <?php echo $_POST['noun'] ?>.</p>
<?php else : ?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj"></p>
**strong text** <p>A noun: <input type="text" name="noun"></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php endif ?>
</body>
</html>
As you said you don't want to "keep it simple", you may simply add the needed value attribute to each of your <input>s, like this:
<html>
<head>
<title>My MadLib</title>
</head>
<body>
<h1>MadLib</h1>
<?php
if (isset($_POST['action']) && $_POST['action'] == "show") {
?>
<p>Hello, I am a <?php echo #$_POST['adj']; ?> computer that owns a <?php echo #$_POST['noun']; ?>.</p>
<?php
} else {
?>
<form action="madlib.php" method="post">
<input type="hidden" name="action" value="show">
<p>An adjective: <input type="text" name="adj" value="<?php echo #$_POST['adj']"; ?> /></p>
**strong text**
<p>A noun: <input type="text" name="noun" value="<?php echo #$_POST['noun']"; ?> /></p>
<p><input type="submit" value="Go!"></p>
</form>
<?php
}
?>
</body>
</html>
Note the (sometimes unloved) "#" to prevent firing a notice when $_POST['...'] doesn't exist yet. I also added the same in your <p>Hello... line.
I'm currently working through a POST method problem in PHP. I'm given the following code:
<html>
<body>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
First Name: <input type="text" name="firstname"><br>
<input type="submit">
</form>
</body>
</html>
Within the PHP tags I am supposed to write PHP code that will check if a post request method has been used to access the page, and if so, print either "correct" or "incorrect" if the first name entered is equal to "John".
Here is what I have so far:
<html>
<body>
<?
if(isset($_POST['firstname'])) {
echo 'Correct';
}
if(!isset($_POST['firstname'])) {
echo 'Incorrect';
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
First Name: <input type="text" name="firstname"><br>
<input type="submit">
</form>
</body>
</html>
I'm just not quite sure how to incorporate the "John" condition and if I am on the right track. Thanks in advance.
Just use a basic comparison operator like ==:
<html>
<body>
<?
if(isset($_POST['firstname']) && $_POST['firstname'] == 'John') {
echo 'Correct';
}
else {
echo 'Incorrect';
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
First Name: <input type="text" name="firstname"><br>
<input type="submit">
</form>
</body>
</html>
A slightly better way to only do the check if the form is submitted. You can check to see if the $_SERVER superglobal has a key REQUEST_METHOD with a value of POST.
<html>
<body>
<?
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['firstname']) && $_POST['firstname'] == 'John') {
echo 'Correct';
}
else {
echo 'Incorrect';
}
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
First Name: <input type="text" name="firstname"><br>
<input type="submit">
</form>
</body>
</html>
This is very basic PHP. I suggest learning more about PHP and programming before going any further in your project.
I am currently learning the most basic PHP ever. I have 5 files.
index.php:
<html>
<head>
<title>Budget Calcule</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h2>Put in your: - </h2>
<form action="functions.php" method="post">
<h3>Income</h3>
<label>Salary: <input name="salary" type="text" /></label><br />
<h3>Outgoings</h3>
<label>Living: <input name="living" type="text" /></label><br />
<label>Insurance: <input name="insurance" type="text" /></label><br />
<label>Communication: <input name="communication" type="text" /></label><br />
<label>Loan: <input name="loan" type="text" /></label><br />
<label>Food & Drink: <input name="foodAndDrink" type="text" /></label><br />
<label>Entertaintment / Shopping: <input name="entertainmentOrShopping" type="text" /></label><br />
<label>Transport: <input name="transport" type="text" /></label><br />
<label>Other: <input name="other" type="text" /></label><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
this is my functions.php:
<?php
include('variables.php');
if(!($_POST['Submit'])){
if(isset($_POST['salary'])){
header('Location: output.php');
return $_POST['lon'];
}else{
echo "All fields are required";
}
}
?>
this is my variables.php:
<?php
$salary= $_POST['salary'];
$living= $_POST['living'];
$insurance= $_POST['insurance'];
$communication = $_POST['communication'];
$loan = $_POST['loan'];
$food = $_POST['food'];
$entertaintmentOrShopping = $_POST['entertaintmentOrShopping'];
$transport = $_POST['transport'];
$other= $_POST['other'];
?>
this is my output.php file:
<?php
include('outputFunction.php');
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
and last but not least, this is my outputFunction.php file:
<?php
include('variables.php');
function myText(){
echo "Your salary per month is: " . $_POST['salary'];
}
?>
Now you're thinking "why have he split up his code in different files?" Well first of all, I split the variables from functions.php because I wanted outputFunctions.php to get the variables from variables.php so i could echo my `$_POST['salary']; . The function myText(); outputs the text just fine, but it doesnt output the $_POST['salary'];.
I do not know why it doesnt work, I just wonder if you could be my extra eyes and see if I've done some mistake.
PS! Don't down vote my question just because you think it's stupid. I am having problem with this issue and been working on it for hours without advancing anywhere.
A few things:
You don't need to include a variables.php file. The variables you're accessing are global and you're just creating duplicates that aren't being used. They also go away after the page changes since you're re-declaring them each page load.
You are also trying to call a variable that doesn't exist when you reference $_POST['lon'] instead of 'loan'.
And finally to actually answer your question:
Your myText() function is referencing a variable that is not there anymore.
You need to merge functions.php and outputFunction.php and output.php into one file so the variables aren't lost and all the processing is done without opening a new file each time. I can see your original concept for separated files but an output file is going to be the file to process the input data from the form.
Now in your newly merged output.php, you should have something resembling this:
<html>
<head>
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
echo "Your salary per month is: " . $_POST['salary'];
}
} else {
echo "All fields required.";
}
?>
</body>
</html>
This means only two files - your form page and this page.
A few more tips:
If you want to check if the form was submitted, it has look something like this:
if(isset($_POST['Submit'])){ ... }
Also, you should add a name="" attribute to your submit-Button:
<input type="submit" name="Submit" value="Submit" />
And what is the variables.php for? You don't use any of those variables.
When you redirect the user via header() the data that is stored in the $_POST array gets lost.
You could directly redirect to ouput.php
<form action="output.php" method="post">
And do something like this:
<?php
include('outputFunction.php');
if(isset($_POST['Submit'])) {
if(isset($_POST['salary'])) {
?>
<html>
<head>
<title>Output.php</title>
</head>
<body>
<?php myText(); ?>
</body>
</html>
<?php
} else {
echo "All field required";
}
}
?>
By the way you can always check what your $_POST contains with print_r($_POST);
This can be very useful for debugging.