I have following code and it doesn't works for me.
I need print variable from input.
<html>
<head></head>
<body>
<form action='#' method="post">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
<?php
$name = name
echo $name;
?>
</form>
</body>
</html>
Thanks.
you have to use POST to get text from input.
This is right code:
<html>
<head></head>
<body>
<form action='#' method="post">
<input type="text" name="name">
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
echo $name;
}
?>
</form>
</body>
</html>
Im trying to upload a file through a form to my php server and then display the name of the file. ATM I'm getting an error when I'm trying to submit the form:
Objekt was not found! Error 404
<html>
<body>
<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
<?php
header('Content-type: text/plain');
if(isset($_FILES["file"])){
$file = $_FILES["file"];
echo("File: ".$file);
}
?>
if you want to upload/do any operation on same file then remove action from form. then change your code as below to echo file name
<html>
<body>
<form method="post" enctype="multipart/form-data" >
<input type="file" name="file" size="35">
<br>
<br>
<input type="submit" value="Upload" name="submit">
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_FILES["file"])){
$file = $_FILES["file"]["name"];
echo "File: ".$file;
}
}
?>
I'm trying to upload files to my server but it doesn't work at all. Here is test code:
<?php
echo count($_FILES['upload']['name']);
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
It always prints 0, file upload is enabled on my server.
The problem is that you're not counting the $_FILES['upload'].
Simple fix for your problem
Use:
echo count($_FILES['upload']);
Instead of:
echo count($_FILES['upload']['name']);
Edit:
Remove [] from the input's name.
<?php
echo count($_FILES['upload']);//only this modified//
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
I am trying to get user input from a text box and then echo it using php. Here is my code, and it is not seeming to work.
<html>
<body>
<?php
echo $_POST['value'];
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<?php
if(!empty($_POST['value']))
{
echo filter_var($_POST['value'], FILTER_SANITIZE_STRING);}
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
First check if form posted.
I have two .php files as such:
test1.php
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
test2.php
<html>
<head>
<title>Sample Display Page</title>
</head>
<body>
<?php
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
?>
</body>
</html>
I want to know how can I get it all to display on a single page, i.e. on the test1.php, without having to have two files.
Check if $_POST["userNumber"] isset, and echo the form if it's not.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST["userNumber"]){
echo "You have chosen ".$_POST["userNumber"];
}else{
echo '<form action="test1.php" method="post">';
echo 'Please enter a number <input type="number" name="userNumber"><br>';
echo '<input type="submit">';
echo '</form>';
}
?>
</body>
</html>
Your test1.php will need to look like this
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="test1.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
</body>
</html>
You simply need to remove the action="test2.php" section and combine both pages into one just like the following:
<html>
<head>
<title>Sample</title>
</head>
<body>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
<?php
if(isset($_POST['userNumber'])){
$user_number = $_POST['userNumber'];
echo "You have chosen $user_number";
}
?>
</body>
</html>
Pretty simple actually:
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST) && isset($_POST['userNumber'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if (isset($_POST['submitted'])) {
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
}
else {
?>
<form method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submitted">
</form>
<?php
}
?>
</body>
</html>
Just use isset() for submit button, note that i added name="submit" to your button since it was missing
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
if(isset($_POST['submit'])
{
$user_number = $_POST["userNumber"];
echo "You have chosen $user_number";
} else {
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<?php } ?>
</body>
</html>
This one is fully tested and allows you to revise the value after submission. You still have to worry about cross-site scripting attacks, etc... but that is outside of the scope of the question.
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
$user_number = '';
if (isset($_POST['userNumber'])) {
$user_number=$_POST['userNumber'];
?>
<p>You have chosen <?php echo $user_number ?></p>
<?php
}
?>
<form method="post">
Please enter a number <input type="number" name="userNumber" value="<?php echo $user_number ?>"><br>
<input type="submit">
</form>
</body>
</html>
set the variables up before the form as placeholders and check for isset
<?php
$user_number = '';
if(isset($_POST["userNumber"])) {
$user_number = $_POST["userNumber"];
}
?>
<form action="test2.php" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit" name="submit">
</form>
<p>You have chosen: <?php echo $user_number ?></p>
Sample
<form action="<?php $_PHP_SELF ?>" method="post">
Please enter a number <input type="number" name="userNumber"><br>
<input type="submit">
</form>
</body> </html>