访问URL时,Codeigniter提交表单

时间:2022-06-01 19:35:42

I have a form that works perfectly when I click the submit button. What I want is that if I add parameters in the URL, it submits the form automatically when accessing the URL.

当我单击提交按钮时,我有一个完美的表单。我想要的是,如果我在URL中添加参数,它会在访问URL时自动提交表单。

My form looks like this :

我的表单看起来像这样:

<form id="form_group_data" name="form_group_data"  method="post" class="form-horizontal" action="<?php echo site_url('admin/group/add')?>">
   <p>
   <div class="form-body">
      <div class="form-group">
         <label class="col-md-2 control-label" for="name">Name<span class="required"> * </span></label>   
         <div class="col-md-4">
            <input id="name" class="form-control" type="text" name="name" required="required" value="">
         </div>
      </div>
      <div class="form-group">
         <label class="col-md-2 control-label" for="firstname">Firstname<span class="required"> * </span></label>   
         <div class="col-md-4">
            <input id="firstname" class="form-control" type="text" name="firstname" required="required" value="">
         </div>
      </div>
      <input type="submit" value="Ajouter" class="btn blue"/>
      <button type="button" class="btn default" onclick="window.location='<?php echo site_url('admin/group')?>'">Annuler</button>
   </div>
   </p>
</form>

If I fill the fields manually and submit it, it works. Now I can enter this URL and it fills the fields via the parameters in the URL, but I'd like it to submit it automatically too, without clicking on the submit button.

如果我手动填写字段并提交,则可以。现在我可以输入此URL,它通过URL中的参数填充字段,但我希望它也可以自动提交,而无需单击提交按钮。

Example URL that fills fields automatically : https://test.ch/admin/user/insert/John/Lennon

自动填充字段的示例URL:https://test.ch/admin/user/insert/John/Lennon

Is it possible ? With JQuery maybe ?

可能吗 ?用JQuery可能吗?

2 个解决方案

#1


0  

  • check if the url parameters exist
  • 检查url参数是否存在

  • if yes, assuming the form is filled with the data from url submit it using jquery
  • 如果是,假设表单填充了url中的数据,请使用jquery提交

Put this code just before the closing body tag

将此代码放在结束正文标记之前

<?php 
if(isset(your_url_parameters)) // check if all parameters exist in url
{ ?>

<script type="text/javascript">
    $( document ).ready(function() {
        $("#form_group_data").submit(); //submit the form
    });
</script>

<?php } ?>

#2


0  

If you have the ability for fill out the form - you are setting this url - then you could also send this data to a different url that already handles the form submit action. So, rather than trying to round about submit this form after filling the data, point it to the action url admin/group/add/other/params/here in the first place with these parameters and in that controller, check for these parameters. Note however, that in this case you are side stepping csrf protection and opening a potential vulnerability as such. You'd be best off to post to this endpoint with the csrf token as well as the data to post, if you have that protection turned on.

如果您有能力填写表单 - 您正在设置此URL - 那么您也可以将此数据发送到已处理表单提交操作的其他URL。因此,在填充数据后,不是试图提交此表单,而是首先使用这些参数将其指向操作url admin / group / add / other / params / here,并在该控制器中检查这些参数。但是请注意,在这种情况下,您可以侧面执行csrf保护并打开潜在的漏洞。如果您已启用该保护,则最好使用csrf令牌以及要发布的数据发布到此端点。

Alternatively, you can use the controller for this form to check if the parameters are set in the url. You should be able to check for the parameters and if they are set (and pass your validation if you are going for that) do what you normally do on a form submit.

或者,您可以使用此表单的控制器来检查是否在URL中设置了参数。您应该能够检查参数以及它们是否已设置(并且如果您要进行验证,则通过验证)执行您通常在表单提交上执行的操作。

if (isset($this->uri->segment(4)) && isset($this->uri->segment(5)) {
    // Do the work here - add to db, etc. Alternatively call the function 
    // that is supposed to do the work and pass these parameters in some way.
    $this->load->model("my_user_model");
    $this->my_user_model->add_user($this->uri->segment(4), $this->uri->segment(5));
    redirect("success/url");
}

This is JUST an example. You need to sanitize and validate any form data you deal with, so in this case, either the my_user_model->add_user needs to do that, or ideally you'd do that before the model using CI's form validation library.

这只是一个例子。您需要清理并验证您处理的任何表单数据,因此在这种情况下,my_user_model-> add_user需要这样做,或者理想情况下,您在使用CI表单验证库的模型之前执行此操作。

I'd highly recommend taking a look at the form validation docs as a good example for how forms and submitted data can be handled using CI's built in functionality.

我强烈建议您查看表单验证文档,作为如何使用CI的内置功能处理表单和提交的数据的一个很好的示例。

#1


0  

  • check if the url parameters exist
  • 检查url参数是否存在

  • if yes, assuming the form is filled with the data from url submit it using jquery
  • 如果是,假设表单填充了url中的数据,请使用jquery提交

Put this code just before the closing body tag

将此代码放在结束正文标记之前

<?php 
if(isset(your_url_parameters)) // check if all parameters exist in url
{ ?>

<script type="text/javascript">
    $( document ).ready(function() {
        $("#form_group_data").submit(); //submit the form
    });
</script>

<?php } ?>

#2


0  

If you have the ability for fill out the form - you are setting this url - then you could also send this data to a different url that already handles the form submit action. So, rather than trying to round about submit this form after filling the data, point it to the action url admin/group/add/other/params/here in the first place with these parameters and in that controller, check for these parameters. Note however, that in this case you are side stepping csrf protection and opening a potential vulnerability as such. You'd be best off to post to this endpoint with the csrf token as well as the data to post, if you have that protection turned on.

如果您有能力填写表单 - 您正在设置此URL - 那么您也可以将此数据发送到已处理表单提交操作的其他URL。因此,在填充数据后,不是试图提交此表单,而是首先使用这些参数将其指向操作url admin / group / add / other / params / here,并在该控制器中检查这些参数。但是请注意,在这种情况下,您可以侧面执行csrf保护并打开潜在的漏洞。如果您已启用该保护,则最好使用csrf令牌以及要发布的数据发布到此端点。

Alternatively, you can use the controller for this form to check if the parameters are set in the url. You should be able to check for the parameters and if they are set (and pass your validation if you are going for that) do what you normally do on a form submit.

或者,您可以使用此表单的控制器来检查是否在URL中设置了参数。您应该能够检查参数以及它们是否已设置(并且如果您要进行验证,则通过验证)执行您通常在表单提交上执行的操作。

if (isset($this->uri->segment(4)) && isset($this->uri->segment(5)) {
    // Do the work here - add to db, etc. Alternatively call the function 
    // that is supposed to do the work and pass these parameters in some way.
    $this->load->model("my_user_model");
    $this->my_user_model->add_user($this->uri->segment(4), $this->uri->segment(5));
    redirect("success/url");
}

This is JUST an example. You need to sanitize and validate any form data you deal with, so in this case, either the my_user_model->add_user needs to do that, or ideally you'd do that before the model using CI's form validation library.

这只是一个例子。您需要清理并验证您处理的任何表单数据,因此在这种情况下,my_user_model-> add_user需要这样做,或者理想情况下,您在使用CI表单验证库的模型之前执行此操作。

I'd highly recommend taking a look at the form validation docs as a good example for how forms and submitted data can be handled using CI's built in functionality.

我强烈建议您查看表单验证文档,作为如何使用CI的内置功能处理表单和提交的数据的一个很好的示例。