I have variables $a and $location. However when I press the 'Submit' button the variable has gone out of scope. I can pass in in the variable from input box T2 but I can't manage to manage to do it to 'a' which is a regular variable. I added echo $b to debug it and I do get that output which verifies the conditional statement is true.
<?php
[...]
$a = '5';
$location = 'home';
if(isset($_POST['submit'])) {
$location=$_POST['T2'];
echo $location;
echo $a;
$b = '6';
echo $b;
[...]
}
?>
<input type="text" name="T2" value="<?php echo $location; ?>">
<button type="submit" name="submit" value="create">Submit</button>
$a="Testing";
echo $a;
if(isset($_POST['submit'])) { // you're not getting here
$location=$_POST['T2'];
echo $a;
echo "Testing one two three"; // even this won't show :)
}
There is only one plausible reason for that second echo not to work, i.e. Your if condition doesnt evaluate to true. There is no other reason that can cause that. Now check your field or button named submit on the form :)
A simple print_r($_POST); will tell you all that was posted, you can check there if submit was also posted.
Far as I can tell, you're missing the form tags along with the method which should be post, least from what you posted for code.
Sidenote: If what you posted isn't full code, do. The following works which printed home56 on submit.
I do believe that's what the expected result should be.
<?php
$a = '5';
$location = 'home';
if(isset($_POST['submit'])) {
$location=$_POST['T2'];
echo $location;
echo $a;
$b = '6';
echo $b;
}
?>
<form action="" method = "post">
<input type="text" name="T2" value="<?php echo $location; ?>">
<input type="submit" name="submit" value="create">
</form>
Sidenote: You can keep your present button if you wish instead of the input I tested with:
<button type="submit" name="submit" value="create">Submit</button>
Footnotes:
Both [...] - any relevance? I'll bet there is.
Got it to work by echoing it in a hidden input
<input type="hidden" name="myvar" value="<?php echo $a; ?>">
and then using $_POST to extract it
$a=$_POST['myvar'];
Related
I have a from that has NEW a hidden variable. Lets say the variable from previous submission was 2.
<?php echo $variable = 3;
echo $variable; //displays 3
?>
<form action="functions/store.php" method="post">
<input type="hidden" name="variable" value="<?php echo $variable ;?>">
<input type="submit" value="Submit" />
When running the code to store it in the data base, it stores the variable form the previus submission: 2. Why is that. The code I in store.php run is:
$variable= $_POST['variable'];
echo $variable; //displays 2. Should display 3
$stmt = $dbh->prepare("INSERT BlahBlahBlah... //other code
I have no idea what's going on even unset($variable) before $_POST does not work.
It worked fine for a while then, it started to behave like this. Even tried on other browser - same result
I have a form which submits data, and as a test I am attempting to first check if a variable is set on submit. If the variable is set, a text will be displayed stating something like "Variable is set". However if it isn't set, a text will be displayed saying "variable not set", once the variable hasn't been set it should then be set so next time when the form is submitted it displays variable is set however this is not working for me, for some reason it always displays that the variable is not set, my PHP code will be below:
<?php
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
I feel really stupid for not being able to do something which seems so easy, I am only just learning and sort of trying to teach myself, thank you for any help provided!!!
Working code and explanation:
<?php
$test="";
if (isset($_POST["text"])) { //always directly check $_POST,$_GET var without assigning
echo "This var is set";
$test=$_POST["text"]; // then assign
}
else{ // and use else clause for otherwise case
echo "This var is not set";
$test = 'set'; // AND if you want set to default/custom value in case of not set.
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off">
<br /><br />
<input type="submit" value="submit">
</form>
If you are using form to submit values, then try this one,
if (isset($_POST['text'])) {
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
Otherwise, If a variable set to empty value like $test = '';
(It means variable is set but it has no values) It will execute your first if condition only.
<?php
$test=$_GET["text"];
if (isset($test)) {
echo "This var is set";
}
if (!isset($test)) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="#" method="get">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
You haven't declared the variable $test.
Unless you've still got a bit of PHP somewhere that you haven't included here, your variable is empty. When a form is submitted, the input will be added to either the $_POST array (for method = "post") or else the $_GET array (for method = "get").
To Fix:
<?php
if (isset($_POST['text'])) {
$test = $_POST['text'];
echo "This var is set";
}
if (!isset($_POST['text'])) {
echo "This var is not set";
$test = 'set';
}
?>
<form action="" method="post">
<input type="text" id="text" name="text" autocomplete="off"><br><br>
<input type="submit" value="submit">
</form>
Is there a way I can pass a variable from one page to another but it only work on the next page clicked? After that it can be discarded/destroyed.
I've tried a php session but can't seem to kill the session on the next page clicked... or even if that way may be the wrong way to approach it.
Here's my session code:
<?php
session_start();
$x = $category;
$_SESSION['sessionVar'] = $x;
echo "$x";
?>
<?php
session_start();
$x = $_SESSION['sessionVar'];
echo "$x";
?>
I want to do this without having to submit a form.
You can pass a variable via a php session and then unset the variable on the following page.
$_SESSION['my_var'] = "some data";
$_SESSION['my_var'] = null;
unset($_SESSION['my_var']);
Send it as POST:
<form action="nextpage.php" method="post">
<input type="hidden" name="yourvariable" value="12345" />
<input type="submit" name="submit" value="Next page" />
</form>
Is it what was supposed?
I am using one session variable in my php page. As per my infomation, it is accessible throughout the program and it is, but problem is that it is showing different value for the same variable at different place in php page?
the code is as follows
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form>
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
value becomes different before and after submit button click? Please tell me why it is so?
thanks in advavnce.
You are redeclaring the value of session variable 'x' here
$_SESSION['x'] = $_SESSION['x']+1;
This is why its appearing 1 greater than its initial value.
it is due to the code itself
if(isset($_SESSION['x'])) //It is set
$_SESSION['x'] = $_SESSION['x']+1; //Add 1 to the value
echo $_SESSION['x']."<br>"; return value with +1
Solution
The reason the output is different is the order you echo and update
//Echo
//Update Value
//Echo again
Simple solution would be to move this
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
to above this
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
Also note set the method and the action in the form to make sure it calls itself
<form method="GET" action="[url to itself]">
<input type="submit" name="save" value="save" />
</form>
Do it like this :
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form method="GET" action="">
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
this way the code prints out the same value after submit as it did before.
Either way you try if you print value and change after or change value and print after, when page reloads it will change value. you could add another button called increment and add the following code inside the php :
if (isset($_GET['inc']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
}
and this one inside the form:
<input type="submit" name="inc" value="inc" />
this way youre variable increment when you press the inc button
I have a problem here saving a value on a textbox using session.
I enter a value on one of the textbox and the other one has none, I submit it. The value will come out on the first textbox and will disable the textbox, but when i put a value on the second textbox and submit it, the value on the first textbox has gone. Can anyone help me with this problem?
PHP
<?php
session_start();
#$two = $_POST['two'];
#$four = $_POST['four'];
if(isset($_POST['submit'])){
$_SESSION['two'] = $two;
$_SESSION['four'] = $four;
}
?>
HTML
<form method="POST">
Textbox One: <input type="text" value="<?=#$_SESSION['two']; ?>" name="two" class="inputborder" size="20" <?phpif(#$_SESSION['two']){ echo"disabled=disabled";} else{echo ""; }?> />
<br>
Textbox Two: <input type="text" value="<?=#$_SESSION['four']; ?>" name="four" class="inputborder" size="20" <?php if(#$_SESSION['four']){ echo"disabled=disabled";}else{ echo ""; }?> />
<br>
<input type="submit" name="submit">
</form>
Why are you trying to grab the variables for $two and $four before checking submit?
<?php
session_start();
if(isset($_POST['submit'])){
if(isset($_POST['two'])){
$two = $_POST['two'];
}
if(isset($_POST['four'])){
$four = $_POST['four'];
}
$_SESSION['two'] = $two;
$_SESSION['four'] = $four;
}
?>
That should remove the need for the '#' symbol in the PHP. (although further error handling would be appropriate). But you are suppressing errors everywhere, how are you supposed to get feedback?