<?php
Function runSearch($name)
{
If(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
This code is suppose to display what is entered into the Search String text box. When I don't use a function it works fine. But as soon as I place the code into the function runSearch there is no output. I'm new to php can an argument be sent to a php function and then displayed on the screen?
you need to call your function, otherwise nothing will happen. Also you need to removed the $name-parameter:
<?php
function runSearch()
{
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
runSearch();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
Related
I'm working on a code which has a form. The answer on the form needs to decide to which page it should go, but it doesn't work.
<html>
<body>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
<?php
if($_GET["graden"]>=28){
$action = "Koelbox.php";
}
else{
$action = "scrabble.php";
};
?>
</form>
</body>
</html> ```
You have to declare and initialize the variable before using it.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>
First check the GET parameter, then set your $action variable accordingly and then use it in your echo.
I just initialized the variable before the form, but as far as I see the IF statement doesn't see the input in the form.
<?php
if($_GET["graden"]>=28)
{
$action = "Koelbox.php";
}
else
{
$action = "scrabble.php";
}
?>
<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">
</form>
Here is my code:
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_POST['postName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$age = $_POST['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
echo $_GET['getName'];
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$age = $_GET['age'];
if ($age >= 16) {
echo "You are old enough to volunteer for our program!";
} else {
echo "Sorry, try again when you're 16 or older.";
}
}
?>
I have two forms. Both displaying the exact same thing, but one form using POST and one using GET.
I have gotten so close to finishing this off but now I have another small/weird issue.
The code technically works correctly, but here's the output explanation:
when I first open up the page the GET form already has the result "Sorry, try again when you're 16 or older." When I fill out the first 'simple' form, it displays the result correctly but then the POST form shows the "Sorry, try again..." result. Then, when I fill in the information and click submit, it displays the correct result and the other two forms are blank as they're supposed to be, and then the same result when I fill out the GET form.
Any help on this is much appreciated.
Try this code :
<h2> Simple Form </h2>
<form action="" method="post">
First Name: <input type="text" name="firstName">
Last Name: <input type="text" name="lastName"><br /><br />
<input type="submit">
</form>
<br />
Welcome,
<?php
if (isset($_POST['firstName']) && $_POST['lastName'])
{
echo $_POST['firstName'];
echo " ";
echo $_POST['lastName'];
}
?>
!
<hr>
<h2>POST Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form action="" method="post">
Name: <input type="text" name="postName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_POST['postName']))
{
echo $_POST['postName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (isset($_POST['age']))
{
$age = $_POST['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
<hr>
<h2>GET Form</h2>
<h3>Would you like to volunteer for our program?</h3>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>">
<input type="hidden" name="p" value="includes/forms.php">
Name: <input type="text" name="getName">
Age: <input type="text" name="age"><br /><br />
<input type="submit">
</form>
<br />
Hello,
<?php
if (isset($_GET['getName']))
{
echo $_GET['getName'];
}
?>
!
<br>
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
if (isset($_GET['age']))
{
$age = $_GET ['age'];
if ($age >= 16)
{
echo "You are old enough to volunteer for our program!";
}
else
{
echo "Sorry, try again when you're 16 or older.";
}
}
}
?>
Please try this. I hope it will help.
Replace
if ($_SERVER['REQUEST_METHOD'] == "POST") {
with
if (isset($_POST['age'])) {
Similarly,Replace
if ($_SERVER['REQUEST_METHOD'] == "GET") {
with
if (isset($_GET['age'])) {
When you first enter on page, default REQUEST_METHOD is GET so you should check if isset($_GET['age']) {
and here check if it is more than 16
}
also you should check this one
echo $_GET['getName']; and change on this
echo isset($_GET['getName']) ? $_GET['name'] : "";
You should also check $_POST request like this and your program will work correctly.
SORRY FRIENDS FOR THIS QUESTION, I TESTED THE SAME CODE IN REAL TIME SERVER. IT WORKED FINE, BUT WHEN THE CODE RETURN IN PHPSTORM IT NOT WORKED.
(don't know exactly what the reason )
First.php
<form action="second.php" method="post" >
Name: <input type="text" name="name" >
<input type="submit">
</form>
second.php
<?php
if(isset($_POST["name"])){
$username = $_POST["name"];
echo $username;
}else
{
echo "null value";
}
?>
Here i am getting null value.
dont know what the reson.
but i simple testing this will GET (where method="get")
and
second.php is
<?php
echo $_GET["name"];
?>
it worked well.
Try this:
HTML
<form action="second.php" method="post" >
Name: <input type="text" name="name" >
<input type="submit">
PHP
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$username = $_POST["name"];
echo $username;
}else
{
echo "null value";
}
?>
i want to do somthing like:
1.php:
<html>
<form action=1.php method=POST enctype="multipart/form-data">
Choose a user name:<input type="text" name="username">
<input type="submit" value="Save and Proceed">
</html>
<?php
if(isset($_POST['username']))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="1.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
f( isset($_POST['age']))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
After the second form is submitted i do not want the script 1.php to run from the start but i want it to run the code following the form which is echoing the age only.
i do not want to go putting the later code in second script and accessing the variable of first script through making them session variables.please help. thankyou in advance
Change your condition to this
if ((isset($_POST['username'])) && ($_POST['submit'] == 'Save and Proceed')) {
}
this is the code i changed:
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
Choose a user name:</font> <input type="text" name="username">
<input type="submit" value="Save and Proceed">
</form>
</html>
<?php
if(isset($_POST['username']) && ($_POST['submit'] == 'Save and Proceed'))
{
$name=$_POST['username'];
echo $name;
if($name=='azra')
{
?>
<html>
<form method="POST" action="2.php" enctype="multipart/form-data"></br>
enter age:</font> <input type="text" name="age">
<input type="submit" value="done">
</form>
</html>
<?php
if( isset($_POST['age']) && ($_POST['submit'] == 'done'))
{
$age=$_POST['age'];
echo $age;
if($age==25)
{
echo "your name is" .$name;
echo "your age is ". $age;
echo"you are eligible";
}
}
}
}
?>
I have this piece of code and I want to show result in the third input box not in the input box and hole page and i want to know if i can a $_GET['$vairbles'] whiten functions
NOTE : I’m beginner in php programming
Here is my code :
<?php
$email = "mail#somedomain.com";
if (isset($_GET['txt1']) and isset($_GET['txt2'])) if (!empty($_GET['txt1']) and!empty($_GET['txt2'])) {
$tex1 = $_GET['txt1'];
$tex2 = $_GET['txt2'];
echo "They are all filled.<br>";
} else {
echo "Please fill in first and second Fields";
}
shwor();
Function shwor() {
global $email;
global $tex1;
global $tex2;
$len1 = strlen($tex1);
$len2 = strlen($tex2);
if ($len1 and $len2 > 0) if ($tex1 == $tex2) echo "matched ";
else echo "Does not match";
}
?>
<form action:"email.php" method="GET">
<input 1 type="text" name="txt1"><br><br>
<input 2 type="text" name="txt2"><br><br>
<input 3 type="text" name="Result" value = "<?php shwor();?>"><br><br>
<input type="submit" value="Check matches"><br>
</form>
everything works, just misspelled in this line:
<form action:"email.php" method="GET">
should be:
<form action="email.php" method="GET">
..and remember to name your file email.php that the data will be sent to the file itself ;-)