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>
Related
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
I really don't know why my code isn't working! Let me clear it by example..
I got a file named 'index.html' example...
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="test">
<input type="submit">
</form>
</body>
</html>
And of course the form action file test.php .. example..
<?php
$something = ($_POST["test"]);
// from here I have some PHP codes and this "something" variable will be process in this codes and will print out something spacial..
?>
Now example If I post "Hello, It's not working" then the output will show a spacial design.
But Instead process, it's just printing out whatever I submit in that form.
But when I manually add the variable to "something" and if I execute the "test.php" . Example..
$something = "Hello, It's not working";
Then it works perfectly..
And yes. Also tried GET method.. It's still same as POST.
This is my first question here..
Thanks for any help and suggestions!
First Convert, index.html to php and follow this code:-
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" value="" name="test">
<input name="submit" value="submit" type="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$something = $_POST["test"];
}
?>
That's correct _POST:
<?php
//NO ($_POST["test"])
$something = $_POST["test"];
?>
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..)
I have the following form on "test.php".
<?php
session_start();
if(isset($_POST['ph']))
if(isset($_POST['submit']))
$_SESSION['ph'] = $_POST['ph'];
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
The "order.php" looks like this:
<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
echo ($_SESSION['ph']);
?>
The first time I load the "test.php" and input the phone number it works perfectly and gives me the correct output on "order.php", but the second time onward, "order.php" gives me the same value which I had entered the first time even though I input a different value. I refreshed the page, same result.
I closed the file and reloaded it, still same value. Why is it behaving that way and how do I correct it? I want session to change value whenever a new number is entered which is not happening.
Change the new value to SESSION ON your order.php page like below:-
<?php
require 'connection.php';
session_start();
if(!empty($_POST['ph'])){
$_SESSION['ph'] = $_POST['ph']; //change value of phonenumber inside SESSION
}
if(!empty($_SESSION['ph'])){
echo ($_SESSION['ph']);
}
?>
Also change test.php code like this:-
<?php
session_start(); // no need to do other stuff
?>
<!doctype html>
<html lang="en">
<body>
<form method="POST" action="order.php" id="custphoneform">
<label for="PhoneNumber">Enter Phone Number:</label>
<input type="number" name="ph" required>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
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">