how to maintain data in text box when page reloads in codeigniter - php

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

Related

Store value on page refresh

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

PHP - return the same page with changes

I have html page where you can insert some information and then submit this form, which will change information in database. I do it normally, that submit button call php file in server.
But what I want, is that this php file will return to me the same html page of which I sent request, with modified changes. e.g: there will be "Database update successfully" text added etc.
How can I do it without AJAX ?
Thanks
In the PHP file, do a call to the header() function to redirect the user. For example:
header('Location: url.php');
To change the content of that page they are redirected to, you could pass something in the URL that your page will check for. For example:
header('Location: url.php?submitted=1');
There are other ways to implement this, but this seems the most straightforward to me. Note that you don't want to call header() until the end of your submission page.
Use POST/REDIRECT/GET
Excerpt:
The user submits the form
This is pretty straight forward. The user completes the form and submits it by pressing the submit button or enter on their keyboard.
We store the form data in a session
After processing the data we discover an error so we need to redisplay the form with an error message but we also want to populate
it with their data so they don't have to refill the entire form just
to fix potentially one little mistake. So we store their data in a
session ($_SESSION). Session variables carry over from page-to-page
for as long as the session is valid or until they are deleted. This is
an ideal place to put their information since redirecting will cause
their information to be immediately discarded by the server.
We redirect the user back to the same page using a 303 redirect
Once we have saved the user's information in their session we need to redirect them back to the same page. In order for this to work
properly we need to use a 303 redirect. This means we need to send a
303 header with our redirect. A 303 redirect will cause the browser to
reload the page without the initial HTTP POST request to be
resubmitted. This includes when the user uses the back or refresh
buttons.
We re-populate the form using the data stored in the session
When the page is sent to the user we re-populate it with their information we saved in their session.
Only by generating the whole page in CGI first, unless you go through some horribly convoluted method of getting value of one of the fields to be set to document.innerHTML or something like that in Javascript. But you'll go through hell to get the quoting issues resolved. Use AJAX, it was created for precisely this purpose and exactly to avoid the utter hell associated with what you need.
Alternatively: the "modified piece" of the page may be an iframe, and you can set the target attribute of the form, so that the PHP returns only the iframe content.

Avoid resending forms on php pages

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.

Does this protect me against page reload and the back button?

Further to my previous question, here's what I decided to implement; it may not be pure P-R-G, but it seems ok. Care to comment?
The form.php has an action; let's call it validate.php.
validate.php is never seen by the user; if validates all $_GET and, if valid writes it to database and generates the HTML of a confirmation page / if not valid, it generates the HTML of an error page explaining what is wrong.
Whichever HTML is generated get stored in a $_SESSION variable and then validate.php does a header('Location: <as appropriate>);
Finally a page called submitted.php of invalid_input.php (in case the user reads the URL) consists only of echo $_SESSION['form_html'];
That seems to me like is proff against both page reload and back button problems.
Or did I goof by trying to reinvent the wheel?
Firstly, you're better off storing the form data, which means you can perform the validation again. It will also be less html. The problem with the method you're employing now is that it doesn't protect against multiple tabs, since $_SESSION is universal to a browser session.
One way I've used to prevent against duplicate submission (without PRG) is to generate a unique id for every page load (where a form is involved). When I generate that unique id, I add it to a $_SESSION['form_unique_ids'] array, and I include it as a hidden field in every form I generate. Then before I take action on a form submission, I check to see if that unique id is in the session. If it is, this is the first time that form has been submitted, and I remove it from the session. That way if I try to resubmit that page, I will know because that id is not in the session not to process the results.
This could be extended so that instead of storing a single id, you use the id as the key in the array, and let the value be the result of the transaction. Then:
If there are errors, you store the $_POST data as well. Then, redirect to original_form.php?id=unique_id and display the validation results. You can either store them or recalculate them there.
If there is success, store the success message and redirect to success_page.php?id=unique_id. Display the success message prominently there. If you like, you can remove it from the page.
You have the option of removing the session data when you display it, but that would mean if they refreshed the edit page they'd lose the validation messages and saved form data. I'd rather find a way to get rid of data that is old enough that they're not likely to need it anymore.
Anyway, some of those ideas might be useful. Then again, maybe it's way too much effort for the problem. Your call :)
As long as you use a php redirect at the end of your validate you cannot reload or back button into the validate.php

User sessions with jquery and Ajax

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.

Categories