How to fill a form with data from Mysql database? - php

I have two HTML forms in a php file(Control.php). I have four dropdown list as filters in Form1. once i click submit button in Form1, i have to pass the values selected from Form1 to another php file(Actions.php) through POST method, where i pass query with the filters and retrieve data from Mysql database(if record is present in the database for those filters). I was able to make it work so far.
Now i want to fill my Form2, with the data retrieved from database based on the filters. if no data is available, i want to set all the elements in the form blank for adding new data.
How to add a new data and modify(update) existing data in same Form.

You can pass the data retrieved from the first query to the other pages in the PHP session. Then your script can simply copy the contents of the session (or NULL) into the value= attributes of the form.

Related

PHP Loading MySQL Columns For a UniqieID Then Updating Columns

Sorry very new to PHP and MySQL. I'm pretty close to having what I need. I created a database that hold values as shown here:
I was able to find some things online to create a form and initially get the data into the database
What I would like to do is have another page to load values for a specific customer by searching for their uniqueid. So have a input box where you input the uniqueid then the current values for that customer get loaded into a form just like the "Customer Creation Form". Then be able to update the loaded values in the the form and save it back. The enddate is probably the field that would be updated the most.
You could use Ajax requests and PHP to update the form live (or update once the user has entered the UniqueId)
https://www.w3schools.com/php/php_ajax_php.asp
To return multiple values grabbed from the database, use php to create an array return it as a json file.
https://www.w3schools.com/js/js_json_intro.asp
https://www.w3schools.com/js/js_json_arrays.asp
https://www.w3schools.com/js/js_json_php.asp

how to show a value from a database in an input field in CakePHP

I liked to know how I can show a value from my database into an input field. I'm working with CakePHP.
Right now, I have some empty fields, but I want them filled with the database values when I display my page the first time and of course when there are values in the database relative to these fields. I'm able to retrieve all my database data, but unable to show them into the fields.
Is there an easy way with CakePHP to do it or I need to set each field individually in my controller and retrieve it in my view.
Thank you
You are able to fill forms from the controller, by using $this->form->data.
Example:
$this->request->data = $this->YourModel->findById($id);
Edit:
You have to create your form with the form helpers to attach the form to your model, otherwise it won't work.
echo $this->Form->create('YourModel');

POST Data Without Form

Basically I have a bunch of data I get from a database and put onto my page in a table. Right now I have the user type in the name, session, etc. in the table and that is sent as post data into the next PHP page, which I then use to lookup more stuff in the DB and so on and so forth.
Obviously that's not a great user experience; it would be much easier to simply CLICK the item in the table and everything gets sent automatically into the next page.
I'm not sure how I'd go about doing this.
My tables are first and last names for now, so if you click a certain row it should go to the next page sending each cell as data.
EDIT: Some examples:
Traditionally you do this with a form
<form method="post" action="pageDataIsGoingTo.php">
to send data to the next page. However, I don't want to do this with a form; but rather when they click a URL and/or button that sends the data. I can "hide" the data from view I suppose, but I still don't know the function to actually go ahead and do that.
Would I make a javascript button/function that sets something in an invisible form?
You can use invisible/hidden form fields.
That might be your best guess.
Javascript would be a good solution if you wanted an ajax POST call, but you want to load other page.
So hidden form fields are your solution.
Parallel with table data.
You need to embed hidden fields and your visible item row within a form
(so each item row contains also a form & hidden form fields and visible submit button,
which you can style with css)
This presuming that your table contains more items which you can choose to send.
Although I would do this with backbone & jquery and do it all in ajax.

how the validation is undertaken when inserting data to two tables using one form?

There is a web page containing set of input fields. The data taken from the user go to 2 tables in the database. I use the view php file to get data from the user and call a function to gather data using jquery and then push those data to database using ajax function. How can I implement the validations?

Post Array Data from Multiple Selects to Mysql

I have a form-1 which has 4 fields. when the user inputs data in these and hits submit, he is taken to form 2 for further selection of more items from multiple selection box. after selections are complete, he is prompted to update and on updating, all the data has to go to a third form for processing.
currently i am passing the single fields data from second form to third form by <input type="hidden" name="abc" value="<?php echo $x[0] ?>">
I am getting stuck as to how to retreive all multiple selected items from the array, perform a calculation on them and then post to mysql and then update the user with posted information.
or is there a better way of doing this, pl. guide me. my fields are:-
first page
customer id - single selection field
date - input
segment selection - single selection field
second page
items inputs - itemid, quantity,price (these are in one row and user will dynamically add or delete rows based on requirements. i have done this through Javascript)
now after all this, i want to gather all details of customer, segment, items(id,quantities,prices) and then post them to mysql.
If you want to use separate pages you can use either hidden inputs fields or sessions to pass along their selections. With sessions, you'd just store the array of data in $_SESSION and use session_start() on each page to get the session from the previous page. With hidden inputs, you can store them just like you would with session, and when they click POST you will rewrite them into the form. Are you stuck on specific aspect of doing this?
On the final page use either the session or the hidden fields (depending on your chosen method) + the final POST, to query MySQL.
Note: As Zirak mentioned in the comments, you could also do this using a single page. You'd use one of the same methods described above, except it would post to itself rather than to another page. This might be a faster/better way to code the page... If you opt for the single page method just ensure that you make it possible to go back, both through their browsers back button and a link you provide.

Categories