I am new to PHP, I tried to work w3 schools example of posting data on forms..
It never works for me... the webpage doesn't display any data, I tried several forums and also SO that never helped.. I still keep getting it empty!
Example #1: A simple contact from - HTML code
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
Example #2: Printing data from our form
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
Expected output of this script may be:
Hi Joe. You are 22 years old.
Actual Output:
Hi . You are years old
The Post parameter is not displaying data.. Any help is really appreciated.
What W3Schools (PHP Form Handling) fail to mention is, that the entire (2) bodies of code need to either be inside a single file, or in 2 seperate files in order for it to work as expected.
However, the code from W3Schools and the OP are not indentical and have been modified, using htmlspecialchars and (int)
If you wish to make use of htmlspecialchars, do the following in your welcome.php file:
<?php
$fname = htmlspecialchars($fname);
?>
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo (int)$_POST['age']; ?> years old.
Form used:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
I did not see any mention on the W3Schools website about the use of htmlspecialchars or (int)
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If you wish to make use of htmlspecialchars then you should the following syntax:
$fname = htmlspecialchars( $fname );
And placed within <?php and ?> tags such as:
<?php
$fname = htmlspecialchars( $fname );
?>
NOTE: I know next to nothing about running a Webserver from my own computer, yet from information I found here on SO
mention that in order to access your PHP files, you need to type in http://localhost in your Web browser's address bar and the folder where your file is in.
Please visit this answer
StackOverflow did not let me insert the codes on that page, for one reason or another.
In your <form> tag the "action" is where your POST data is being sent. So does your file structure look like this?
//index.php
<form action="action.php" method="POST"> // <-- make sure to capitalize method="POST" as well
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
.
//action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
EDIT 2
Sounds like you might be getting errors in PHP that are turned off. Try this in action.php and re-submit the page.
//action.php
<?php
error_reporting(E_ALL);
?>
Hi <?php echo $_POST['name']; ?>.
You are <?php echo $_POST['age']; ?> years old.
'post' or 'POST' both works fine in form tag.
The following should be in action.php
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
If still you get this then go to php.ini and set your errors to E_ALL and E_STRICT
and check whats the error.
Most probably it should work now...
In your form check if it is not sending empty values.
STEP 1
copy and paste the following code in your text editor and run it. It will allow you to test the values from the form without redirecting the page.
The following code should be in index.php
<form action="action.php" method="POST" onsubmit="return validate()">
<p>Your name: <input type="text" name="name" id="name"/></p>
<p>Your age: <input type="text" name="age" id="age"/></p>
<p><input type="submit" /></p>
</form>
<script type="text/javascript">
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
alert ("Name="+name+" age="+age);
return false;
}
</script>
This code will check if the values are getting entered correctly without redirecting the page to action.php.
Step 2
If you are getting the desired output from the previous code then you can replace the validate function with the code below. (replace everything between the script tags)
function validate(){
var name=document.getElementById("name").value;
var age=document.getElementById("age").value;
if (name==null || name==""){
return false;
}
if (age==null || age==""){
return false;
}
return true;
}
If both name and age are filled in the form, the submit will now redirect to action.php
Step 3
In action.php use the following code.
<?
//These code goes in action.php
extract ($_POST);
echo "Hi $name. You are $age years old";
?>
edited with instructions on OP's request
Simply ensue that your form is running from your server (http://localhost..) and not the form location itself(file:///C:xampp..). Happy coding
Related
I don't know how to do it but hope anyone could help me about my program.
My program flows like this.
I have search.php - it will search unique id. I also have a button here, once submitted/loaded, it will echo all of the data on my index.php
index.php - this is where all data will be loaded. my goal is to update the fields. What i wanted to do is that test.php (contains my condition) will be read only after user click submits. because i tried to include it and place it on the top but it gives me error and not giving me the data that supposedly to be loaded. but when i tried to removed it, all data is successfully echoed on the textbox.
My test.php should be trigger once it was submitted.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox 1: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Textbox 2: <input type="text" name="txt1" value="<?php echo $txt2;?>">
Textbox 3: <input type="text" name="txt1" value="<?php echo $txt3;?>">
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$txt2 = "";
$txt3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$txt1 = $_POST["txt2"];
$txt1 = $_POST["txt3"];
}
?>
If I understand it correctly, this should be what you want:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
include 'test.php';
}
?>
instead of
<?php include 'test.php' ?>
I would do this with Javascript and Ajax, but what you should first do, is give your input-elements unique names (now its txt1 in all of them) as well as the variables in test.php
you missed ; at the end of include statement.
<?php include 'test.php';?>
Thank you ....
I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];
so I've got the latest version of Xampp installed and in the htdocs directory I have an htm page containing a very simple form with just a single text field and a submit button which, when clicked, links to a php page that prints a message saying "What you typed is:" followed by what was typed. If nothing was actually typed in the field, after clicking on the submit button, the php page will display an error message saying "Error: you didn't type anything".
Here is the code for the htm page:
<html>
<head>
</head>
<body>
<center>
<form action="p1.php" method="post">
Type something:
<input type="text" name="nom">
<input type="submit" value="SEND">
</form>
</center>
</body>
</html>
And here is the initial code for the php page:
<html>
<head>
</head>
<body>
<?PHP
if (!$_POST) {echo "Error: you didn't type anything";}
else {echo "What you typed is: " . $_POST["nom"];}
?>
</body>
</html>
So with this php code, if I type anything in the field and click the submit button, the php page will display "What you typed is:" followed by what was typed but if I don't actually type anything in the field and click the submit button, the php page will display "What you typed is:" followed by nothing instead of displaying "Error: you didn't type anything".
However, I discovered that if I changed the "if (!$_POST)" to "if (!$_POST["nom"])", then if I didn't type anything in the field, the php page would display "Error: you didn't type anything"...problem solved.
But this surprised me, as I have seen in my course material an example (it is referred as a self-calling form or something along those lines) where "if (!$_POST)" is used.Here it is:
<html>
<head>
<title>Me llamo a mi mismo...</title>
</head>
<body>
<?
if (!$_POST){
?>
<form action="auto-llamada.php" method="post">
Nombre: <input type="text" name="nombre" size="30">
<br>
Empresa: <input type="text" name="empresa" size="30">
<br>
Telefono: <input type="text" name="telefono" size=14 value="+34 " >
<br>
<input type="submit" value="Enviar">
</form>
<?
}else{
echo "<br>Su nombre: " . $_POST["nombre"];
echo "<br>Su empresa: " . $_POST["empresa"];
echo "<br>Su Teléfono: " . $_POST["telefono"];
}
?>
</body>
</html>
So why isn't "if (!$_POST)" not working in my case? (using Mozilla as the browser)
when posting like this and having empty fields, it still gets saved as value.
var_dumping this
<form method="post" action="fgc.php">
<input type="text" name="horse">
<input type="submit">
</form>
will return:
array(1) { ["horse"]=> string(0) "" }
In PHP, the $_POST superglobal is always defined, regardless of whether or not the method was actually POST, or if any data was posted. However, if it isn't a POST, or if there is no data, that array will be empty.
If you convert an array to a boolean, the value will be false if there are no elements in the array.
<?php
var_dump(isset($_POST)); // Always TRUE
var_dump(!!$_POST); // TRUE if data was posted (even if empty fields), FALSE otherwise
The reason your documentation says to use !$_POST is that often times the page will be loaded with the GET method, in which case, $_POST will be an empty array.
try this code
if($_SERVER['REQUEST_METHOD'] == 'POST'){ // if the form was submitted
if(isset($_POST['name']) && !empty($_POST['name'])){ // basic validation
// # Don't forget XSS sanitizing !
$name = htmlspecialchars($_POST['name']);
// Add data to DB or something
}else{
echo 'Error: Required field "name"';
}
}
i am writing a unit converter php program. i have the set up of the page, but it seems like my php file is not being found. when i click the submit button i am brought a an error page. this is my html code.
<html>
<head>
</head>
<body>
<form action="hw7.php" method="post">
<h2>Convert length:</h2>
<p>Select conversion direction: <br />
<input type="radio" name="dir" value="1"
checked="checked"/> Feet to meters<br />
<input type="radio" name="dir" value="2" /> Meters to feet<br
/>
</p>
<p>Value to be converted: <br /><input type="text"
name="cvalue" /></p>
<p><input type="submit" value="Convert" /></p>
</form>
</body>
</html>
and here is my php file
<?php
$fTOm = $_POST["cvalue"] * 3.2808;
$mTOf = $_POST["cvalue"] / 3.2808;
echo "Result: ";
if ($POST[ 'dir'] == "1") <?php echo "$_POST["cvalue"]; ?> feet = <?php echo "fTOm"; ?> meters;
?>
Your PHP script is likely causing a server error due to improper coding. Check your server logs for PHP related errors, and possibly turn on error_reporting for PHP.
Within your PHP script, you have a statement of:
if ($POST[ 'dir'] == "1") <?php echo "$_POST["cvalue"]; ?> feet = <?php echo "fTOm"; ?> meters;
Since you're already in a PHP script, why are you using the <?php echo inline an if statement?? Correct this issue and try running the script by directly calling it.
Change the if line to :
if ($POST[ 'dir'] == "1") {
echo $_POST["cvalue"] . "feet , " . $fTOm . " meters";
}
You should know that the hw7.php file must be in the same directory as the HTML page that is calling it from a form.
I will recommend to use always relative URLs or the contexT_path when possible.
try:
<form action="./hw7.php" method="post">
try this maybe it will help:
<form action="./hw7.php" method="post">
Also remove all those syntax errors in the php script.<?php .... ... ?> can not have another <?php inside of it.
I am just starting to learn php, how would I initiate a echo statement after a submit button is pushed, or even a anchor tag.
Here is my code so far
form name="myform" method="get" actions="madlib01.php"
Name: <input type="text" name="name" /> <br />
<input type="submit" name="submit" />
form
<?php
$Name = $_GET['name'];
$hello .= "Hello $Name";
echo $hello //I would prefer the echo to happen after the submit button is hit
?>
the correct attribute for your form tag is "action", not "actions"
When the form is submitted, a new request is sent to the server (in your case, using GET).
So to do it all in one page:
form.php:
<form action="form.php" method="GET">
<input type="text" name="name"/>
<input type="submit">
</form>
<?PHP
if (! empty($_GET['name'])){
echo 'Hello, ' . $_GET['name'];
}
?>
You will first need to check if PHP has received your GET parameter using isset or array_key_exists:
if(isset($_GET['name']) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
}
or:
if(array_key_exists('name', $_GET) && !empty($_GET['name'])) {
$Name = $_GET['name'];
echo "Hello $Name";
} else {
//example: default to something if nothing has been passed
echo "Hello Guest";
}
Also note, if you're submitting to the same page, you can omit the action attribute from your form tag altogether:
<form method="GET">
echo $hello
You've just gained an HTML-injection vulnerability. If someone sends your user to:
http://www.example.com/madlib01.php?name=<script>stealYourCookies()</script>
you've got problems.
Yes, this is a My First PHP Script. That doesn't make security optional. This is a mistake every tutorial makes: teaching bad practice from the start, treating correctness (and security, which is a subset of correctness) as an optional extra.
The result is that most PHP code out there is full of holes. But there's no need for yours to be! Every time you place a pure-text string into a surrounding HTML context, escape it properly:
echo htmlspecialchars($hello);
I tend to define a function with a shorter name than ‘htmlspecialchars’ to do that for me, as I'm lazy.
<?php
function h($text) {
echo(htmlspecialchars($text, ENT_QUOTES));
}
$name= '';
if (isset($_REQUEST['name']))
$name= trim($_REQUEST['name']);
?>
...
<?php if ($name!=='') { ?>
<p> Hello, <?php h($name); ?>! </p>
<?php } ?>
<form method="get" action="madlib01.php">
<p>
<label for="namefield">Name:</label>
<input id="namefield" type="text" name="name" />
</p>
<p>
<input type="submit" />
</p>
</form>
Now if you say your name is Mister <script>, the page will greet you exactly as such, angle brackets and all, instead of trying to run JavaScript. This is the correct output and thus also secure.