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>
Related
<?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>
I have a basic form to upload an image:
<?php
if(!empty($_POST['?upload_img'])) {
echo "true<br>";
} else { echo "false<br>"; }
?>
<html>
<form action='' method='post' enctype='multipart/form-data'>
<input type='file' name='input_img' id='input_img'><br>
<input type='submit' value='Upload Image' name='?upload_img'>
</form>
</html>
I would like a PHP boolean/evaluation pair to run only once for each upload.
I have tried evaluating using !empty,isset,sizeof(foo)>0
I have also tried to find a solution using $_SESSIONs
Is there are correct use of unset or $_POST=array()/$_FILE=array() that I have missed?
I can achieve the desired behaviour with sessions:
<?php
session_start();
if(!empty($_SESSION['foo'])) {
echo "true<br>";
} else { echo "false<br>"; }
?>
<html>
<?php
if(!sizeof($_SESSION['foo'])) {
$_SESSION['foo']=array();
$_SESSION['foo']['bar']="path/to/the/file.png";
} else { $_SESSION=array(); }
?>
</html>
How can I achieve the same with input from a <form> instead?
Any help is much appreciated!
For completeness, an outline approach specific to my issue:
<!--filename:foo.php-->
<?php
session_start();
if(isset($_POST['?confirm'])) {
$_SESSION['input']=$_FILES['input'];
header("HTTP/1.1 303");
header("Location: http://$_SERVER[HTTP_HOST]/foo.php");
die;
} else if(!empty($_SESSION['input'])) {
echo "true<br>";
session_unset();
session_destroy();
} else { echo "false<br>"; }
?>
<html>
<form action='foo.php' method='post' enctype='multipart/form-data'>
<input type='file' name='input' id='input'><br>
<input type='submit' value='confirm' name='?confirm'>
</form>
</html>
Based on Post-Redirect-Get Pattern in PHP
use a token
$token = md5(session_id().mt_rand().time());
$_SESSION['token'] = $token;
<input type="hidden" name="token" value="<?php echo $token; ?>" />
later when you process the image delete the token with unset()
<?php
session_start();
?>
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<?php
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
?>
<input type="submit" value="submit" />
</form>
2nd page
<?php
session_start();
?>
<?php
if (isset($_SESSION['userGet'])) {
$form_value = $_SESSION['userGet'];
}
echo $form_value ;
?>
..........................................................................................................
You have used the text box name with that check the submit button name too.
<form action="HardDisk.php" method="post">//form
<input type="text" name="user" />
<input type="submit" value="submit" name="submit" />
</form>
<?php
session_start();
if(isset($_POST['submit'])&&isset($_POST['user'])) {
$_SESSION['userGet']=$_POST['user'];
}
?>
And in your second page
<?php
session_start();
if (isset($_SESSION['userGet'])) {
echo $form_value = $_SESSION['userGet'];
}
Give the submit button a name of submit then use the following PHP code:
if(isset($_POST['submit'])){
if(isset($_POST['user']))
{
$_SESSION['userGet']=$_POST['user'];
}
}
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 a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>