My Code:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
<Input Type = "text" Value ="<?PHP print $username ;?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>
Problem: The PHP code gets displayed in the textbox instead of the actual value
That syntax is correct (albeit vulnerable to XSS).
If it doesn't work, then you aren't using a server that supports PHP for the file you are loading.
PHP, on the WWW, only works when the page is accessed:
Through a web server
That has PHP installed
That knows that the file you are loading contains PHP and should be processed by the PHP engine (this is usually done by giving it a .php file extension).
Update: You have edited your question to include a screen grab. This confirms this answer. You are trying to load the PHP from your file system directly into your browser (with a file:/// URI). You need to access it through a web server (with an http:// URI).
As #CORRUPT points out in a comment. You don't give $username a default value, so you will get an error if the POST data isn't set. I suggest setting it to an empty string before the if (isset($_POST['Submit1'])) { line.
Try Below code:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
}else{
$username='';
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
<Input Type = "text" Value ="<?PHP print $username ;?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>
Write this function in the php block:
function prepopulate($name) {
if(isset($_POST[$name])) {
return $_POST[$name];
}
else {
return "";
}
}
and then in the HTML part
<FORM NAME ="form1" METHOD ="POST" ACTION = "">
<input name="fname" type="text" value="<?php echo prepopulate('fname');?>">
<input name="lname" type="text" value="<?php echo prepopulate('lname');?>">
</FORM>
You just have to pass the name of the textbox to the function prepopulate to retain it after form submit.
Related
php submit form is not redirecting given action, it redirects to home page.. I use Yii 2, pretty URL.. But I don't want to use yii2 submit form.. Kindly help me on this.. I'm not sure if we can use core php form in Yii 2 framework
<head>
<title>Form</title>
</head>
<body>
<form action = "/syllabus/lms/index" method = "post">
Author : <input type = "text" name = "author"
placeholder = "Author's Name" />
<br><br>
Number of published Article : <input type = "number"
name = "num_article" placeholder = "Published Article" />
<br><br>
<input type = "submit" name = "submit" value = "Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
echo "test";
}```
Try to change the given action parameter.
Change this line of code:
<form action = "/syllabus/lms/index" method = "post">
To:
<form action = "syllabus/lms/index.php" method = "post">
I have a PHP file named "delete-wish-form.php" with a form that has the following fields:
wishId (hidden field) The value of this field should be equal to a GET variable that is passed in via the URL.
So my question is if the URL is delete-wish-form.php?wishId=1 then I want my hidden field to look like this:
CODE:
<!DOCTYPE HTML>
<html>
<title>Delete Wishes</title>
<body>
<h2>Wishes Form</h2>
<?php
$wishId = $_GET['wishId'];
?>
<form method="get" action="process-delete-wish-form">
<input type="hidden" name="wishId">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
You should check if the wishId is define in your url param to avoid thrown error in form, and use htmlspecialchars to avoid xss attack on GET parameter
<?php
$wishId = '';
if(isset($_GET['wishId'])) {
$wishId = htmlspecialchars($_GET['wishId'],ENT_QUOTES);
}
?>
and then in your input hidden field
<input type="hidden" name="wishId" value="<?php echo $wishId;?>">
I have written a PHP script to run with HTML that takes the text entered into a textbox and place it in a text file, however, the code does not work and I cannot figure out why. I'm fairly new to PHP and it would be great if somebody could help :D
<!DOCTYPE HTML>
<html>
<head>
<title>Altering text files</title>
</head>
<body>
<form name ="form1" method ="post" action = "">
<input type = "text" name = "string">
<input type = "submit" name = "submit" value = "Add text">
</form>
</body>
</html>
<?php
$file = "./lines.txt";
$write = $_POST['string']
file_put_contents($file , $write, FILE_APPEND);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Altering text files</title>
</head>
<body>
<form name ="form1" method ="post" action = "">
<input type = "text" name = "string">
<input type = "submit" name = "submit" value = "Add text">
</form>
</body>
</html>
<?php
if(isset($_POST))
{
var_dump($_POST);
echo 'foo!';
/*$file = "./lines.txt";
$write = $_POST['string'];
file_put_contents($file , $write, FILE_APPEND);*/
}
?>
test it to see if something is printed
try to add semicolon after $_POST['string']
also try display errors to know if anything is going wrong
I have an if else statement. The code logic is that the first if statement would get all the necessary informations needed and the second if statement would print all the informations that are generated at the first if statement. The problem is that when it triggers the second if statement, it disregards all the data that are stored in the first if statement.
Can anybody help me how to solve this problem? Thank You
This is just a sample code but the process and logic of code is somehow the same.
<head>
<title>Sample PHP Web</title>
</head>
<body>
<form method = "post">
<input type = "submit" value = "submit" name = "submit">
<?php
if(isset($_POST['submit']))
{
$nn[0] = "man";
$nn[1]= "men";
echo'<input type = "submit" value = "print" name = "print">';
}
?>
</form>
<?php
if(isset($_POST['print']))
{
echo $nn[0];
echo $nn[1];
}
?>
</body>
After the initial form submission is handled the values you set into PHP variables are lost. They do not persist across page requests. If you want them to persist you need to use sessions.
<?php
session_start();
?>
<head>
<title>Sample PHP Web</title>
</head>
<body>
<form method = "post">
<input type = "submit" value = "submit" name = "submit">
<?php
if(isset($_POST['submit']))
{
$_SESSION['nn'][0] = "man";
$_SESSION['nn'][1]= "men";
echo'<input type = "submit" value = "print" name = "print">';
}
?>
</form>
<?php
if(isset($_POST['print']))
{
echo $_SESSION['nn'][0];
echo " "; // seperate words with a space
echo $_SESSION['nn'][1];
}
?>
</body>
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</head>
<body>
<form name ="form1" method ="POST" action = "allNewPractice.php">
<input type = "text" value ="username">
<input type = "submit" name = "submit1" value = "login">
</form>
</body>
</html>
The name of this file, as you can probably tell, is "allNewPractice.php" and I use
localhost/allNewPractice.php directly through my browser to access it, not through notepad++'s run.
it doesn't work whatsoever; its supposed to print the information I typed into the text box to the page but The page does NOTHING.When I click the login button the page only refreshes, shows the original text box and the login button, but doesn't show what i entered.
I got the tutorial from http://www.homeandlearn.co.uk/php/php4p6.html
what am i doing wrong?
Is there something wrong with my computer?
Learn basic HTML forms:
<input type = "text" value ="username">
Inputs with no name do not submit anything. It should be
<input type="text" name="username" value="somevalue" />
^^^^^^^^^^^^^^^----must be present.
try this. personally i just dont seem to use the post method but it works
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if ($_GET['username']) {
$user = $_GET['username'];
echo $user;
}else{
echo "no text";
}
?>
</head>
<body>
<form method ="GET" action = "allNewPractice.php">
<input id="username" name="username" type="text" placeholder="username">
<input type = "submit" id = "submit" value = "login">
</form>
</body>
</html>
First, you need to add a name attribute to the input box, as such:
<input type = "text" value ="username" name="username">
When you call $_POST['username'], it is referencing the name attribute, not the value.
Second, you are setting the value of $username before anything has been posted. This will result in an error, undefined variable: username, because $_POST['username'] does not exist when you first load the page. Not until after you submit the form will it have a value. So, you need to check if the form has been submitted first:
if (isset($_POST['submit1'])) {
// Process data from form
}
Here is a complete and working version of your program:
<?php
if (isset($_POST['submit1'])) {
$username = $_POST['username'];
print ($username);
}
?>
<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
<form name ="form1" method ="POST" action = "allNewPractice.php">
<input type = "text" value ="username" name="username">
<input type = "submit" name = "submit1" value = "login">
</form>
</body>
</html>