Ngassa Carine
2009-Sep-07 19:44 UTC
Calling a commandline function using the onclick button
Hello everyone, I''ll start by describing what I''m doing and saying that I am a newbie. In my ROR (2.1.2) application, would like to send read data from a text field using the onclick tag to a python script I wrote. The problem I encounter is with the onclick tag. I used the system() function to call the python script, but it doesn''t function. The python script is only called when I reload the page and not when I click on the send button. Who can help me or tell me how to better call the python script once I click on the send button. I''ll be grateful for your help. This is what my code looks like: <form id="frmmain" name="frmmain" method="post"> <input type="text" id="txt_message" name="txt_message" style="width: 400px;" /> <input type="button" name="btn_send" id="btn_send" value="Send" onclick="<% system(@path + ''myfile.py '' + @partner + '' txt_message.value &'') %>" /> </form> -- Posted via http://www.ruby-forum.com/.
Matt Harrison
2009-Sep-07 19:51 UTC
Re: Calling a commandline function using the onclick button
On Mon, Sep 07, 2009 at 09:44:31PM +0200, Ngassa Carine wrote:> > Hello everyone, > > I''ll start by describing what I''m doing and saying that I am a newbie. > In my ROR (2.1.2) application, would like to send read data from a text > field using the onclick tag to a python script I wrote. > The problem I encounter is with the onclick tag. I used the system() > function to call the python script, but it doesn''t function. The python > script is only called when I reload the page and not when I click on the > send button. > Who can help me or tell me how to better call the python script once I > click on the send button. > I''ll be grateful for your help. > > This is what my code looks like: > > <form id="frmmain" name="frmmain" method="post"> > <input type="text" id="txt_message" name="txt_message" style="width: > 400px;" /> > <input type="button" name="btn_send" id="btn_send" value="Send" > onclick="<% system(@path + ''myfile.py '' + @partner + '' txt_message.value > &'') %>" /> > </form>I have no idea if calling system() from within an ERb template should work or not, it seems like a pretty wild idea :D I would try making a controller action that the form submits to, and have the controller make the system call with the data. I haven''t tested that but I would give it a try if possible. HTH Matt
Frederick Cheung
2009-Sep-07 19:58 UTC
Re: Calling a commandline function using the onclick button
On Sep 7, 8:44 pm, Ngassa Carine <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello everyone, > > I''ll start by describing what I''m doing and saying that I am a newbie. > In my ROR (2.1.2) application, would like to send read data from a text > field using the onclick tag to a python script I wrote. > The problem I encounter is with the onclick tag. I used the system() > function to call the python script, but it doesn''t function. The python > script is only called when I reload the page and not when I click on the > send button. > Who can help me or tell me how to better call the python script once I > click on the send button. > I''ll be grateful for your help. > > This is what my code looks like: > > <form id="frmmain" name="frmmain" method="post"> > <input type="text" id="txt_message" name="txt_message" style="width: > 400px;" /> > <input type="button" name="btn_send" id="btn_send" value="Send" > onclick="<% system(@path + ''myfile.py '' + @partner + '' txt_message.value > &'') %>" /> > </form> > --I think you have a basic misunderstanding of how erb templates work. Anything in <% or <%= tags is evaluated when the template is rendered - that is what rendering an erb template means. Furthermore, an onclick options can only be a chunk of javascript. As Matt says, you need to write a controller action that your form is submitted to. Furthermore, be careful of the data you receive - if the example you posted worked it would allow a user to run arbitrary shell commands on your server. Fred> Posted viahttp://www.ruby-forum.com/.
Ngassa Carine
2009-Sep-07 20:19 UTC
Re: Calling a commandline function using the onclick button
> As Matt says, you need to write a controller action that your form is > submitted to.Ok, thanks for the quick reply. I''ll try writing a controller action. -- Posted via http://www.ruby-forum.com/.
Ngassa Carine
2009-Sep-08 11:59 UTC
Re: Calling a commandline function using the onclick button
> I''ll try writing a controller action.The idea with the controller action was really a good one. Now I can call the pythonscript without problem while clicking on a link, but it still cannot read from the form. How can I do this, or what should be written at the place of :msg => "$(''txt_message'').value". This is what my code now looks like: <form id="frmmain" name="frmmain" method="post"> <input type="text" id="txt_message" name="txt_message" style="width: 400px;" value="test" /> <%=link_to_remote ''send'', :url => { :action => ''sendmsg'', :controller => ''contacts'', :partner => @partner, :msg => "$(''txt_message'').value" } %> </form> Carine -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Sep-08 13:48 UTC
Re: Calling a commandline function using the onclick button
On 8 Sep 2009, at 12:59, Ngassa Carine wrote:> >> I''ll try writing a controller action. > > The idea with the controller action was really a good one. > Now I can call the pythonscript without problem while clicking on a > link, but it still cannot read from the form. > How can I do this, or what should be written at the place of :msg => > "$(''txt_message'').value".Read up about the :with option to link to remote (I wrote a bit about it a while ago: http://www.spacevatican.org/2008/5/17/with-or-without-you-link_to_remote-s-mysterious-parameter ) Fred> This is what my code now looks like: > > <form id="frmmain" name="frmmain" method="post"> > <input type="text" id="txt_message" name="txt_message" style="width: > 400px;" value="test" /> > <%=link_to_remote ''send'', :url => { :action => ''sendmsg'', :controller > => ''contacts'', :partner => @partner, :msg => "$ > (''txt_message'').value" } > %> > </form> > > Carine > -- > Posted via http://www.ruby-forum.com/. > > >
Ngassa Carine
2009-Sep-09 05:54 UTC
Re: Calling a commandline function using the onclick button
Thanks to you all, I got my problem solved using the form_tag and I could query the entry from the field using params[:myparameter] and use them in the controller action. Using :with also functioned. Carine -- Posted via http://www.ruby-forum.com/.