This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP Undefined index / variables
(2 answers)
Closed 9 years ago.
I am trying to learn PHP.I can't run this example.But I think codes are true.I am trying it at my localhost.How can i run it?
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name"fname">
<input type="submit">
</form>
<?php
$name=$_REQUEST['fname'];
echo $name;
?>
</body>
</html>
Error:
( ! ) Notice: Undefined index: fname in C:\wamp\www\index.php on line 12
You haven't submitted the form yet so $_POST['fname'] does not exist. Try this:
<?php
// turns off all errors and notices, recommended for a production website
// comment out this code if on development environment, it will make you a better programmer
error_reporting(0);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name"fname">
<input type="submit">
</form>
<?php
if(isset($_REQUEST['fname']))
{
echo $_REQUEST['fname'];
}
?>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if(isset($_POST['fname'])){
$name=$_POST['fname'];
echo $name;
}
?>
</body>
</html>
You need to check if the variable is set, otherwise you will end up with that error message.
Do something like:
<html>
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">Name:
<input type="text" name "fname">
<input type="submit">
</form>
<?php
if (isset($_REQUEST['fname'])) {
$name = $_REQUEST[ 'fname'];
echo $name;
}
?>
</body>
</html>
Related
I'm trying to create a post request on the same page with infinite amount of posts. The only problem is that the post that I am doing keeps overwriting.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="https://wikiscams.org/form.php" method="post">
<input type="text" placeholder="Anon" name="user">
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if(isset ($_POST["submit"])) {
$user = $_POST['user'];
// echoes post but overwrites
echo $user;
}
?>
What should I do?
With HTML you can use arrays on PHP side and that's what you need right now.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<form action="https://wikiscams.org/form.php" method="post">
<?php if(!empty($_POST['user'])){
foreach ($_POST['user'] as $key => $value) { ?>
<input type="hidden" name="user[]" value="<?=$value?>">
<?php }
} ?>
<input type="text" placeholder="Anon" name="user[]">
<input type="submit" name="submit">
</form>
<?php
if(!empty($_POST['user'])){
foreach($_POST['user'] as $key => $user){
echo $user."<br />";
}
}
?>
</body>
</html>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I recently start the php programming. However, I encountered a problem about running a function on php file. My php file is listed as follows:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="Test()" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function Test()
{
echo "Goodbye";
}
?>
</body>
</html>
After running the program, a webpage is observed with A button entitled submit and the word "Hello". However, After click on the button, the webpage "This page cannot be displayed" is observed. But I respect the word "Goodbye" is shown. I transferred the php code to another file, but the problem was not resolved.
actions in forms aren't functions like JavaScript. You are trying to run Test() on submit which isn't a thing. What you want is action to be action=" "
In reality you should be doing something like this:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
if(isset($_POST['submit'])){
echo "Goodbye";
}
?>
</body>
</html>
If you WANT to use a function....
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="aaaaa" />
<title>Untitled 3</title>
</head>
<body>
<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
echo "Hello";
function test(){
echo "Goodbye";
}
if(isset($_POST['submit'])){
test();
}
?>
</body>
</html>
Lets say he file name is test.php where you have put all your code so the action values should be test.php
<form method="post" action="test.php" >
I am trying to create a very simple Control Panel for a web programming project, the "Show" function in my control panel works very well and shows all the elements by my Delete, Add and Update functions do not work at all.
Here's what I want to do with each function:
Add function -> I want to add an element to my database from the input elements
Delete function -> I want to delete the element that its ID is input by the user in the Control Panel
Update -> Using this function I want to change the Product Title of the selected Product.
These 3 functions don't work, when I input data in the webpage I see that nothing is added/altered to/in the tables.
Here's the code:
add.html
<title>Add</title>
</head>
<body>
<form action="add.php" method="post">
<font size="+2" color="#CC0033">ID</font><input type="text" name="ID" />
<font size="+2" color="#CC0033">Product Title</font><input type="text" name="ProductTitle" />
<font size="+2" color="#CC0033">Price</font><input type="text" name="Price" />
<font size="+2" color="#CC0033">Quantity</font><input type="text" name="Quantity" />
<input type="submit" value="Insert" />
</form>
</body>
</html>
add.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Done</title>
</head>
<body>
<?
$ID=$HTTP_POST_VARS["ID"];
$ProductTitle=$HTTP_POST_VARS["ProductTitle"];
$Price=$HTTP_POST_VARS["Price"];
$Quantity=$HTTP_POST_VARS["Quantity"];
$db=mysql_connect("localhost","root","");
if($db==false)
{
print "Error";
exit;
}
mysql_select_db("Computer");
$query=("insert into Products values('".$ID." ',' ".$ProductTitle."',' ".$Price."',' ".$Quantity."')");
mysql_query($query);
?>
</body>
</html>
delete.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
Enter the ID of the product that you wish to delete:
<form action="delete.php" method="post">
<input type="text" name="UserInput">
<br>
<input type="submit" value="Delete">
</form>
</body>
</html>
delete.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?
$UserInput=$HTTP_POST_VARS["UserInput"];
$db=mysql_pconnect("localhost","root","");
if(!db)
{
print "Error";
exit;
}
mysql_select_db("Computer");
$query=("delete from Products where ID=".'$UserInput');
mysql_query($query);
?>
</body>
</html>
update.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="update.php" method="post">
Product ID<input type="text" name="ProductID">
Product Name<input type="text" name="ProductTitle">
New Product Name<input type="text" name="NewProductTitle">
<br>
<input type="submit" value="Update">
</form>
</body>
</html>
update.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?
$ProductID=$HTTP_POST_VARS["ProductID"];
$ProductTitle=$HTTP_POST_VARS["ProductTitle"];
$NewProductTitle=$HTTP_POST_VARS["NewProductTitle"];
$db=mysql_pconnect("localhost","root","");
if(!$db)
{
echo "Error";
exit;
}
mysql_select_db("Computer");
$query=("update Products set ProductTitle='".$NewProductTitle. "' where ID=$ProductID");
mysql_query($query);
?>
</body>
</html>
firstly, good on you for trying to come up with a solution, however, as other commenters have said, you do have issues in your script. Firstly, I'd consider replacing $HTTP_POST_VARS["ID"]; with the $_POST global variable, and also running an if(isset()) to ensure all fields were set when the user submitted the form.
Secondly, you should consider using MySQLi or PDO for handling your database connection, as they can also provide validation and filtration to prevent injection attacks as Marc B mentioned. It's a good attempt if you are an absolute beginner and I feel if you stick at it you can progress further, and this community is great for getting answers and understanding where you have gone wrong. Hopefully this helps and good luck! :)
This is my coding for the search form:
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="https://excelforth.com/search1.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
</body>
</html>
The thing is , When I submit this form on my web page , It goes to the php Results page and displays the results. But the thing is that it is just a plain page with just the results displayed.
My search page :
www.mysite/test/certification-database-search/
The Php file is located in my webroot folder . I was told that for wordpress , the individual subpages cannot be edited .
How do I :
1.Display the results on the same page as the search page. Not a completely new page.
2.Retain the page layout and theme / headers/ footers of the page
3.If possible run the .php file in /certification-database-search/ and query it from there. instead of using the one in my webroot folder.
THANKS!!
Put your form and PHP in one page, PHP on top with HTML below, then use action=""
Use the variables from the inputs as $var=$_GET['var']; then echo $var;
Sidenote: If you want to stop a process, you can use die(); or exit();
You can put a message inside it; i.e.: die("Enter a search term");
A basic example:
<?php
if(isset($_GET['submit'])){
$query=$_GET['query'];
echo $query;
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="" method="GET">
<input type="text" name="query" />
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
An alternative, showing an error message if field is empty:
<?php
if(isset($_GET['submit'])){
if(empty($_GET['query'])){
echo "Enter a search term";
}
$query=$_GET['query'];
echo $query;
}
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="" method="GET">
<input type="text" name="query" />
<input type="submit" name="submit" value="Search" />
</form>
</body>
</html>
I'm trying to pass a variable in PHP in the same page. The form is:
<form name="form1" method="POST" action="index.php">
<input type="text" name="F1" value="" size="35">
</br>
<input type="submit" name="B1" value="Go!">
</form>
and the PHP script is:
<?php
if(isset($_POST['B1']))
{
$u=$_POST['F1'];
$document=new domDocument('1.0', 'WINDOWS-1251');
$document->loadHTML
('<html>
<head>
<title></title>
</head>
<body>
echo $u;
</body>
</html>');
$document->formatOutput=true;
$document->encoding='WINDOWS-1251';
$document->saveHTMLFile("output.php");
}
?>
The script is meant to create an "output.php" file that prints the content of the text area decleared in the form, but it not works, what can I do?
Thanks!
change this:
$document->loadHTML
('<html>
<head>
<title></title>
</head>
<body>
echo $u;
</body>
</html>');
to
$document->loadHTML
('<html>
<head>
<title></title>
</head>
<body>
'.$u.'
</body>
</html>');
You can also use like
$document->loadHTML
("<html>
<head>
<title></title>
</head>
<body>
$u
</body>
</html>");