您好,我正在尝试将数据从 html.erb 文件(在我的 ruby on rails 网站中)发布到 php 脚本,我想知道这是否可能。到目前为止,当我从我的 html.erb 文件中尝试这段代码时,我得到了“缺少模板”
<form method="POST" action="uam_handle_form">
<input type="text" name="email" placeholder="Enter email address" id="email" class="email-input">
<input type="hidden" name="password">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="connect">
<button type="submit">CONNECT</button>
<label class="control control--checkbox">Opt in all marketing communications from X
<input type="checkbox" checked="checked"/>
<div class="control__indicator"></div>
</label>
</div>
</form>
其中“umm_handle_form”是一个 php 文件。我相信我在 rails 上正确设置了路由,因为如果我将文件扩展名更改为 html.erb,页面将呈现纯文本 php。
有什么方法可以从这个表单提交中发布、接收和运行 php 文件?谢谢!
最佳答案
这个问题类似于Run a php script in ruby on rails ,尽管该问题询问如何在您从 View 运行时从 Controller 运行 php。不过,另一个问题有一个公认的答案,即通过系统命令(反引号)从 Ruby 服务器运行 PHP 脚本。
因此,您应该采取的步骤如下:
通过 AJAX 将表单提交到运行 php 脚本的 Rails Controller 端点,如下所示:
@script_result_string = `php script`
render json: { result: @script_result_string }.to_json
然后在您的 AJAX 回调中,读取 response.result
以获取字符串形式的结果。
如果您尝试使用 PHP 运行浏览器代码,这可能不是一个可接受的解决方案。我认为这样做并不容易。换句话说,您必须将 PHP 代码限制为仅用于服务器的内容。
关于php - 从 html.erb 表单运行 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38469755/