displaying a form with $_POST? - php

i have been trying to get my filled out form to display next to it.
I have tried doing this with an if statement but no luck.
I also trying just doing print_r and that did work but i want it to be after u hit submit it shows.
heres my code so far!
<form action="phptest1.php" method="POST">
<fieldset>
<legend>Inloggegevens:</legend>
<label for="naam">Gebruikersnaam:</label>
<input type="text" name="naam" id="naam">
<label for="wachtwoord">Wachtwoord:</label>
<input type="password" name="wachtwoord" id="wachtwoord"><br>
<label for="man">Man</label><br>
<input type="radio" name="gender" value="male"><br>
<label for="for">Vrouw</label><br>
<input type="radio" name="gender" value="female"><br>
<label for="checkbox">Ik heb de algemene voorwaarden gelezen.</label><br>
<input type="checkbox" name="conditions" value="agree">
<textarea name="commentaar" rows="4" cols="40"
placeholder="Schrijf hier uw commentaar…"></textarea>
<select name="land">
<option value="nl">Nederland</option>
<option value="be">België</option>
<option value="de">Duitsland</option>
<option value="fr">Frankrijk</option>
</select>
</fieldset>
<input type="reset"><br>
<input for="submit" type="submit">
<?php
if (isset($_POST['submit']))
{
echo '<h1>'.$_POST['naam'].'</h1>';
echo '<br><br>';
echo $_POST['wachtwoord'];
echo '<br><br>';
echo $_POST["gender"];
echo '<br><br>';
echo $_POST["land"];
echo '<br><br>';
} else {
echo 'U bent nog niet ingelogd.';
}
?>

The indices of $_POST are populated by the name of the input elements. Since you have no element named submit your isset is never met. To fix that give it a name.
<input for="submit" type="submit" name="submit">

Instead of
action="phptest.php"
if(isset($_POST['submit']))
You can just use
action=""
if($_POST)
The action="" will make sure the post is send to the current page/ file again.
The if($_POST) still makes sure that the code inside the if statement is only run when the page is posted. You could use isset($_POST['submit']) however your submit button doesn't have a name of submit so there will never be a $_POST['submit'].

Replace your submit button with the following :
<input type="submit" name="submit" value="submit"/>
And make sure the post is submitted to the same page by removing phptest1.php from action or just use:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">

Related

getting object not found error after clicking submit button, I want to display the value of hidden tag

