show data from database into form? - php

I have a html form where I take all the user data and then insert/edit in the database. Then I also have a search button which shows all the available entries in the database. Problem is, when I click on a particular entry,I want all it's data in the form..in an editable format..how can I do it??
I tried storing all data in session variables and then redirecting it to the main page containing the form then assigning session variables to respective form field,but it's not working (as it says undefined when you first load the page).
https://i.stack.imgur.com/usyUf.png this image shows the error when I first load the form...all help appreciated!
PS- I did use session_start() at the beginning!

All you have to do is put the database values in "value" attribute of input form.
e.g.
<input type="text" name="User" value="<?php echo $User;?">
Here, $User is a variable which stores the User Name retrieved from Database.

Related

How can I send an Object/Array through form and hide the PHP form input from the user?

I'm trying to send an object or a array from one page to another through form. Json coding could help, but I need to send the info when the user press the form submit button. I've searched a little and tried to use serializing/coding through:
<input type="hidden" name="geracoesc" value='. base64_encode(gzdeflate(serialize($generations))).'>,
but the code is still shown at the HTML Code (because of the input), so, how can I hide it from an user at the moment the form is sent?
Couldn't you use the $_SESSION super-global?
On your first page, start a PHP SESSION with:
session_start();
Then assign the object you want to pass to the $_SESSION like so:
$_SESSION['somedata'] = $object_I_want_to_hide;
On any following pages, you can access the data like so:
session_start();
echo $_SESSION['somedata'];
you can create a table and save that data with session_id of that user and use from that everywhere which you need with unique session id which each visitor has.

php code logic for transferring data from one page to another

i made a (php) form, and used php session and post to transfer the form data to another page, which gives the user an option to have a look at the data and if wrong he shall move to the main form..
now here is what i did
on page1 i made a main form say form1, when user clicks on submit, i saved the data to DB and added that id to a session, and sent that session to next page using the same id
on next page i called from db matching the same id and displayed data accordingly..
now i displayed the data (on the second page) inside a div styled with css, the logic for this second page is giving the user the choice if the data is wrong he can edit, and bottom of this div i added a button with code
Edit
now my question is this, what shall be next? my target is, if user clicks on this edit button he shall be taken to main form (i.e. page1, form1) and there he shall has the option of editing the data in the same id. How can i achieve this friends?
Also i used
<?php session_start(); ?>
on top of both the pages...
Just change your code on the form1 page to; load the data from the database if the id of the record is set in your session variable.
Then if the user decides the data is wrong he just gets sent back to form1 and has the possibility to make changes.
Use something similar to: value="<?php echo $data['name']" in your input fields.
Then check for the id and load the data:
if(isset($_SESSION['record_id'])) {
// Do a query with `where id = record_id
// Fetch the data in the $data variable
}
IF you dont want use GET then use SESSION.
in file.php set
$_SESSION['your_id'] = $theid ;
and in your edit.php retrieve it like that
$id = $_SESSION['your_id'] ;
of course you must session_start in both files.
If you already have all the data in your session you can simply link back to the first page. All you have to do is set all of your fields on the first page to pre-fill with the session data as long as it isset. This way, when you go back the forms will auto fill with the data the user has already entered and they can simply change what is needed.

A form with 3 submits; how to keep data for final submit?

I have created a PHP form which requires the user to select a postcode from a list of postcode values.
The user presses submit two times:
- once to go to address select menu which will display a select drop-down with values
- second presses "ok" button to select the address corresponding to his postcode value
I need to keep the value of the selected postcode value for when the form gets submitted. I have tried setting up the postcode drop-down value chosen in a SESSION... but it gets lots when user presses form submit.
How can I keep all the form values even after refreshing the page when the user presses one of the submits?
"How can I keep all the form values even after refreshing the page when the user presses one of the submits?"
Reading your question I didn't understand if each of the form submits actually gets submitted to the server, but I'm going to assume so. I'm also assuming you're trying to use PHP sessions to accomplish this.
When the user submits the form, save the values server-side in a PHP session
//Start the session
session_start();
//Save the values
$_SESSION["foo"] = $_POST["bar"];
...
If, after choosing the address, the user gets redirected to the initial form and you want to populate that:
//Start the session
<?php
session_start();
?>
<!-- Populate HTML form based on previously submitted values -->
<input type="text" name="foo" value="<?php echo $_SESSION["foo"] ?>" />
...
After the final submit you should have all the submitted values saved in the $_SESSION array. Don't forget to always session_start() before trying to handle anything session related.
The short answer is I don't think you can track form data across multiple forms.
I haven't seen your project and so might not fully understand the requirements, but I would suggest you consider using AJAX. Check out the jQuery post() manual, it's really simple actually. This has the advantage of allowing you to update the page once your first form has been completed.
EDIT: Sorry I meant you can't access multiple form data in a single $_POST. Of course you could store it in $_SESSION (remember to start your session properly).

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.

Php page refresh , get the post values from formm

i got a jquery upload and crop script and i am trying to use it.
First i have a 1.html file which has a form, which requires some texts and image. After submitting the form it goes to main.php where it checks for some image properties and if successful it refreshes the page using header("location:".$_SERVER["PHP_SELF"]);
So if i place my $_POST['name'] i get the value from 1.html . Now when the image in displayed after page refresh there is one more option to select the thumbnail and upon selecting the thumbnail there is one more page refresh, to display the final images (both bigger and thumb). Now my problem is for second page refresh i am not able to get the fields which i had posted from 1.html. Any suggestion would be highly appreciated. Thanks
With that header refresh you are losing every information. Drop the refresh and do consecutive forms: you need to propagate the values you need from the first form, using hidden input fields in the forms that follow
<input type="hidden" value="<?php echo $value_from_original_post; ?>">
or you can store the value(s) of interests in session variable(s).
Alternatively, you can use an AJAX solution which requires no reload or page change, but it's a bit more work (and you might not want javascript).
You can not store states between pages unless:
Keep rolling the value(s) forward as hidden input type if it's a form submission.
Temporarily save value(s) as cookie until consumed.

Categories