I am currently creating an HTML form that has 2 fields; name and an address. It also has a way of selecting one of 2 options. The form will either be used to look up the address of a person. In this case, only the name field is required to be filled out. The other option is to add a person to the file. In this case both fields need to be filled out. For some reason, I am not able to get the values that inputted from the form into my PHP file. Please help.
Here is my HTML Form
<html>
<head>
<title> Form </title>
</head>
<body>
<form action="action_page.php" method="post">
<div>
<label for="name">Name: </label>
<input id="name" type="text" name="name"><br>
</div>
<div>
<label for=address">Address: </label>
<input id="address" type="text" name="address"><br>
<input type="radio" name="action" value="lookup">Lookup<br>
<input type="radio" name="action" value="add">Add<br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
Here is my PHP file
<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name = $_POST['name'];
echo "<p>",$_POST["name"],"</p>";
echo "<p>",$_POST["action"],"</p>";
echo "<p>",$_POST["address"],"</p>";
$address = array();
if($_POST["action"]=="lookup"){
$fh = fopen("address.txt","r") or die("No such file found.");
while(!feof($fh)) {
$line = fgets($fh);
$info = explode("|",$line);
$address[$info[0]]=$info[1];
}
if(array_key_exists($_POST["name"],$address)) {
echo "<p>",$_POST["name"],"<p>";
echo "<p>",$address[$_POST["name"]],"</p>";
}
?>
<body>
</html>
The error was in
echo "<p>",$_POST["name"],"</p>";
It should be
echo "<p>".$_POST["name"]."</p>";
and same for others
Related
I have these three pieces of code which are supposed to be 2 different forms that send the data to the php. But when I am done with filling in the second form the data of the first form is gone. I can't use 2 different php files because I have to send the data to planyo using file_get_contents.
1st file, testform 1:
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform1">
<label for="naam">naam:</label>
<input type="text" name="naam"><br>
<input type="hidden" name="action" value="form1">
<input type="submit" value="form1">
</form>
</body>
</html>
2nd file, testform 2:
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform2">
<label for="achternaam">achternaam:</label>
<input type="text" name="achternaam"><br>
<input type="hidden" name="action" value="form2">
<input type="submit" value="form2">
</form>
</body>
</html>
test.php file:
<?php
$firstname = "";
$lastname = "";
switch($_POST['action']) {
case 'form1':
$firstname = $_POST['naam'];
echo "form 1 gelukt";
header("Location: http://localhost:8081/greenjoy/testform2.html");
break;
case 'form2':
$lastname = $_POST['achternaam'];
echo "form 2 gelukt";
echo "$firstname" . "$lastname";
break;
}
?>
Modify your files like this:
testform1.html: no change.
testform2.html, change it into a php file (testform2.phtml):
<html>
<head>
<meta charset="UTF-8">
<title>testform 1</title>
</head>
<body>
<form action="test.php" method="post" name="testform2">
<label for="achternaam">achternaam:</label>
<input type="text" name="achternaam"><br>
<input type="hidden" name="action" value="form2">
<input type="hidden" name="naam" value="<?php echo $_GET['firstname']; ?>">
<input type="submit" value="form2">
</form>
</body>
</html>
test.php:
<?php
$firstname = "";
$lastname = "";
switch($_POST['action']) {
case 'form1':
$firstname = $_POST['naam'];
echo "form 1 gelukt";
header("Location: http://localhost:8081/greenjoy/testform2.html?firstname=$firstname");
break;
case 'form2':
$firstname = $_POST['naam'];
$lastname = $_POST['achternaam'];
echo "form 2 gelukt";
echo "$firstname" . "$lastname";
break;
}
?>
This demonstrate the concept, but you should add code to verify if $_GET['firstname'] is set (and react if not).
The idea is to carry the firstname value with you when going to testform2.
Ensure your application flow does not allow a user go to testform2 before going to testform1. This could be done by: if $_GET['firstname'] is not set when calling testform2, redirect the user to testform1 automatically.
Remember that each call to test.php is indendant of the other one. This is why the value set when processing testform1 is not available in testform2.
Another method that could be used is to set the value from testform1 into a session or a database and retrieve it when accessing testform2. More complicated.
Ask yourself if exposing the parameter to testform2 is a security risk or not. If yes == use sessions or database! If not, you can use what I propose.
Another solution if you want to hide your $_GET values, you could encrypt the parameters.
im trying to do simple save of form input with php.
im doing this on 000wehost.com
here is the output that goes wrong
https://hikola.000webhostapp.com/
and here is my code in it.
index.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
</body>
</html>
and another myprocessingscript.php file
<?php
$var1 = $_POST['field1'];
$var1 = $_POST['field2'];
$handle = fopen("dataSave",'w');
fwrite($handle,$var1);
fwrite($handle,$var2);
fclose($handle);
}
?>
can anyone tell me why wont it save a file with input values?
(im trying to do this for 3 hours already... thx..)
My HTML:
<form action="test.php" method="get">
<input type="text" name="text" />
</form>
My PHP:
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<?php
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi, {$text_html}</h1>";
?>
</body>
I want to transport and show data input from type="text" fields in my HTML form, into my PHP file, but the result is as per below:
Hi, {$text_html}"; ?>
Why is the extra code showing?
This is my Source Code.
Assuming you use something (like js) to submit your form.
When you try to output a variable you should use concat of strings.
// this will print the variable name, not is content
echo "<h1>Hi, {$text_html}</h1>";
// Using '.' you can concat strings, so:
echo "<h1>Hi".$text_html."</h1>";
In this way you tell the script that you want the value of $text_html instead of print the string "$text_html"
Hello You need to change in your form code you have to add submit button just. all other code is working fine. Just change your form code with below code.
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
Because you need to transform data from one page to other page either via form submit or you can use Session or Cookie. but currently in your case you just need to add submit button your code work
You will need a submit button. After the submit button is trigerd the if condition will be set to true and the code will execute.
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
</body>
<html>
<head>
<title>Result</title>
</head>
<body style="background:aqua;">
<form action="test.php" method="get">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit" /> // add submit button
</form> ////html page
on php page
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<h1>Hi".$text_html."</h1>";
}
?>
<html>
<head>
<title>Result</title>
<meta charset="UTF-8">
</head>
<body style="background:aqua;">
<?php
if(isset($_GET["submit"])){
$text = $_GET["text"];
$text_html = htmlspecialchars($text);
echo "<div>Hi,".$text_html."<div>";
}
?>
</body>
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!
I am writing an intranet site in HTML, and what online users to be able to add in their own infomation (such as skype name) so that I dont have to go through and add it all myself for the 1000 people at the company, and also so that when new starters come in, they can add their own infomation themselves.
I currently have a 'Users' page with a link in it to another page where there is a form code in it looking like this:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
<?php
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = file_get_contents("users.html");
$posts = "$msg - $name\n" . $posts;
file_put_contents("users.html", $posts);
echo $posts;
?>
I want the infomation users put into this form to be automatically put onto a list on the 'users.html' page, visible to everyone. I had hoped the above code would achieve this, but I cant get it to work.
Also, how would I specify whereabouts in the 'users.html' page the infomation is put? I want it to be amongst the:
<div id="content">
<div class="content_item">
..coding, so that it loads into a list inside the webpage.
Many Thanks in advance :)
Don't try to edit users.html with each submission. That way lies madness.
Store the submitted data in a database. Use the PDO library to do this.
Have the page that displays that data pull it from the database on demand.
Although the use of database would be better and files tend to grow rather large with time, am answering your actual question as per your posted code.
To show the values that were written from a form (seperate from below), you could set up a page with this inside it:
<div id="content">
<?php echo file_get_contents("users.html"); ?>
<div class="content_item">
And the seperate page form would be like the following (tested)
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title Here</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && empty($_POST['msg']) && empty($_POST['name']) ) {
die("All fields need to be filled, please try again.");
}
if(isset($_POST['submit'])) {
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = $msg . "-" . $name;
$file = "users.html";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $posts . "\n<br>") or die("Couldn't write values to file!");
fclose($fp);
}
echo $posts;
echo "<hr>Previous messages<hr>";
$file = "users.html";
if (file_exists($file)) {
$fp = file_get_contents($file, "r") or die("Couldn't open $file for writing!");
echo $fp;
exit;
}
else {
echo "The file is empty";
}
?>