<html>
<BODY>
<form action= "myscript.php" method="post">
first name: <input type="text" name="firstname" value="Mary"><br>
Last name: <input type="text" name="lastname" value= "clan"><br>
<input type="checkbox" name="mychoices[]" value="choice1" checked>
<input type="checkbox" name="mychoices[]" value="choice2">
<input type="checkbox" name="mychoices[]" value="choice3">
<select name="myselection">
<option value="selection1" selected>Option1</option>
<option value="selection2">Option2</option>
<option value="selection3">Option3</option>
</select>
<select name="myselections[]" size="3" multiple>
<option value="choice1" selected >Choice1</option>
<option value="choice2" selected>choice2</option>
<option value="choice3">choice3</option>
</select>
<textarea name="mytextarea" rows="10" cols="40">
Welcome to the web developement world.
</textarea>
<input type="password" name="mypassword">
<input type="hidden" name="myname" value = "myvalue">
<input type="reset" value="reset form">
<input type="image" name="myimage" src="desert.jpg" height="42" width="42" onclick= "document.write('<? php Aftersubmit() ?>');"/>
<input type="submit" name="submitbutton" value="Submit Form">
</form>
<?php
function Aftersubmit()
{
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
</BODY>
</HTML>
I want to display the value of hidden tag after clicking submit button. But getting "Object not found" error 404. Beginner in php, pls help. I also want to know how to call php functions from html.
You can't call a PHP function from an onClick event. That only works with Javascript functions. So one problem is in this line:
onclick= "document.write('<? php Aftersubmit() ?>');
You can remove that, and then pull your PHP out of the function. This will display your name if you click the submit button.
<?php
if(isset($_POST['submitbutton'])){
$myname = $_POST['myname'];
if(isset($myname)){
echo ($myname);
}
}
?>
You may also have to change the action of your form:
<form action= "myscript.php" method="post">
If the file myscript.php doesn't exist, then you'll get a 404 Not Found error every time. You can make a form point to itself by removing the action attribute:
<form method="post">

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

PHP - HTML - Displaying input form even after submit

I have two input forms and would like the second one to stay on the page even when it is submitted.
<div id="first">
<form method="POST">
Number: <input type="text" name="number"><br>
<input type="submit" value="Submit">
</form><br>
</div>
<div id="second">
<?php
if (isset($_POST['number']) && !empty($_POST['number'])){
?>
<form method="POST">
Name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form><br>
<?php
}
?>
</div>
<div id="third">
<?php
if (isset($_POST['name']) && !empty($_POST['name'])){
echo "TEST";
}
?>
</div>
When I submit my first form, the second form appears correctly since $_POST['number'] is not empty. However, the content of 'number' disappears as soon as I submit it.
Then, when I submit the second form, the word "TEST" appears correctly but the form itself disappears since $_POST['number'] from the first form is now empty.
I need to find a way to somehow save the value of number in the first form so that the second form does not disappear.
Any suggestions?
You can add hidden field:
<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
Then your second form will be changed to:
<form method="POST">
Name: <input type="text" name="name"><br>
<input type="hidden" name="number" value="<?php echo $_POST['number']; ?>">
<input type="submit" value="Submit">
</form

php echo not working properly

Basically, I want made a simple form in html and i want to output the all of the user input once clicked submit using php. So far its just displaying the details I entered but it doesn't get user's data to echo them out. Here are my two files
output.php:
<?php
echo 'Name: '.$POST["name"].'<br /> Gender: '.$POST["gender"].'<br />Country: '.$POST["country"];
?>
testingformphp.html (form part):
<form id="form" name="form" method="post" action="./PHP/output.php"/>
Name<input name="name" type="text" id="name"/><br/>
Gender<label><input type="radio" name="gender" id="gender_0" value="male"/>Male</label><br/>
<label><input type="radio" name="gender" id="gender_1" value="female"/>Female</label><br/>
<select name="country" id="select">
<optgroup title="europe">
<option value="Russia">Russia</option>
<option value="Greece">Greece</option>
</optgroup>
</select>
<input type="submit" id="button" value="Submit"/>
<input type="reset" id="reset" value="Reset"/>
</form>
Can anyone help?
$POST does not exists, try $_POST.
<?php
echo 'Name: '.$_POST["name"].'<br /> Gender: '.$_POST["gender"].'<br />Country: '.$_POST["country"];
?>
http://www.tizag.com/phpT/postget.php To review the post and get method.

How to access the form's 'name' variable from PHP

I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.
I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.
My code's below (though I'm not sure it's strictly relevant to the question):
<?php
$bmiSubmitted = $_POST['bmiSubmitted'];
if (isset($bmiSubmitted)) {
$height = $_POST['height'];
$weight = $_POST['weight'];
$bmi = floor($weight/($height*$height));
?>
<ul id="bmi">
<li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>
<li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>
<li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>
</ul>
<?php
}
else {
?>
<div id="formSelector">
<ul>
<li>Metric</li>
<li>Imperial</li>
</ul>
<form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (<abbr title="metres">m</abbr>):</label>
<input type="text" name="height" id="height" />
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">
<fieldset>
<label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
<input type="text" name="weight" id="weight" />
<label for="height">Height (Inches):</label>
<input type="text" name="height" id="height" /
<input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
</fieldset>
<fieldset>
<input type="reset" id="reset" value="Clear" />
<input type="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
}
?>
I verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.
To identify the submitted form, you can use:
A hidden input field.
The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
You can use code as follows:
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
You can do it like this:
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">
Check the posted values
if (isset($_POST['myform'])) {
$values = $_POST['myform'];
// $login = $values['login'];
// ...
}
The form name is not submitted. You should just add a hidden field to each form and call it a day.
In the form submitting button (id method of form is post):
<input type="submit" value="save" name="commentData">
In the PHP file:
if (isset($_POST['commentData'])){
// Code
}
For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.
Use a unique value on the submit button for each form like so
File index.html
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="contact">Send Message</button>
</form>
<form method="post" action="bat/email.php">
<input type="text" name="firstName" placeholder="First name" required>
<input type="text" name="lastName" placeholder="Last name" required>
<button name="submit" type="submit" value="support">Send Message</button>
</form>
File email.php
<?php
if (isset($_POST["submit"])) {
switch ($_POST["submit"]) {
case "contact":
break;
case "support":
break;
default:
break;
}
}
?>
As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.
To prevent an XSS attack, where you have written:
<?php echo "$weight"; ?>
You should write instead:
<?php echo htmlentities($weight); ?>
Which could even be better written as:
<?=htmlentities($weight); ?>
You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.
For example: action="loginregister.php?whichform=loginform"
I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:
<form name="ctc_form" id="ctc_form" action='' method='get'>
<input type="hidden" name="form_nm" id="form_nm">
<button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
</form>
It seamlessly and efficiently accomplishes the following:
Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
Works with both GET and POST methods.
Requires no additional, independent JavaScript.
You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.
Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.

Categories