【问题标题】:Controller and javascript function not working together控制器和javascript函数不能一起工作
【发布时间】:2014-02-14 05:55:11
【问题描述】:

我有一个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>

【问题讨论】:

  • 提供您的代码示例。
  • 点击按钮时发生了什么
  • 您的控制器文件夹结构是什么?你的控制器叫什么名字?
  • @Sherin:点击按钮会创建一个数据库条目。
  • 它是一个 AJAX 调用..??不,我认为..那么当您的页面被重定向到操作页面时,javascript 如何工作......

标签: javascript php forms codeigniter


【解决方案1】:

代码点火器

定义表单和

您可以在表单中添加 (onsubmit=return false )

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

【讨论】:

  • 但是听说form_open()和form标签都不应该用。
  • 不要使用form_open,使用正常的表单操作,id和这个onsubmit,,,它会工作
  • 你有没有给 onsubmit="return false"
  • 是的,在我给 onclick=addinfo() 的按钮上,我在表单标签中写了 onsubmit="return false"..
  • 你现在能不能给出你的完整代码,在 addinfo() 函数中,你首先描述一个你想要处理的 javascript 函数
【解决方案2】:

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

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

应该像几十个问题/答案>关于在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,我没有尝试过,但即使它有效 不要不要使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多