Dynamically fill entry form - php

I have two tables on my page and they appear side-by-side. The first one contains an entry form and the second one contains a grid that displays rows of records.
When the user clicks on a row, the data related to that row must appear on the boxes of the form in the first table.
I know how to call a .php file through AJAX. But when I have queried the database and obtained the result-set, how to bring all those values to fill the entry form of the first table?

You can get your data in json format and set input fields values in callback function using it.
$.getJSON(
"your_url_to_request_data", {
your_params: and_it_values
},
function(data) {
// data <-- this is your data
$("#field1").val( data.something );
}
);

You need to send the result from the database to your JavaScript file via a callback function as silent implied. You also need to encode the data to JSON before sending it to JavaScript.
What you have to do in the callback function in your JavaScript file is to parse your result to JSON using native JSON parser. You access it by writing
JSON.parse(here_you_write_the_data_to_be_parsed);

Related

Insert values from multiple elements with PHP into a database

I need to save the data within these page elements into a database - they will be dynamically created by the user, but always in a similar format
The HTML appears:
<div><p>1</p>
<div><p>Broccoli</p></div>
<div><p>Carrot</p></div>
</div>
<div><p>2</p>
<div><p>Chicken</p></div>
<div><p>Beef</p></div>
<div><p>Lamb</p></div>
</div>
values needed from this:
"1:Broccoli", "1:Carrot", "2:Chicken", "2:Beef", "2:Lamb"
(happy to add classes/IDs to any divs to extract the required values)
What should happen:
On click of a save button, data is scraped into an array (via
Jquery?)
Array is sent to PHP file and inserted into the database
Any help on this would be great, thanks
you should save this data to the database as the user is creating it. not try to scrape your own page afterwards. This is going o save you a lot of time and make your script run faster.
Once you've done that, you can use serialize() to convert your array into a string that you can store in a database, and then turn the string back into an array with unserialize()

How to fill a form with data from Mysql database?

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.

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?

passing specific data from a table by jquery ajax

the application simply is
dynamic page generate html code with some data within table cells by receiving parameters
i could get these pure data by jquery
but i find trouble sending it by ajax to another page(the basic page)
to display it in another format
You could try to serialize the table into a json and pass it to the next page where you could recreate the table based on the json. Just a quick idea

What is the best method to populate form with jQuery?

I have used jQuery Form plugin to submit the whole form data to server while Ajax. When the server code (php) returns, the retrieved data is in JSON format. What is the best method to populate the form with jQuery.
Here is what I want to do:
The user enters the data on the form
The user submits the form data (I use ajax to submit)
The server code returns.
The user has the ability to retrieve the entered data.
So for the step 4, I want to know what the best method to use to populate a form?
Thank you
If you can ensure that the returned JSON object contains property names that match your form fields, it can be quite simple to populate the form.
Of course, you need to first evaluate the retrieved JSON, either using eval, or using a JSON library such as this for added security checks.
Then, you can just iterate over the object like this:
var obj = eval(json); /* or JSON.parse(json); */
for (var field in obj)
$('#' + field).val(obj[field]);
If your form contains non-textual fields (such as a select) you could add an additional check to set the selected index instead.

Categories