How to pass date picker from one page to another - php

how can i pass a date picker from one page to another page and according to that how can I fetch data from database using PHP to display it?
UPDATE
Firstly, i just want to said that i use PDO to access the database.
Second, i dont use session method because i just want to make it simple.
after read the comment, i thing that i need to learn about get and post method. After learn about get and post method, now i can send the data from one page to other page. i also can sent data in form that phpmyadmin can read.
Thanks.

You will have to submit data when going on to another page. Date picker is essentially a front-end handler for a form input. As any form element it needs to be submitted (via GET or POST). On you receiving script (form action=). You can either, store it to DB as any other variable value as it will be available in $_GET or $_POST as a text variable, or just use that value to populate your date picker on the next page if you do not need to store it on this step.
If you need any additional help with any of the steps: form submission, DB queries, populating date pickers, please show us your attempts to achieve the result and we will be able to guide you towards the right direction.

You can get datepicker value depends your methods post and get and your input name.
for example if your code is
<form method="post" action="resp.php">
<input class="timepicker logs-datepicker" name="from_date" value="" type="text">
<input type="submit">
</form>
in resp.php page
$time = $_POST['from_date'];
and for change to timestamp
$time = strtotime($time,' 00:00:00');

Related

How to clear an HTML Form, even the form's initial values?

I have an HTML form that sends information via post to a PHP file.
On the user's second visit the page should remember the last search input. So if on their first visit they were looking for pencil then on their second visit, the form would already have prefilled the Product Name input with pencil. I'm doing this via a session variable that is shared between the two files.
For example this is what my code looks like:
<label for="minPrice">Minimum Price</label>
<input id="minPrice" type="text" value="<?php echo $_SESSION['minPrice'];?>" name="minPrice">
<input class="clearForm" type="reset" value="Clear Form">
As you can see, I'm setting the value of the input field using the session variable. Which means the initial value on the second visit of the input will be the value of $_SESSION['minPrice'], so the typical type="reset" for clearing forms doesn't work. Reset just resets the form to it's initial values.
My first thought was to unset the session variables, but that wouldn't change the current values in the input fields of the form.
There are 2 ways to make it happen
Using PHP session the correct way
Using Javascript local storage
Using PHP sessions
Make sure your .php file has session_start() at the top.
Now you need to request the server to save the value(s) you wanna use on "the next visit". This means, requesting the server without refreshing the page through an HTML form submit, using AJAX.
Following JS snippet will post a form to the server, you can modify what to post as easily as eating an apple pie.
fetch(url, {method: 'POST', body: new FormData(form)})
But you have to POST when the user types something so add an eventListener that triggers the fetch method.
document.getElementById('minPrice').addEventListener('keydown', () => {fetch...})
url is the name of the file or the url you wanna POST to,
form is the form you wanna submit, in case you wanna submit some input field(s) alone, replace new FormData(form) by {minPrice: document.getElementById('minPrice').value} and so on.
assign the fetch method to a variable and you can get the server's response using
variable.then(res => res.json()).then(response => //do whatever you want)
On the server side, get the value(s) using the superGlobal $_POST, such as $_POST['minPrice'] and you can save it in the $_SESSION['minPrice'] variable and whenever the user reloads or makes a second visit, the $_SESSION['minPrice '] will assign the last written minPrice to the input field.
Using Javascript local storage
localStorage is built-into javascript and is quite easier to use. Read more about localStorage on MDN docs. Use
localStorage.setItem('minPrice', document.getElementById('minPrice').value)
And assign the localStorage value to the field on every page load.
document.getElementById('minPrice').value = localStorage.getItem('minPrice')
That's it!
Take a look at this !
Make page to tell browser not to cache/preserve input values
Stop browser from filling textboxes with details
Alternatively, try adding this in Jquery :
$("form :input").attr("autocomplete", "off");
Use JavaScript to clear out the values of the form fields.
Something like:
<button onclick="() => {
document.querySelectorAll('input').value = '';
}" />
That way when you click the reset button, it sets all inputs value to empty string.
If you're never going to want the field autofilled by the browser it seems like you'd simply want to use the autocomplete="off" flag on the input field you desire to be dynamically filled by your php script.
You can read more about the specific of this on the MDN docs.
Basically though you'd take the input, store it as a session variable, load the next page and populate the search variable into the input field as a value and turn the autocomplete functionality off so that the browser cannot override the value you provide from the session value.
The support for for this seems fairly broad. and should in most cases prevent the browser from overriding whatever it has stored for the field.
If you're still running into issues with it filling you cvould maybe look to adding some javascript functionality with the reset() function. However depending on how this is fired it might actually end up overriding whatever you populate with the PHP function at the time the DOM is actuall rendered

