訪問URL時,Codeigniter提交表單

[英]Codeigniter submit form when accessing URL


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的內置功能處理表單和提交的數據的一個很好的示例。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/04/27/472779fa9fb3ed7af26e4c339b72e43c.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com