I have a problem in accessing session variables outside the code for submit button.when i am printing the session variable within submit code it is printing but at the time of echoing outside submit code it is not printing the value of date .Actually i want to insert the value of session variable in database but it is not getting inserted .the code is given below:
<!Doctype html>
<?php
session_start();
$_SESSION['date']='';
include 'connect.php';
?>
<form>
Date: <input type='date' name='date'> <br>
<input type='submit' name='submit'>
</form>
<?php
$date='';
if(isset($_POST['submit'] ))
{
$date=$_POST['date'];
$_SESSION['date']=$date;
echo $_SESSION['date'];
}
?>
<?php
echo $_SESSION['date'];
?>
It will not echo because you haven't set any form method and getting the values by $_POST.
By default it will take GET as form method.
So you can do two things
Set the form method to POST like this <form method="post"> or
Use $_GET instead of $_POST like this $_GET['submit'] and $_GET['date'].
The form doesn't have a method.
Set form method to POST
Related
So I have a PHP code similar to this
<?php
//some code here
?>
<form>
<input />
</form>
And according to this example, I want to get the input's content to use further in PHP and/or insert in the desired input some variables from my PHP code, how can I perform this?
You can post the data to php from a HTML form
<?php
// data is available in php POST array
var_dump($_POST);
// create a var to hold the post data
$sNameOfPostVar = "";
// if posted, set the var to the value of the posted content
if(isset($_POST['NameOfPostVar'])){
$sNameOfPostVar = $_POST['NameOfPostVar'];
}
?>
<!-- current action is blank to send to same page as php script -->
<form method='post' action=''>
<input name='NameOfPostVar' value='<?php echo $sNameOfPostVar;?>' />
</form>
I'm trying to add a value to $_POST data while it gets submitted to the target page as follows:
post.php
<?php $_POST['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
catch.php
<?php
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
but I cannot catch 'field1' on the other end. I don't want to use a hidden input field. How can I achieve that?
Thanks!
When you send the form, the $_POST data is reset and assumes only the inputs inside the form and a possible query string you may have appended to form action.
The best way to accomplish what you want is using hidden field but since you dont want it, you can append a query string to your form action:
<form method="post" action="catch.php?field1=Value1">
You're not submitting field1 anywhere. What happens is this:
post.php generates a HTML page (one that doesn't contain any reference to field1)
the user's browser renders the page
on submit, only the elements inside the form are submitted
catch.php receives the elements submitted above.
In other words, you need to get that value into your form:
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input name="field1" type="hidden" value="<?php echo htmlspecialchars($_POST['field1']) ?>"/>
<input type="submit" value="Submit" />
</form>
There is no other way to get the value into your POST data, if it's not present in the form. What you could do as a workaround is store the data in GET (size limit), session (concurrency issues - what happens when the user has two tabs open, each with different session data?), or cookies (size limit AND concurrency issues).
You can't do it this way. If you want to send the data you're trying to add to the POST there only through the users form, you are forced to also store it somewhere client side (e.g. a hidden field or a cookie). What you're doing right now is setting some value into the POST variable, but it gets overridden by the users form post (or rather the $_POST variable you're using after the form post is another instance).
What you could do instead to keep it serverside is save the value in the variable to the session of the user, then in the form post action server side get the value again (given the fact that you're using sessions). Lastly you could just store it in some table in a database, though I wouldn't do this.
Since $_POST are data sent by post method to script, you can not use it for another request directly. You need to compose and send another post request. The easiest way for you will be to use hidden input field/s.
Or you can choose another approach to make http post request, for example curl methods.
If you don't need data to be given by post method, you can save it in session, for example.
try this:
in post.php
<?php $_SESSION['field1'] = "Value1";?>
<html>
<head>
</head>
<body>
<form method="post" action="catch.php">
<input name="field2" type="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
in catch.php
<?php
if(isset($_SESSION['field1']))
{
$_POST['field1'] = $_SESSION['field1'];
unset($_SESSION['field1']);
}
foreach ($_POST as $key => $value) {
echo $key . " : ". $value;
echo "<br/>";
}
?>
Make sure you have started the session.
Note: you must use hidden elements or query string as other user suggested.
There are some inputs, and there is a function. The function requires these inputs, and the inputs are user-given. But, the buttons that fire the function and the input submission form are two different buttons. So, when the user presses "submit" to store his variables, the variables are stored fine. But, when he presses the "calculate" button (which fires the function), php says "undefined index" because it reads the $_POST of that input again and again.
If I disable register_globals, it does not show 'undefined index' but these values are 0 again.
If I use another file just to store these values and then redirect back to the page where the function button is, there is a redirect loop, require_once does not work.
What is the way to store the inputs in such way that they can be used again and again in functions and whatsoever? No databases, I need a way to store them in variables.
edit: the form: <label for="asdf">enter value:</label> <input type="text" id="asdf" name="asdf" value="<?php echo $asdf;?>" />
storing the value:
$asdf=$_POST['asdf'];
then I need to write $asdf in the function with the updated value that the user gave through the html form. How to do it? Cannot be much simpler
I would just store them in the session. That way they, they can be used across php scripts, but are not stored in the long-term. Here's an example:
form.php
<?php
session_start();
?>
<html>
<body>
<form action="store.php">
<input type="text" name="x" value="<?php echo $_SESSION['x'] ?>">
<input type="text" name="y" value="<?php echo $_SESSION['y'] ?>">
<input type="submit" value="Submit">
</form>
<form action="calculate.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
store.php
<?php
// Start the session
session_start();
$_SESSION["x"] = $_POST['x']; // substitute your input here
$_SESSION["y"] = $_POST['y']; // substitute your input here
?>
calculate.php
<?php
// Start the session
session_start();
$result = $_SESSION["x"] * $_SESSION["y"];
echo $result;
?>
There is no way to store them in variables. Every request to your server is a new request.
You could store the variables in a cookie/session or give them back after pushing the first button and store them in a hidden field in your html form. Or store them in a file on your server.
here i am getting a value from previous page with form here i assign the value to php variable $foodid i want to echo its value after the continue button is clicked
//its value is passed from the previous page form with action to this page
$foodid = $_REQUEST['foodid'];
//as soon as continue button is clicked i want to display $foodid
<form method="post" action="">
<input type="submit" name="continue" value="continue">
</form>
if(isset($_POST['continue'])){
echo $foodid;//here the foodid variable must be declared
}
PHP is a server side languaue
JAVASCRIPT - is a client side language
After redirecting to new page , you have the value with your self, but displaying it on click is possible with javascript only (will display the number without refreshing the page)
in PHP - its not impossible, but it does not make sense to redirect to same page with some additional parameters's to display
eg; on click continue , submit a form with no action and there form should have that input field with value which you want to display (can be in hidden type), it will get submitted to same page and you will get your value using
$_REQUEST['field_name'];
But its not recommended , use JS for this, people purposely use JS for such kind of things
You should pass the $foodid value through form to the php page. This can be done by declaring a hidden variable and assigning foodid value to it.
Try this
<?php
$foodid = $_REQUEST['foodid'];
?>
<form method="post" action="">
<input type="hidden" value="<?php echo $foodid ?>"
<input type="submit" name="continue" value="continue">
</form>
<?php
if(isset($_POST['continue'])){
echo $foodid = $_POST['foodid'];
}
I have a form and table like this.
What I'm trying to do is - If the select box is selected with a value, it is stored in session on submit(post) and i would like to retain the session value in select box.
If there is session value, it will display the session value in select box, else it will display all.
The below case is not working in my case.
What the below code do is - first time it doesn't set the session value. If I submit again(second time) the session is stored with the first value(first submit value) and it goes on like this.
Hope this question is answerable.
EDITED:
<?php
session_start();
if($_POST){
$_SESSION['book_id'] = $_POST['book_id'];
?>
<table class="table table-bordered" cellpadding="0" cellspacing="0" border="0">
My Table Content
</table>
<?php } ?>
<form method="post">
<?php print_r($_SESSION); ?>
<select name="book_id" class="form-control">
<option value="0">Select Book</option>
<?php while($row=mysql_fetch_array($book_query)){?>
<option <?php if(isset($_SESSION['book_id']) && $row['book_id'] == $_SESSION['book_id']) echo 'selected="selected"'; ?> value="<?php echo $row['book_id'];?>"><?php echo $row['book_name']; ?></option>
<?php }?>
</select>
<input type="submit" value="submit">
</form>
EDIT:
I have print_r for the session value.
What happens is - When I submit the form first time, the session value is empty.
When I submit it second time, the first submit value is stored in session, and it goes on.
Thanks,
Kimz
A few notes:
It seems you are mixing up book_id and book_name.
Also, you don't seem to submit a "book_name" in your form.
The "if($_POST)"-block is executed when the page loads, and not when the form is sent. So essentially, you first try to select something by a value in your session, and then afterwards store said value into your session.
First of all make sure you have session_start() included somewhere.
Secondly, try changing your IF on the options to this:
<?php if(isset($_SESSION['book_id']) && $row['book_id'] == $_SESSION['book_id']) echo 'selected="selected"'; ?>
Notice the isset. Without that on page load you are likely to get a PHP undefined index error.
Finally, try placing the 'if ($_POST) {}' before the form is displayed, so the session gets set before the form is displayed. Currently you wont see any changes until you refresh the page again, this is because the form is being rendered and then the session is set from the POST data afterwards.