0

I want to keep the values of a form after submitting and I'm working with php. I tried this :

 <input type="text" name="title"  id="title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ;?>">

but I think it just work when I don't use header("location:something.php") after submitting. I know I can use sessions for this purpose but I wanted to know is there any ways to do this without using sessions?

Atena Dadkhah
  • 623
  • 1
  • 5
  • 19
  • 1
    The other way is to include the variables in the querystring of the URL you redirect to. Or store them in a database or other cache, and include the ID of the storage record in the URL you redirect to, and then retrieve the data in the other script. There are probably some other ways too, if you get imaginative. Basically it's all about maintaining state – ADyson Feb 03 '22 at 19:43
  • There are couple of way to prevent the form values from getting autoclear when submitted such as `e.preventDefault();` via javascript here's are more alternative solution for your concern https://stackoverflow.com/questions/19454310/stop-form-refreshing-page-on-submit – Emma Marshall Feb 03 '22 at 23:25
  • Emma Marshall's point is good but it perhaps needs clarifying that this only applies if you are using AJAX to submit the data. That is another totally different option instead of posting back and redirecting. It's worth considering. – ADyson Feb 04 '22 at 09:00
  • Yes. I'm not using ajax. But I've seen the ways using ajax. Do you know any library or something like this for this purpose? – Atena Dadkhah Feb 04 '22 at 09:37
  • What do you mean? AJAX functions are built into your browser (there's fetch(), or the legacy XmlHttpRequest). There are 3rd party libraries such as axios or jQuery which attempt to produce a nicer AJAX interface for developers, but they're not essential. – ADyson Feb 04 '22 at 09:39
  • Using AJAX requires a bit of a rethink of your overall page design, such that you're expecting to stay on the same page when you submit the form, and show any results (success or errors) within that same page, based on the results of the AJAX request. And you have to write JS code to show those results - PHP would only provide some data to indicate whether the form submission suceeded or not. So no redirecting, no page reloads, no generating HTML via PHP etc. It's a great technology but it does require you to think differently about how your site's user interface is implemented. – ADyson Feb 04 '22 at 09:41
  • I mean as I don't use AJAX and javascript for this process, I can't use e.preventDefault();. Is there any library in *php* to handle this? – Atena Dadkhah Feb 04 '22 at 09:49
  • Well, recommendations for libraries are off-topic here anyway (see the [help/on-topic]) but in any case, I would guess not, specifically, because exactly how it would work depends on exactly what you want to end up doing and how the rest of your system is working. I already gave you some possible options for implementing this in my first comment. First you need to decide what you want to do and how you want it to work, and then you can worry about implementing it – ADyson Feb 04 '22 at 09:57
  • About your solutions because the form consists mostly of textareas so I don't think I can include them in querystring cause the values are large or if I want to store them in database just for keep them again, I think over time their number will increase a lot. – Atena Dadkhah Feb 04 '22 at 10:20
  • Yes the number could increase a lot, but it would take a _lot_ of data (like 10s of millions of rows) before it becomes problematic for storage or efficiency, on modern hardware. And you could always timestamp each row, and then have a cron job which clears out the older ones periodically. Or just use the Session, so then it's taken care of automatically - the data will just expire when the session ends. Or use some other caching solution like Redis or something. There are so many possibilities. – ADyson Feb 04 '22 at 10:31

0 Answers0