Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
控制器和javascript函数不能一起工作_Javascript_Php_Forms_Codeigniter - Fatal编程技术网

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

控制器和javascript函数不能一起工作,javascript,php,forms,codeigniter,Javascript,Php,Forms,Codeigniter,我有一个表单,onsubmit调用控制器函数,从而将数据保存到数据库中。在同一个按钮上,我需要调用一个Javascript函数。该函数单独工作,但与控制器一起工作时,它不起作用 <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates

我有一个表单,
onsubmit
调用控制器函数,从而将数据保存到数据库中。在同一个按钮上,我需要调用一个Javascript函数。该函数单独工作,但与控制器一起工作时,它不起作用

<!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>

内容
添加帐户
&时代;
添加帐户
新帐户:
帐户名:
帐户类型:
脸谱网
啁啾
谷歌+
LinkedIn
一款图片分享应用
品特烈
取消
添加
共点火器

定义表单和

您能在表单中添加(onsubmit=return false)吗


在提交表单之前,您可以在查看页面本身中运行javascript函数,我不认为您的代码不好,但您的方法是正确的

如果您想在提交表单后使用javascript,请使用AJAX,没有太多其他选项,可能是iframe中的表单,我在答案末尾提到的表单或一些神秘的技巧,但您有正确的工具:js framework jQuery,使用一个用健壮的服务器端语言(PHP)构建的MVC框架,使用AJAX

在stackOverflow中,应该有很多关于使用AJAX提交表单的问题/答案>(使用codeigniter的问题/答案更少)(我将在白天用链接更新这个答案)

既然已经在加载jQuery,那么就使用jQuery进行AJAX调用


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

提供您的代码示例。当您单击按钮时会发生什么情况您的控制器文件夹结构是什么?你的控制器叫什么名字?@Sherin:点击按钮会生成一个数据库条目。这是一个AJAX调用吗。。??不,我想..那么当你的页面被重定向到操作页面时javascript是如何工作的…但是我听说form_open()和form tag都不应该被使用。不要使用form_open,使用普通的form action,id和这个onsubmit,,,它会工作id你给onsubmit=“return false”耶,在我给onclick=addinfo()的按钮上在我写的表单标记onsubmit=“return false”中,您现在可以给出完整的代码了吗?在addinfo()函数中,您首先描述一个javascript函数您想要处理什么