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.
Related
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.
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).
I am creating a web interface in jsp. I have a java String variable (let's call it 'a') and some radio buttons, and I am trying to get the value of the checked one on click (without submiting the form) and give it's value to the variable.
I made a research on weather this can be done using only jsp and I could not find anything. So I assumed that I have to do this using Javascript. I am new in Javascript(so please excuse me if my question is stupid), but I wrote the following code.
HTML:
`<input type="radio" name="phase" value="value1" class="checkboxes" id="design_phase" onclick="getRadioValue(this.id)" />Value1
<input type="radio" name="phase" value="value2" class="checkboxes" id="development_phase" onclick="getRadioValue(this.id)" checked/>Value2`
Javascript:
`function getRadioValue(id) {
var radioBtn = document.getElementById(id);
if(radioBtn.value=="value1"){
alert(radioBtn.value);
<%a="value1";
System.out.println("value1!");%>
}
else{
alert(radioBtn.value);
<%a="value2";
System.out.println("value2!");%>
}
}`
When I run this, both System.out contents are printed just when I load the page, before I even choose a checkbox. I have put the alert functions in order to make sure that javascript reads the values correctly, and it does indeed! So the problem seems to be with using js together with jsp.
Does anyone know what I am doing wrong, and how can this be done correctly?
Thanks in advance!
You should use AJAX (if you dont want to submit the form).
This is the methodology:
As soon as the user selects a radio button, an AJAX call is triggered that sends a request to a servlet. The servlet recuperates the option selected from the radio buttons (from the request object) and using this value it generates a response containing the values you want to display in the select (perhaps the values are stored in an array) and then sends the response to the client. The response is received and the values to be displayed in the select are recuperated and dynamically added to the DOM.
Have a look at this link for an explanation of how to do it: How to use Servlets and Ajax?
also here is a very simple example: Return JSON from one JSP to another?
I have an ajax query that returns an array . . I only show the first 20 items in the array right now but i have now added a link for "Show All" after I displayed those first results.
When the user clicks Show All i want to display all of the items in the array.(either inline or in a dialog (but that implementation shouldn't matter)
I want to avoid having to go back to the server to get the full list since I already retrieved the full list from the first ajax call . . where is the best way to locally store this array to be accessed later.
Save the array in a variable, or possibly attached as a data element to something on the DOM. Then, when your "Show All" event fires, read from that array and rebuild your display.
<div id="myArrayDisplay">
</div>
<script>
$(function () {
$.getJSON("backendCode.php", function (jsonData) { // assumes your array arrives as a response from a JSON-based Ajax request to some server code
$("#myArrayDisplay").data("arrayObj", jsonData);
});
});
</script>
Obviously I'm not showing how you would display the contents of the array in the above code, just how I would store it.
I think the easy way you can do is to put the full list data into a hidden input textfield
I don't know about the best way, but the simplest is using a global variable.
Define it here:
$(document).ready(function(){
$.myArray = [];
})
And use it like so:
function myFunction(){
alert($.myArray[0]);
}
More info about $(document).ready.
On the other hand, you could create two separate fields, one with the initial info and one with the rest, making the second one invisible. When the user clicks "Show more" you can show the hidden one.
More information about .show().
I have a ViewModel of a form (Name, Address) etc and it's all bound to controls on my page (using Spark engine) - e.g.
!{ Html.DropDownList() }
That works fine. However, there is one DropDownList which is bound that has no values in it to begin with, the values are populated using Ajax (by selecting previous drop downs)
The problem lies when I submit the page and there's a validation error. The page loads and my select list has no values in it (as it hasn't been triggered to get them).
How can I set it up so that the ViewModel knows about values got dynamically so that it can populate the select list on page load?
You'll either need to:
Do this server side by using the Html.DropDownList() call that allows you to pass values in:
Html.DropDownList(string name, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes)
Or the other way would be to just trigger the same ajax call when the page loads so you don't have duplicated logic.
I'd actually recommend the second way, as it'll keep you from doing the same thing multiple ways, but it does mean that there'
You can add a property of selectlist type in your viewmodel, populate it in constructor, and call it through Html.DropDownList function in your view page. Further since this selectlist is dependent on another list(i.e another property of you viewmodel). in your constructor you can write code like,
say if first listbox is connected to list1 property of type int and allowed values are >0
<code>
if (list1!=0)
{
write code to populate second list based on your first list parameter and also set
the selected value as if selected by the user at submit its stored in the other
property linked to the second list box.
}
</code>
if you are still not clear, write here the concerned code of your viewmodel, and server side database call of your second list, I may help you by editing your code.