How to save data from dynamically generated textboxes? - javascript

I have a page to edit the 'utilization' rate of each employee, based on their designations. So the page has all the designations listed as , next to which a textbox, for the user to fill in the utilization rate. Scenario is that the user will not save each designation's utilization rate immediately after filling it. User will keep going till he fills the last item and then hit the submit button to save. Now, in php I can get all the values from the Request Array. But, how will I know which designations these values belong to? So, what i have as a solution is to name the textboxes with the designation_ids as suffix. May be like;
utilTextbox_1, utilTextbox_2, utilTextbox_3 etc...
Then when the form is submitted;
Check each Request Array element to see if its name starts with 'utilTextbox'
If Yes, split it using '_' to get the designation_id
Update the db table with the value of the text box
Check the next Array Element.....and so on...
is this the correct method or is there a better way of doing this?

You can have array as names in html forms. So something like utilTextbox[1], utilTextbox[2], utilTextbox[3] will work perfectly fine. In php you will get an array in $_REQUEST and you need not to convert in an array because it is already an array.

Related

Getting row data on Interactive Report

Morning,
I'm currently using an Interactive Report on APEX that contains several columns, some of them being checkboxes that represent if a certain number is present in the table that is being used to fill the report.
For example, row 1 has a telephone number "99091021", row 2 a provider, and the following 5 rows are checkboxes that should represent branches in certain areas. If a checkbox is clicked, it means that this number is present in that area.
Now, I'd like to create a dynamic action that inserts that number into that table when I click the checkbox (and it becomes checked), and a delete for when it's also clicked (and becomes unchecked). Problem is, I don't know how to access the rest of the data in the row of the IR to use as a comparison for the delete and insert statements.
Say I try to click the Checkbox 3 on the row where the telephone number is "99091021". A dynamic function would get the data row, then a true action would compare the necessary data to perform either an insert or delete, depending on the state of the checkbox. That's the plan.
I've done something similar before, using localStorage, but it didn't quite work, because before I used it on an interactive grid. Is there a similar function for interactive reports?
Also, is there a way to check if a checkbox is checked or unchecked in a PL/SQL Code?
Here's the solution I found:
I had a select that got the necessary data for the Interactive Report, but because I kept getting an error of temporary data table being exceeded and always breaking the server, I had to change the checkbox query to something like this:
apex_item.checkbox (1, '1_'||a.pk, case when max(decode(b.nr,1,1,null)) is null then '' else 'CHECKED' end) as checkbox1
The '1_' part represents me selecting the first checkbox. With the value still there as the selection, I was able to do the following:
$s("P165_GET_PK",this.triggeringElement.value)
The result would basically be "1_PK_number". With this function I was able to get the value (a.pk) hidden in the checkbox to an Apex item, and then continue using it for my insert and delete statements. It's just a matter of separating the value in it with substring functions and so on.
Now I can delete or insert new data inside that table with a simple click.
I hope it helps in case someone tries doing something similar.

Calling an Action Method from Javascript in PHP MVC

I am working on php phalcon with XAMPP server.
I have a controller called PrescriptionsController. It has several action methods. In the index view, I fill a <form> to make the prescription then when I hit a save button, a javascript function is called which calls another action method in the controller.
The thing is that the I can dynamically increase the number of textboxes depending upon the number of medicines I want to prescribe to the patient. This is why it is necessary that when I hit save, the javascript function is called in order to check the number of textboxes and the values that they contain.
Now, I wish to know that how can I achieve this? Okay I can read the values from the textboxes and insert them into an array, but I actually have two arrays and I want to know how to pass them as parameters to the action method which I am calling in the javascript function? One more thing, if I do something like this:
<script>
function generate()
{
//necessary code
open("actionmethod");
}
</script>
Along with this:
<form method="post">
<-- form body -->
<input type="submit" onclick="generate()">
</form>
My $_POST returns empty. How can I solve this too?
So my two questions are:
How to pass parameters to action methods called in javascript?
How to get $_POST values?
NOTE: I need two submit buttons and I am using two as well. One to preview and the other to save.
I think you should think of an alternative way to do it, you could for example, suffix every input name with its Id, set a counter for the number of inputs. Every time you add an input, you increase the count. Then when you save, you just do a for loop to get the inputs values using document.getElementsByName("InputNameId")[0].value.
You can also just add ids to the inputs and get it by document.getElementById('InputNameId').value !
All the syntax is assuming you are using pure javascript.

Order of checkboxes on secondary PHP page

