控制器和javascript函数不能一起工作

时间:2014-02-14 05:55:11

标签: javascript php forms codeigniter

我有一个onsubmit调用Controller函数的表单,它导致将数据保存在数据库中。在同一个按钮上,我需要调用一个Javascript函数。该功能单独工作,但与控制器,它不是owrk。

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Content</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Bootstrap -->
        <link href="<?php echo base_url(); ?>stylesheets/bootstrap.min.css" rel="stylesheet" type="text/css">
        <link href="<?php echo base_url(); ?>stylesheets/bootstrap.css" rel="stylesheet" type="text/css">
        <link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.min.css" rel="stylesheet" type="text/css">
        <link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.css" rel="stylesheet" type="text/css">
        <script type="text/javascript">
            function addinfo()
            {
                //Create an input type dynamically.   
                element = document.createElement("button");
                element.className='btn btn-default';
                var t=document.createTextNode("Edit");
                element.appendChild(t);
                element.id=t;
                //var account=document.getElementById('newaccname').value;

                var foo = document.getElementById("acc");
//                //Append the element in page (in span).  
                foo.appendChild(element);
//                    var d = document.getElementById('acc');
//                    d.appendChild(i);
            }
        </script>
    </head>

    <body>
        <?php
        echo form_open(base_url() . 'index.php/accounts_controller/addAccount   ', array('name' => 'myform'));
        ?>
        <div id='boxed'>
            <button type="button" name="addaccount" id="addaccount" class="btn btn-default" data-toggle="modal" data-target="#myModalacc">Add Account</button>
        </div>
        <!-- Modal -->
        <div class="modal fade" id="myModalacc" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Add Account</h4>
                    </div>
                    <div class="modal-body">
                        <h5>New Account:</h5>
                        <input type="text" name="newacc" id="group" placeholder="Account"> 
                        <h5>Account Name:</h5>
                        <input type="text" name="newaccname" id="group" placeholder="Account Name"> 
                        <h5>Account Type:</h5>
                        <select id='acc' name='acc'>
                            <option value='Facebook'>Facebook</option>
                            <option value='Twitter'>Twitter</option>
                            <option value='Google+'>Google+</option>
                            <option value='LinkedIn'>LinkedIn</option>
                            <option value='Instagram'>Instagram</option>
                            <option value='Pinterest'>Pinterest</option>
                        </select>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        <!--                        <button type="submit" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>-->
                        <button type="submit" name="addaccount" class="btn btn-default" onclick="addinfo()"/>Add
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
        <div id="acc" style="width:500px;height:100px;border:1px solid #000;"></div>
        <?php
        echo form_close();
        ?>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://code.jquery.com/jquery.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="<?php echo base_url(); ?>javascripts/bootstrap.min.js"></script>
        <script src="<?php echo base_url(); ?>javascripts/bootstrap.js"></script>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

定义表单和

你可以在表格中添加(onsubmit = return false)

在您提交表单之前,您可以在视图页面中运行您的javascript函数

答案 1 :(得分:0)

我不认为你的代码不好,而是你的方法。

如果你想在提交表单后做一些javascript,请使用AJAX,其他选项并不多,可能是iframe中的表单,我在答案的最后提到的那个或者一些神秘的技巧,但你有正确的工具:js框架jQuery,使用一个使用强大的服务器端语言(PHP)构建的MVC框架,再次使用AJAX

应该像几十个问题/答案&lt;&lt;好吧也许没那么多&gt;&gt;关于在stackOverflow中使用AJAX(少了一些codeigniter)提交表单(我会在白天用链接更新这个答案)。

Learning how to use AJAX with CodeIgniter

CodeIgniter Ajax form - submitting form

I try to submit form by jQuery ajax() in Codeigniter ... but nothing happen ...:(

php - codeigniter ajax form validation

codeigniter and ajax contact form

既然您已经加载了jQuery,请使用jQuery进行AJAX调用。

是否有机会使用“document.referrer”来了解您的页面是否已被控制器加载,然后使用$(document).ready()执行js,我没有尝试过,但即使它有效不要使用它。