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
Related
I am trying to get my page to redirect after deleting a record from the database but it wont redirect. It is echoing out text and also deleting the record but it just reloads me on the same page with a form that no longer has information in it.
Here is the button im using
echo '<form>';
echo '<br /><br /><input style="cursor:pointer" class="button" type="submit" value="Delete Event" name="delete" id="delete" />';
echo '</form>';
Here is the PHP im using to delete the row from the DB
if (isset($_POST['delete'])){
$mysqli->query('DELETE FROM information WHERE id = '.intval($_GET['id']));
header('Location: index.php');
}
I have no idea whats wrong with it.
The header doesn't work, probably because there's already another one.
I suggest you to use a little part of Javascript:
if (isset($_POST['delete'])){
$mysqli->query('DELETE FROM information WHERE id = '.intval($_GET['id']));
echo "<script type='text/javascript'>window.location.href = 'index.php'</script>";
}
I assume that both the code snippets are present in a single PHP file. Then, your form will by default post to the same file using GET method.
Change your PHP code to look like this:-
<?php
if (isset($_GET['delete'])){
$mysqli->query('DELETE FROM information WHERE id ='.intval($_GET['id']));
header('Location: index.php');
}
echo '<form>';
echo '<br /><br /><input style="cursor:pointer" class="button" type="submit" value="Delete Event" name="delete" id="delete" />';
echo '</form>';
?>
Your form tag is completely empty. You check if $_POST["delete"] is set, but you haven't even specified the method of communication between your form and your PHP file on your form. Try changing your starting form tag to this:
<form method = "POST" action = "YOUR PHP FILE.php">
Your PHP file seems fine as long as index.php is on the same level as the form.
You can also redirect your page to somewhere else this way:
echo "<meta http-equiv="refresh" content="1;url=index.php"/>"
<form>
Delete id <input type = "text" name ="deleteid" id ="deleteid"/>
<input type ="submit" name = "submit" />
</form>
<?php
if (isset($_POST['submit'])){
$id = $_POST['deleteid'];
$query = "DELETE FROM information WHERE id = $id";
//rest Execute the query
header('Location: xyx.php');
}
<?
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 trying to get the value from a textbox in another page I created and the textbox is automatically set to 0 but it keeps on telling me that the index is undefined? but once you click the confirm button the value gets submitted but I want it to automatically set as 0 without having to click the button.
This is page1:
<?php
if(isset($_POST['topscart']))
{
$_SESSION['topscart']=$_POST['topscart'];
print"<script language=javascript>
window.location='ACCOUNT.php';
</script>";
}
?>
This is page 2:
<td>No. of Female Tops Ordered: </td>
<td><center><?php
print" ".$_SESSION['topscart'];
?>
</td>
Textbox Code in page 1:
No. of Female Tops in Your Cart: <input type="text" value="0" size="10" name="topscart" id="topscart" >
Total: <input text="text" size="10" value="0" name="topstotal" id="topstotal" >
<input type ="submit" value="Confirm Order"></button>
<input type ="reset" id="topsbutton2" name="topsbutton2" value="Cancel Order"></button>
</center>
I couldn't quite understand the purpose but you must click the button to populate the $_POST variable.
If you must, set the value to 0 and automatically get it in $_POST then you need to use JS also - on page load fill-up the "topscart" with 0, and use .click() of the button.
Hope this helps
<?php
if(isset($_POST['topscart']))
{
session_start();
$_SESSION["topscart"] = $_POST['topscart'];
print"<script language=javascript>
window.location='ACCOUNT.php';
</script>";
}
?>
2nd page
<?php
session_start();
echo $_SESSION["topscart"];
?>
I think you want to default the session value to 0 unless it is set by the initial form? If that is the case then update the code on page 1 to:
<?php
if(isset($_POST['topscart']))
{
$_SESSION['topscart']=$_POST['topscart'];
print"<script language=javascript>
window.location='ACCOUNT.php';
</script>";
} else {
$_SESSION['topscart'] = 0;
}
every time i am refreshing the page and i am getting the same value stored in the post array.
i want execution of echo statement only after submit and after refreshing no echo results..
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
From just a form, you won't be able to check if it was a refresh, or a first submit, regardless of using GET or POST method.
To ensure a single message, you need to:
a. redirect the user to somewhere else after you processed the request.
if(isset($_POST['submit'])) {
// process data
header("Location: new-url");
}
And display the message on the other URL.
b. set a cookie / session variable, which tells you the form was already processed.
if(isset($_POST['submit']) && !isset($_SESSION['form_processed'])) {
$_SESSION['form_processed'] = true;
}
This second approach will kill your form until the user closes the browser, so you should do something more complex - like storing another hidden field in the form, and storing that in the session.
If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.
An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.
Within the form:
<input type="hidden" name="time" value="<?php echo $time; ?>" />
In the PHP:
session_start();
if(isset($_POST['submit']))
{
if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
{
echo "User name : <b> $name </b>";
}
}
$time = $_SESSION['time'] = time();
Another option is to redirect after processing the post data:
if(isset($_POST['submit']))
{
...
...
header('Location: ' . basename($_SERVER['PHP_SELF']));
exit();
}
You need to maintain a state as to whether $name has already been displayed or not. The easiest way is probably to maintain that state in a browser cookie.
<?php
$nonce = $_COOKIE['nonce'];
$new_nonce = mt_rand();
setcookie('nonce', $new_nonce);
if(isset($_POST['submit']) && $_POST['nonce'] == $nonce)
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="nonce" value="<?php echo $new_nonce ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Problems
you are polluting the user “session” with stale variable.
this will break if your user opens several windows (or tabs) to the same page. To fix this you would have to change the nonce cookie into an array of nonces, and update it accordingly.
if you want refresh page after submit use
<form method="get"
sure if your form hasn't a lot of data and also need to use $_GET instead of $_POST variable:)
correct way for you, but this logic is not good, need to refactor this script:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
unset($_POST['submit']);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
I have a form in index.php
<?php
echo '<form action="update_act.php" method="POST">';
echo '<input type="submit" name="'.$row['act_name'].'" value="edit">
echo </form>
?>
Here $row['act_name'] is a value fetched from database.
My update_act.php file is
<?php
echo "Old Activity Name : ".$_POST['$row[\'act_name\']'];
?>
But I am getting an error Undefined index: $row['act_name'] in C:\wamp\www\ps\activity\update_act.php.
I want to have different names for different submits but I am not able to get its value in another page. Is there any way for it?
I don't exactly know, what you're trying to do, but if the value of variable $row['act_name'] is same in both of the cases (form page and update script), then you can access to that this way:
echo "Old Activity Name : ".$_POST[$row['act_name']];
PHP only substitutes variables enclosed in double quotes " . What you want is:
echo "Old Activity Name : ". $_POST[$row['act_name']]
But your whole form does not make any sense. The output you get would be:
Old Activity Name : edit
because this is the value of the submit button.
Can you please clarify your question, what do you want to achieve? Here are some thoughts from my side:
Maybe what you want is more like:
<form action="update_act.php" method="POST">;
<input type="hidden" name="act_name" value="<?php echo $row[act_name] ?>" />
<input type="submit" name="submit" value="edit">
</form>
// ---- other file ---
<?php
echo "Old Activity Name : ".$_POST['act_name'];
?>
Why do you want to have different names for different submits ?
Do you want to differentiate between different actions? If so, it is easier you the buttons have the same name and you check against their values, e.g.:
<form action="update_act.php" method="POST">;
<input type="submit" name="submit" value="edit">
</form>
<form action="update_act.php" method="POST">;
<input type="submit" name="submit" value="delete">
</form>
and then
<?php
if($_POST['submit'] == 'edit') {
}
else if ($_POST['submit'] == 'delete') {
}
Use $_POST[$row['act_name']].
echo "Old Activity Name : ".$_POST['$row[\'act_name\']'];
should be
echo "Old Activity Name : ".$_POST["$row[act_name]"];
or
echo "Old Activity Name : ".$_POST[$row[act_name]];