php - How could I keep the value of an input field unchanged by switching between webpages and not using the URL
Get the solution ↓↓↓I am using two buttons to switch between pages. Button A in page1 sends me to page2. In page2 I have another button, which is button B. This Button sends me to page1 again. I do that many times.
In page1 I have an input (textarea), that I have filled out before hitting Button A. I would like to keep the value in that input(in page1) while going and coming between the two pages.
I could also modified my input in page1 when I come back from page2.
I don't want to use the URL. I have already been using it for other purpose.
I have also tried this, but it does not work.
This is page1.php
<textarea name="textarea" id="textarea">
<?php if(isset($_POST['textarea'])) {
echo $_POST['textarea']; ?>
</textarea>
<a href="page2.php" id="button1"> Button A </a>
and here is page2.php
<a href="page1.php">Button B </a>
When I do it with SESSION it gets empty when I come back to page1.
Then I have tried it using ajax. -> in page1.php
<?php
if(isset($_POST['actual_val']) and strlen($_POST['actual_val'])>5)
{
echo $_POST['actual_val'];
}else{
echo "nothing";
}
?>
<html>
<textarea name="textarea" id="textarea"><?php if(isset($_POST['actual_val'])) {
echo $_POST['actual_val']; }//closing brace of if
?></textarea>
<a href="page2.php" id="button1"> Button A </a>
<script>
$(document).ready(function(){
$('#button1').on('click',function(){
var input_val = $('#textarea').val();
var ajaxurl = 'page2.php';
data = {"input_val":input_val};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
});
});
});
</script>
//Until here it seems to works fine.
</html>
Now in page2.php
<?php
if(isset($_POST['input_val']))
{
echo $_POST['input_val'];
}else{
echo "nothing";
}
?>
<html>
<a href="page1.php" id="button2"> Button B </a>
<script>
$(document).ready(function(){
$('#button2').on('click',function(){
var actual_val= <?php echo $_POST['input_val']; ?>;
var ajaxurl = 'page1.php';
data = {"actual_val":actual_val};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
});
});
});
</script>
</html>
what would I be missing?
Answer
Solution:
I am doing this by just adding a form and using basic HTML and PHP, If its okay then good otherwise please give more scenario, we will implement that also. May be it will help you. There are many ways, I am doing this by basic one.
page1.php
<?php
if(isset($_POST['actual_val']) and strlen($_POST['actual_val'])>5)
{
echo $_POST['actual_val'];
}else{
echo "nothing";
}
?>
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="input_val" id="textarea">
<?php if(isset($_POST['actual_val'])) {
echo $_POST['actual_val']; }?>
</textarea>
<button type="submit" href="" id="button2"> Button A </button>
<form>
<script>
</script>
</body>
</html>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: please make sure the php redis extension is installed and enabled.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.