[英]Calling an Action Method from Javascript in PHP MVC
I am working on php phalcon with XAMPP server. 我正在XAMPP服务器上使用PHP Phalcon。
I have a controller called PrescriptionsController
. 我有一个名为
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. 在
index
视图中,我填写一个<form>
以进行处方,然后当我按下save
按钮时,将调用javascript函数,该函数将调用控制器中的另一个操作方法。
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.
这就是为什么当我单击保存时必须调用javascript函数以便检查文本框的数量及其包含的值的原因。
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?
好的,我可以从文本框中读取值并将它们插入到数组中,但是实际上我有两个数组,我想知道如何将它们作为参数传递给我在javascript函数中调用的操作方法? 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. 我的
$_POST
返回空。 How can I solve this too? 我也该如何解决?
So my two questions are: How to pass parameters to action methods called in javascript? 所以我的两个问题是:如何将参数传递给javascript中调用的操作方法? How to get $_POST values?
如何获得$ _POST值?
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. 我认为您应该考虑采用另一种方法,例如,可以在每个输入名称后加上ID,为输入数量设置一个计数器。 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
. 然后,在保存时,只需执行一个for循环即可使用
document.getElementsByName("InputNameId")[0].value
获取输入值。
You can also just add ids to the inputs and get it by document.getElementById('InputNameId').value
! 您还可以将id添加到输入中,并通过
document.getElementById('InputNameId').value
获取它!
All the syntax is assuming you are using pure javascript. 所有语法都假设您使用的是纯JavaScript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.