Reading data using post - php

Consider following html page:
index.html
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
If GET is used for reading the data let's say "name" then following is understandable:
welcome.php
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
When POST is meant for writing the data How is it possible to read the "name" using POST? Why should the following work?
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

First of all add a name in your submit input <input name="submit" type="submit">
Second into welcome.php add if(isset($_GET["submit"])){ $submit = preg_replace('#[^a-z0-9]#i', '', $_GET['submit']); }
and then use Welcome <?php echo $submit; ?>
i suggest to use preg_replace if you want to pass values from page to page

Related

Php POST not receiving any data

I've just got into php recently, and now i am working on a login form which has a signup page and a signup script to do all the database and such. But my $_POST is not working at all. I`ve tried searching online, confirm that my xampp/php/php.ini setting enable post process, and such. But all i get is nothing. To make the problem simpler i used a post tutorial code online
Posttest.php
<!DOCTYPE HTML>
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" id="name" name="name"><br>
E-mail: <input type="text" id="email" name="email"><br>
Submit
</form>
</body>
</html>
welcome.php
<html>
<body>
<?php
$disname="";
$disname = isset($_POST['name']) ? $_POST['name'] : '';
echo "$disname is your username";
var_dump($_POST);
?>
</body>
</html>
and the result is empty. Please help i`ve been working on this problem for few days.
Everything looks fine but the submit href link, which you should replace with a Submit input type:
Submit
to:
<input type="submit">
You can't use "a" tag for submit. "a" tag just direct to address. So you should use this:
<input type = "submit" value = "Submit">
<!DOCTYPE HTML>
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" id="name" name="name"><br>
E-mail: <input type="text" id="email" name="email"><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>
///////////////////////////////////////////////////////
//////welcome.php
<html>
<body>
<?php
$disname="";
$disname = isset($_POST['name']) ? $_POST['name'] : '';
echo "$disname is your username";
var_dump($_POST);
?>
</body>
</html>

php code wont work and display data

This code is from w3schools which displays the email address and name once the user submits the form. However when I tried copying the code it wouldn't let me display the same output and would only say Cannot POST /welcome.php or it would display the welcome.php code whenever i submit.
I didn't even alter it in anyway but why wont it work? D:
index.html
<!DOCTYPE HTML>
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

PHP code doesn't print any value

Here i entered name nd email id but when i click on submit button my entered information is blank like below image.
In PHP clicking on the submit button doesn't print the information. I used $GET and $POST, both are not working.
<!DOCTYPE HTML>
<html>
<body>
<form action="php_forms.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
It should be work.Change your action (php_forms.php) file code.I have tried in my server.Hope it will be helped.
<html>
<body>
Welcome <?php echo htmlspecialchars($_POST["name"]); ?><br>
Your email address is: <?php echo htmlspecialchars($_POST["email"]); ?>
</body>
</html>
Read More dealing with html forms in PHP.NET.
Your form uses get method and php_forms.php uses post method. Try
<form action="php_forms.php" method="post">

donĀ“t recognize variable of php

I have the following code in two files separately
file one.php
<HTML>
<BODY>
<FORM ACTION="two.php" METHOD="POST">
Age: <INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="OK">
</FORM>
</BODY>
</HTML>
file dos.php
<HTML>
<BODY>
<?PHP
print ("The age is: $age");
?>
</BODY>
</HTML>
the age variable is not recognized, someone knows fix?
It's not recognized because you don't create it. Variables don't magically appear in PHP1. You need to get that value from the $_POST superglobal:
<HTML>
<BODY>
<?PHP
$age = $_POST['age'];
print ("The age is: $age");
?>
</BODY>
</HTML>
1 Anymore. They used to when register_globals existed. But that has been obsolete since long before you started coding.
Your trying to access the value of age from a page( dos.php) but you posting it to (two.php) and your missing $_POST['age'].
one.php
<HTML>
<BODY>
<FORM ACTION="two.php" METHOD="POST">
Age: <INPUT TYPE="text" NAME="age">
<INPUT TYPE="submit" VALUE="OK">
</FORM>
</BODY>
</HTML>
two.php
<HTML>
<BODY>
<?PHP
$age = $_POST['age'];
print ("The age is: $age");
?>
</BODY>
</HTML>

Pass form data to another page with php

I have a form on my homepage and when it is submitted, it takes users to another page on my site. I want to pass the form data entered to the next page, with something like:
<?php echo $email; ?>
Where $email is the email address the user entered into the form. How exactly do I accomplish this?
The best way to accomplish that is to use POST which is a method of Hypertext Transfer Protocol https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
index.php
<html>
<body>
<form action="site2.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>
site2.php
<html>
<body>
Hello <?php echo $_POST["name"]; ?>!<br>
Your mail is <?php echo $_POST["mail"]; ?>.
</body>
</html>
output
Hello "name" !
Your email is "whatyou#addedonindex.com" .

Categories