$_POST method PHP issue - php

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.

Related

PHP Submit button doesn't have any effect (PhpStorm)

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

PHP: HTML Form disappears after submit

I've got a simple <form> in myFunction, as you can see here:
<?php
function myFunction()
{
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
if (isset($_POST['postform']))
{
echo "I'm working!";
}
}
?>
When I call this function, I can see the form but, when I submit it (by clicking the submit button), it disappears. How can I solve this problem?
Here is the full code:
<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
</div>
";
if (isset($_POST['first']))
{
myFunction();
}
?>
<?php
function myFunction()
{
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
if (isset($_POST['postform']))
{
echo "I'm working!";
}
}
?>
The issue is that $_POST superglobal is purged on new request, just like when you're navigating through pages, which is quite natural.
So, if one comes to a page with, let's say, $_POST = ['first' => ''], and then he submits a post form (or any form) ['postform' => 'send'], the resulting $_POST would be ['postform' => 'send'].
So, in your case the easiest solution would be either to follow Shailesh's answer or submit the first form with method='get' and, of course, then you'll have to change $_POST['first'] to $_GET['first'].
But a better solution would be to pass some 'step' parameter in request on each step, so you'll have <input type="hidden" name="step" value="1">. And then, depending on a step variable, do some stuff.
Also check out $_SESSION.
Cheers!
It disappears because you don't get to call the function myFunction() itself. The second form does not include the field "first".
If you want it to work "as is", include this in myFunction() code:
function myFunction()
{
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='hidden' value='send' name='first'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
if (isset($_POST['postform']))
{
echo "I'm working!";
}
}
?>
The only change is
<input type='hidden' value='send' name='first'>
which makes the form visible again. Anyway, you should rethink this whole code.
if (isset($_POST['first']))
{
myFunction();
}
Replace with:
if (isset($_POST['first']) || isset($_POST['postform']) )
{
myFunction();
}
Move your postform form processing out of the myFunction function definition and call myFunction function from there. Here's the complete code,
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
";
if (isset($_POST['first'])){
myFunction();
}
?>
<?php
function myFunction(){
echo "
<form method='post'>
<fieldset>
<legend>Something</legend>
<input type='text'>
<input type='submit' value='send' name='postform'>
</fieldset>
</form>
";
}
if (isset($_POST['postform'])){
myFunction();
echo "I'm working!";
}
?>
</body>
</html>
write the same as two different documents.
<html>
<body>
<form method="post" action="submit.php">
<fieldset>
<legend>Something</legend>
<input type="text" name="input">
<input type='submit' value='send' name="postform">
</fieldset>
</form>
now write the following in submit.php
<?php
$input=$_POST['input'];
if ($input!="")
{
echo "I'm working!";
}
?>
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
echo "<br>You can use the following form again to enter a new name.";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Change this place on this code:
<?php
echo "
<form method='post'>
<button name='first'>First step</button>
</form>
</div>
";
if (isset($_POST['first']) or isset($_POST['postform']))
{
myFunction();
}
?>

why php post global variable iis not working

Faculty's server's $_post[] global variable doesnt work.Is there anybody knows the reason why is not working?
<form action="" method="POST">
<input name="asd" type="text" value="mesaj">
<input type="submit" name="eray" >
</form>
<?php
echo $_SERVER['REQUEST_METHOD'] ;
var_dump($_POST["asd"]);
echo $_POST["asd"]; ?>
I recieved null and posts in the result of this example.
I also want to share phpinfo but it s forbiden.
echo error_reporting(E_ALL); result is 22519
BTW get global is certainly working.
if
$_POST["asd"]="Working !!";
echo $_POST["asd"];
i recieved "Working !!" i dont understand. i think form doesnot submitted
Var dump is not echo-able
remove the echo and retry like this :
var_dump($_POST["asd"]);
have a good day !
The problem is
echo var_dump($_POST["asd"]);
which produces an error because
echo var_dump
is wrong/doesn't exist.
Needs to be
var_dump($_POST["asd"]);
only
Then check your php.ini for those lines:
post_max_size = 8M
variables_order = "EGPCS"
and check if those modules are running
RadCompression
RadUploadModule
you should also set this up a bit better so you dont potentially confuse yourself by looking at an empty $_POST array before you submit the form
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
print_r($_POST);
} else {
?>
<form action="" method="POST"> <input name="asd" type="text" value="mesaj">
<input type="submit" name="eray" > </form>
<?php
}
or test this
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
print_r($_REQUEST);
} else {
?>
<form action="" method="POST"> <input name="asd" type="text" value="mesaj">
<input type="submit" name="eray" > </form>
<?php
}
or this
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
print_r($_GET);
print_r($_REQUEST);
} else {
?>
<form action="" method="GET"> <input name="asd" type="text" value="mesaj">
<input type="submit" name="eray" > </form>
<?php
}
?>

I would like generate page in steps; form1 generates form2, form2 generates the form3. Is this possible?

I would like generate page in steps; form1 generates form2, form2 generates the form3.
Is this possible?
<!DOCTYPE html><html><head><title> some </title></head><body>
<form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> ">
print "in form1";
<input type="submit" id="fsi1" value="fsv1" name="fsn1"> <br>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST['fsn1'])) {
print '<form name="form2" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> "> echo "in form2"; </form><br>';
print '<input type="submit" id="fsi2" value="fsv2" name="fsn2"> <br>';
}
if (!empty($_POST['fsn2'])) {
print '<form name="form3" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> "> echo "in form3"; </form><br>';
print '<input type="submit" id="fsi3" value="fsv3" name="fsn3"> <br>';
}
}
?>
</body></html>
It seems i generate wrongly.
First the file should be named with *.php extension, not html
Second, i use wrong syntax, i should not echo HTML lines, i should write in HTML and break php appart whenever it is needed.This syntax works:
<!DOCTYPE HTML >
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>
<form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> ">
print "in form1";
<input type="submit" id="fsi1" value="fsv1" name="fsn1"> <br>
</form>
<?php if($_SERVER["REQUEST_METHOD"] == "POST") { if(!empty($_POST['fsn1'])) { ?>
<form name="form2" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> ">
<?php echo "in form2"; ?>
<input type="submit" id="fsi2" value="fsv2" name="fsn2">
</form><br>;
<?php }} ?>
</body>
</html>
The best example, which claims it is working is:
Form generator in php

Why does it always echo the "Not set!" though I enter data?

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!

Categories