Is there any way to store the value in session on page refresh?
For e.g.
If I write the "abc" in textbox, and when I refresh the page, it will store in session.
Updated:
Actually, If I write the "abc" in textbox then I will go on 2nd page of pagination without ajax (that is on-loading).
When I come back to 1st page, the value of textbox remain.
This is what I want to say.
What have you tried so far? It's very basic question
just session_start(); and $_SESSION['abc'] = $_POST['abc']; if you send it via post and
<input `name="abc" value="<?= isset($_SESSION['abc']) ? $_SESSION['abc'] : 'default value' ?>" >`;
Edit:
If you hit f5 it won't be send as POST, you could check if f5 is hit and send it VIA ajax or you could send input after blur event when input is not empty, there are many possiblities. Check capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116
Also the other option is to save value in JavaScript in cookie after value is entered or input is blured and then when you are back on the site check if this value exists and show it.
I don't think this is possible. A refresh happens on the client side. Without any requests send to the server to be able to process.
To get a request to your server you either need a post or a get and refresh is neither of them.
AS #Robert already suggested in his comment, you will need more then just php to solve your problem (i.e. ajax)
im not sure i just found this maybe it can help you. You can use jquery and ajax. Using jquery you can detect if page is refreshed
$('body').bind('beforeunload',function(){
//get your textbox value and send it to php file and you can start your session there.
});
Link
Related
I am creating a system in php to get user's feedback on solutions. I am using MySQL to get the solution and store the feedback. The feedback are integers. There are two columns for feedback- Yes and No. I want to increase the value of them when a user clicks on a text link/radio button or checkbox.
Like-
.................Solution..............
Did this help you? Yes / No
When user clicks on Yes, it should increase value of yes column by one and if it clicks on No, it should increase value of no column by one.
The main difficulty is this that the page contains numbers of solution and feedback options..
Can you help me??
Clicking a form control on a page is a client-side action. Updating a variable in PHP is a server-side action. Luckily, AJAX gives us a way of accessing server resources from the client.
The following code performs an AJAX request using the jQuery JavaScript library. You do not need jQuery to perform AJAX requests, but then you must code your AJAX request natively. Doing that is not hard, but it's not exactly trivial, either.
JavaScript:
$.ajax({
url: '/path/to/file.php',
data: 'url=encoded&query=string', // Can also be an object
success: function( output ) {
// This function gets called once the AJAX Request returns. It is sent
// a string parameter containing all output of the Server-side script
}
});
In the file.php, you just perform whatever actions you want to. The AJAX request can be sent either via GET or POST, and any variables passed will be available in the $_GET and $_POST superglobals, respectively.
The output parameter is passed in is a string, and is all of the output (i.e. echo/print) that is generated from the server-side script. That string can be a JSON representation of an object as well, in which case you will have to parse the string to be able to use it as an object in JavaScript. This is beneficial if your server-side script returns large amounts of data instead of just plain HTML/XHTML.
For more information, look at jQuery's documentation for AJAX, or Google for native ajax requests
An alternative is just to have your controls as regular form elements which submit the form when they are pressed (e.g., each one of them is a named submit button / image input). Your form handler will determine which one is pressed depending on what value is present in the $_POST. This method will then send the user input to a form handler which gets executed on the server, which then redirects the user back to the page once it has finished processing.
PHP is server side only, so when the user clicks on Yes, it needs to send a request to load a new page, an Ajax request, etc. that will then update the server. Anything done on the page after page load can not be done with PHP.
I would attach $_POST/$_GET requests to each link/radio button that is then sent to MySQL on a page refresh or something to that effect in order to update your table.
how to maintain data in text box when page reloads in codeigniter.
Example:
Suppose that I fill up the username and password and I refresh the page then the username and password’s fields should not be blank, they should contain the values before the reload
If this cannot be done in codeigniter what is the normal php method
I think you must use cookies or ajax fire on text changed of your textbox.
If you use ajax, you can then store values insides session.
Do it on client side.
Save the value of textbox to a cookie value on each key press.
When the page is loaded, check the cookie for a value, if there is, set it on the box.
(Don't forget to flush the value, after submitting data or using for whatever reason you are using)
By refresh do you mean upon submitting and failing on validation refreshing the page or simply just refreshing ?? If ur talking about a simple refresh then I Doubt there is any method to do it in CI or php. Yeah you could do it by client side scripting (js) and writing it to a cookie or make an ajax call to php script and save it to a session variable. And yeah do flush those values once saved as marvin said
Is there a way to avoid reprocessing forms when I refresh php pages? I'd like to prevent resending forms when refreshing links to php files with an insert function in them. For example, I am processing a series of notes written by users at the top of each page for a new note. Besides the obvious creating a separate php file with a header function is there another way to do it?
Use the Post-Redirect-Get Pattern.
Accept a Post request
Process the data
Issue a redirect response
Accept a Get request
Issue a 200 response
If you need to display data from the submitted stuff, then include a row id or similar in (for example) the query string of the URL you redirect to.
The best way would be to do a header("location: form.php"); call after you process the form. That would redirect you back to the form page, and if you refresh, the browser wont resend the form data.
Alternatively, you could check to see if you already processed the data received, but that would still give you the browser warning message that you are going to resend the data.
You might do both, just in case someone uses the back button and accidentally clicks Submit again.
Just set some flag when you process the form first time so you could check for it and abort reprocessing later on. Session variable or cookie will work fine.
You could put a nonce into the page that is only allowed to be used once so that if you see the same nonce come in you don't do the insert of the page.
I redirect users to a new page after processing of the form.
The form is a POST-request to do-something.php. I check the input data and if it validates I process the data and perform a redirect to do-something.php?somethingdone. So the user can hit F5 w/o resending the POST request.
I tried to use header("Location:..."), $_POST = array(), unset($_POST), but (idk why) they didn't work in my php page.
So, what I did, I just used
echo '< script>window.location.replace("http://.../this.php")</script>'
😂 it works very good! Maybe it is not a good idea, I am learning PHP for the 4th week.
I am using 9 checkboxes to get input from user and using POST method to get the data. The problem is that when I try to reload that page, the browser shows me this message-
"To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier."
with 2 options, RESEND CANCEL. Please tell me what should I do. Can't use GET as it displays my whole search query.
If you are perform some search to get information, I recommend to just use GET. POST-REDIRECT-GET will also display your search query.
If you use post, the browser will confirm that you really want to do a post once more.
Use GET to get data, use POST to operate the data, in my personal opinion.
Quick and dirty: Store all the values of the check boxes in a $_Session[] array and check for that first.
if($_SESSION["CheckBox1"] === "on")
{
// Do Stuff
}
else
{
// Get $_POST[] Data and do stuff
$_SESSION["CheckBox1"] = $_POST["CheckBox1"];
...
}
When the user submits the Form you first check if the $_SESSION has data otherwise put data in it and go about your normal work. If a reload happens then the $_SESSION values are used and not the now empty $_POST array.
After posting the data, Redirect to the current page. That will cancel out the resend/cancel issue.
If your website is User-Profile based, you can have a look at the users data and return selected checked boxes.
OR
You can set a cookie value, via javascript [on checkbox click] or backend [when posting], this way you can store checked checkboxes and return it clicked on reload.
This only happen when you try to REFRESH page after POST Request.
Bowser asks you what should it do: resend POST data or simply refresh page without POSTING (send GET only).
The same behaviour on Chrome. Opera doesn't asks, just resend previously sent POST data by default.
Changed the method request type from POST to GET in my search form and got rid of the confirmation box..
I am using jquery to set a session, i have a php page which gets the values of the person logging. The value in the session array, is then used in another page where, it is stored in a hidden field for database entry.The problem is, the value is not set unless you refresh the page of which beats the purpose of AJAX and Jquery.Again,the session seems to be one session behind.How can I do this without page refresh/ reload?
It sounds like you are doing this...
1) User types some stuff in
2) AJAX requests fires that stuff off to the server
And it sounds like you need to add:
3) Using the result of step 2, set the value on the current page
ok, so if I get this straight, you use ajax to let a user log in on one page, and on another page you want some magic value from the session to be filled in? Is this other page already open at the point the user logs in? e.g:
user opens two windows, one to "loginpage.php" and the other to "formpage.php"
user logs in, data sent to server via ajax, magic value created in session
user switches to "formpage.php" window, fills in the form there, and submits
server expects magic value to be returned in hidden field, but it's not there
something like that? If that's the case, there's no way for the "formpage" window to know that you've logged in via the other window. The server can't 'push' a login notification to it, and generally javascript in one window can't affect the contents of another window, unless that window was created by the first window to begin with.
You could have the "Formpage" window poll the server to see if the user's logged in via another window, and then request this magic value and dynamically fill in the hidden field. But otherwise you'd have to refresh the page to get that hidden form field filled in.