I am using the following solution which almost fixes my issue:
Sequential Order of Checkboxes Selected PHP
The solution mentioned works perfectly if you view the results of the $_POST['col_list'] on the same page.
For me, when the 'submit' button is pressed I am taken to a separate PHP page which displays this and other variables I have input. When I do a print_r($_POST['col_list']) I see the correct variables that I selected but in their original order -- not the order in which they were selected.
My question is: how do I carry over the order of the checkboxes I've selected to a separate PHP page?
If you applyed the solution quoted, $_POST['col_list'] doesn't contain the checkbox selection order, it's $_POST['order'] which does. If you print_r([$_POST['order']), you'll see your selection order.
The solution you linked will not change the order of the values in the $_POST array.
If you write the input inside the <form> element as indicated in the solution, you will get another value ($_POST['order']) inside the $_POST array containing the order for the checkboxes (which should look like ",host,atom_name").
It is up to you to reorder the elements in your array, based on this value (I would suggest creating a new array, as changing the $_POST array is bad practice).

how to display the which was inserted recently in to table

I have a table in MySQL test_info . This table gets an insert command at random times such that a new record is added. This insert can be fired from anywhere.
Actually what have to do is?
front end should display the data which was inserted into the table recently in a real-time fashion.
please suggest me how solve this problem
You would use $mysqli->insert_id to retrieve the id that was auto generated and used by the last query. Check it out here.
Edit: Based on Thorsten Kettner's comment, you could also use SELECT LAST_INSERT_ID(); which to my understanding isn't based on the current connection and instead is referenced globally.
You could even use SELECT MAX(ID) to get the last ID within a table, which most likely would be the most recent if set to AUTO_INCREMENT.
First, for your client side, check out the Server-Sent-Events.
Secondly, you need a mechanism to check for latest updated data at your server side
You can use a mysql query to get last records. In my example shows how to get last 10 records.
here is the example
select * from table order by date DESC LIMIT 10
insert_id has some restrictions and I'm not sure how you will differ inserts from deletes.
What I would do will be *on insert trigger in each table where you need to track the insert
(may be you will want to track on update and on delete as well)
triggers
.
what you probably need is a separated table - historical_data_tab which holds all the historical data triggered by any event which you want to track.
for mysql :
USE `db_name`;
DELIMITER $$
CREATE DEFINER=`root`#`%` TRIGGER track_insert AFTER INSERT ON your_table
FOR EACH ROW BEGIN
insert into historical_data_tab
values (new.ID, 'INSERT',NOW());
END
pay attention to new.ID and NOW(). In case you don't need the original PK, just insert NULL in place of new.ID and any Auto Increment PK will take care of the rest. NOW will populate historical_data_tab table with a timestamps of the time the event was triggered. INSERT should tell you what type of event it was.
Replace it according to the trigger you'r using.
you might want to keep data about any of the following events*
after delete, insert, update
or
before delete, insert, update
p.s. There are a lot of ways to do what you need. Don't go crazy with triggers and try catching front end events in the front end itself
select * from your_table where ID = Max(ID)

JS - Advice on how to merge multiple user input to one form field design

I have a page that allows users to enter a lot of information about them (metadata) they can then click on a icon which opens a modal window containing a googlemap which allows them to add locations, and a title for that location.
Using mootools I can pass the value of a form field back to the original form, using onclose. The main page form then has a single hidden input field, which goes into the database as one field, serialised.
The problem is a user can add as many locations as they want, there are also 3 types of location. Each with its own set of co-ordinates, which can be single or multiple!
So I want to know the best way to handle all of this data, is it possible to load it into one form and then use Moo to submit that form to a single form field, or can I use moo to just append all the information into a single hidden input field, but if I do that, how does user input come into it. Im stumped and looking at some suggestions on how to set this up in the 'best' possible way.
Currently I have a table, and each item is added as a new row, by JS when a user clicks on the map, it creates a new row with the details about the click, item and then a user input field.
If its a single location then its added as 'placemark', a user input field for the name and then the co-ordinates go into a 3rd table cell.
However if its a shape, then the first cell contains 'shape', user input field for name/description, and the third cell contains a list of co-ordinates one for each point, this is the same for lines.
The problem I have is I could write it all to a single form field, but then how do I allow for user input of the titles, I need to use a form field for that? The other option is to take each row from a table and input it into the single form field, seperated by a pipe or similar, but then im not sure if I can read from other form fields.
I hope the above makes some sense!! All feedback welcome!
Im using mootools for this, but providing I can get my head around the layout then that should not really be an issue.
I would suggest that you use an object to maintain all of the location data while the user is making their selections. When, when the form is submitted, serialize that data into a hidden field as a JSON object.
So, when the user clicks in the map window, you record the info in the object, in addition to the table. The populated object would look like this:
var usersLocations = {"locations": [
{"type": "point", "coords": [100,200]},
{"type": "line", "coords": [[200,300],[400,500]]},
{"type": "shape", "coords": [[200,300],[400,500],[1000,1500]]}
]
};
Mootools may have some JSON methods, but if not, look at http://www.json.org/js.html for working code. You can use JSON.stringify() to convert the object into a plain string suitable for saving in the DB, and when you get that string back out, you can use JSON.parse() to turn it into an object again.
As for handling the user input, you could write the table to the page just as your do now (or base it on the above object). Then when the user enters something in one of the fields, copy the value into the usersLocations object. So have an onBlur event handler on the input field that updates the data structure (something like this) :
usersLocations.locations[0]["type"] = "<what the user typed>";
are you looking for something like this?
usersLocations.locations[0]["name"] = document.getElementById("location0name").value;

Categories

Resources