How to add date and time when html form is submitted in PHP pure scripting?

I am making a basic post-box management system in PHP. the postman can add 2 inputs which are 'delivered by' and 'item number'. and a button for the postman to click to submit the inputs.
Now, when the submit button is clicked, I want PHP to record the date and time of when the inputs are being submitted (so that later on I can set a mechanism to track the item duration and see how long an item has been in the box-but this is not part of the question). How do I do that?
sorry I have just started learning PHP so my current code is still very basic, and I haven't start with the Box-function.php page yet:
The HTML page:
<form method="post" action="Box-function.php">
Delivered by : <input type="text" name="Delivered-by"> <br>
Item No : <input type="text" name="Item-no"> <br>
<input type = "submit">
</form>
Note: I have seen similar questions but those questions involve using MySQL while I am not using it. it's just pure PHP and HTML.
In the box-function.php page, you can get the current date and store it along with the other data.
Getting current date
Do the same when the order is delivered. The difference between the two times will tell you how long the package took to be delivered.
Also, as the comments under your post say, you will have to store the data somewhere. You can't retrieve it later if it isn't stored anywhere.

Unable to pre populate form with url

I am trying to pre-populate a set of form fields by passing info via parameters in the URL. I have been able to do this with html forms before by simply adding the parameters to the URL, for example ?name=John. The variable I enter usually appears in the form field.
I am finding that this approach is not working on the latest form. I have been able to identify the parameter names but when I add them to the end of the URL they are not populated in to the form.
For example using website.co.uk/admin/usersearch.php?email=test#test.com I would expect the email field to be populated with test#test.com but the page refreshes and the form is still blank.
Is this because it is a .php form? Is there anyway round this? I only have the options to use the URL or javascript.
Thanks
Give your field value as <?php echo $_GET['email'];?>
Like this :
<input type="text" name="email" value="<?php echo $_GET['email'];?>" />
There is no such default procedure for pre-populating form fields built in to any web server. So, I'm not sure how you got it working earlier. Maybe the developer had actually coded it such that the form pre-population occurred.
For the new form, you could do as Prasanth suggested. However, since you require only JavaScript or HTML, refer to this prior question for further assistance: How to retrieve GET parameters from javascript?
Basically, what you'll be doing is getting the value of the field from the url and setting the field's value to it in the form using JavaScript.

Saving multiple $_POST from a single text form

I have a page with a single form, and a submit button.
What I'm trying to achieve is when some text is entered it's saved to the $_POST array and outputted below. However, what I then want to do is use the same form to then perform the same task (albeit different text), but ensure both/multiple values are saved.
I'm assuming the best way to achieve this would be to save them to an array as the page is reloaded; but i'm not sure where to start.
Thank you.
What you could do is make some hidden fields and init them with the data from the form,
wich has been edited the first time.
Then when it is posted for the second time you could use the values from the hidden fields and the new information from the normal fields
<input type="hidden" name="hiddenFieldName" value="<?php $_POST['normalFieldName'] ?>"

PHP: form action on same page, still show same until refresh

Yes, Im having a little edit profile page, index.php?mode=profile. Lets take the username in the editprofile form as example. The username is already in the username-field. So i changed from "Peter" to "Tom" and press save.
The action is ?mode=profile&edit=true. So now when i have pressed save it has updated the column in the db from Peter to Tom. But this field keeps having the value "Peter" until if i do press refresh (or f5), then "Tom" will appear. Like it hasnt updated in the database anything, although it did but it still shows Peter until next refresh.. like it caches, but it shouldnt cache nothing?
Any help on this? Is it because its on the same "page" / file? what can i do
I think you fetch the data first, put it in the form and then update the database contents. You should update the database contents first when you submit the form. The fetch and creation of the form should be the next step.
Most likely you're rendering the page before doing any of the profile modifications. Make sure you're dealing with changes to the profile before you render it (i.e., all your database calls to update values should be before the SELECT statements to render the page).
HTTP provides two methods : POST and GET. With URL starting with ?param= you are actually using GET. "GET" should be used if and only if the form processing is idempotent query (ie the page content is NOT altered by the query).
If your form modifies data, you should use POST method.
What you can do is in each input field of your form just echo post values for respective fields.
For eg -
lets take an example of username field -
<input type="text" name="username" value='<?php echo $_POST['username']?>' />
By this way if you post any field then that value will be displayed and not from cache.
In my above example i haven't cleaned the post data, make sure that you use cleaned post data
in actual scenario.

Categories