PHP: HTML Form disappears after submit - php

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();
}
?>

Related

Undefined array key when trying to get a value out of a textbox

im currently trying to get a value from a textbox but im always getting undefined array key.
<head>
<!-- Links -->
<link rel="stylesheet" href="./css/vouches.css">
</head>
<body>
<div class="content">
<form method="get"><input type="text" id="oderid" name="oderid" placeholder="oderid / invoiceid"></form>
<form method="post"><input type="submit" name="button1"class="submitbtn" value="Button1" /></form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_GET['orderid'];
echo $id;
}
?>
</div>
</body>
</html>
in the part is where the stuff happens. I hope i can get some help, im really new to php
When clicking the button it should echo $id, better said the textbox input
Don't use both POST and GET; pick one.. Like this:
<form method="post">
<input type="text" id="oderid" name="orderid" placeholder="oderid / invoiceid" />
<input type="submit" name="button1" class="submitbtn" value="Button1" />
</form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_POST['orderid'];
echo $id;
}
?>
Also take care of your spelling, orderid is not the same as oderid.

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

$_POST method PHP issue

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.

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!

In nested if statement form's if statement not work

if($media=="pet")
{
if($pressure=="bar")
{
if($f_req=="ltr/m")
{
$kvreq=$flowreq/16.666666667*16.6667/(pow(($presur*1/0.98066/660*1000),0.5));
echo "<b>KV Required:- </b>$kvreq ";
?>
<br/><br/>
<?php
if($kvreq > 14 and $kvreq <= 38){
$minOrfice=10;
$n_kv=38;
$maxOrfice=7;
echo "<b>Minimum Orfice Required:- </b>$minOrfice";?>
</br></br>
<?php
$n_kv1=($n_kv/16.66667*(pow($presur*1/0.98066/660*1000,0.5)))*16.666666667;
echo "$n_kv1 <b>liter/min</b>";?>
</br></br>
<?php
echo "The Max Flow at $maxOrfice orfice";?>
</br></br>
<?php
$maxfo=((14/16.66667)*(pow(($presur*1/0.98066/660)*1000,0.5)))*16.666666667;
echo "$maxfo <b>liter/min</b>";?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input name="chkb1" type="checkbox" />
<input type="submit" value="Submit" name="chk_btn" id="chk_btn"/>
</form>
<?php
if(isset($_POST['chk_btn'])){
echo "$abc";
}
}
}
}
}
...................................................................................................................................................................................
your form method is GET while you're attemptig to get POST data
change this:
if(isset($_GET['chk_btn']))
if(isset($_POST['chk_btn'])) to if(isset($_GET['chk_btn'])) should fix the problem but OP refuse that still cannot display any text.
I suspect that upon submitting the the form again in the page it lose the value of $media, $pressure and so on there it never pass the if statements and never proceed on next process
if($media=="pet")
{
if($pressure=="bar")
{
if($f_req=="ltr/m")
{

Categories