最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - "Form is not defined" error using jQuery - Stack Overflow

programmeradmin6浏览0评论

I am learning jquery and am writing some very simple jquery code that posts some variables to a method in my controller. I get “myform is not defined” error in firebug when using the code posted below.

Here is my html:

<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="assets/js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        function get(){
            $.post('jqtest/test',{name: myform.myname.value},
            function (output){
                $('#username').html(output).show();          
            }    
        );
        }
    </script>
</head>

<body>
    <div>TODO write content</div>
    <form name="myform">
        <input type="text" name="myname">
        <input type="button" value="Get" onclick="get();">
    </form>
    <div id="username"></div>
</body>

Here is my codeigniter controller code:

class Jqtest extends CI_Controller {

function _Jqtest() {
    parent::controller();
}

function index() {
    $this->load->view("jqtest/jqtest.html");
}

function test() {
    print_r($_POST);
    $this->load->view("jqtest/jqtest.html");
}

}

When the Get button is clicked, the test method should print the POST values. When I run the code in FireFox, it's not working (nothing is getting printed out). When I checked the firebug console, I found a "myform is not defined" error.

I know there is something wrong with my jQuery get() function, can anybody please help me out, thank you very much!

I am learning jquery and am writing some very simple jquery code that posts some variables to a method in my controller. I get “myform is not defined” error in firebug when using the code posted below.

Here is my html:

<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="assets/js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        function get(){
            $.post('jqtest/test',{name: myform.myname.value},
            function (output){
                $('#username').html(output).show();          
            }    
        );
        }
    </script>
</head>

<body>
    <div>TODO write content</div>
    <form name="myform">
        <input type="text" name="myname">
        <input type="button" value="Get" onclick="get();">
    </form>
    <div id="username"></div>
</body>

Here is my codeigniter controller code:

class Jqtest extends CI_Controller {

function _Jqtest() {
    parent::controller();
}

function index() {
    $this->load->view("jqtest/jqtest.html");
}

function test() {
    print_r($_POST);
    $this->load->view("jqtest/jqtest.html");
}

}

When the Get button is clicked, the test method should print the POST values. When I run the code in FireFox, it's not working (nothing is getting printed out). When I checked the firebug console, I found a "myform is not defined" error.

I know there is something wrong with my jQuery get() function, can anybody please help me out, thank you very much!

Share Improve this question edited Nov 30, 2011 at 10:20 thomaux 19.7k10 gold badges81 silver badges105 bronze badges asked Nov 30, 2011 at 5:20 GaryGary 4,72514 gold badges39 silver badges50 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

"myform is not defined" error occures because you have not set myform in javascript.

try Jquery's serializeArray()

var myform= $('form').serializeArray();

function get(){
            $.post('jqtest/test',{name: myform.myname.value},
            function (output){
                $('#username').html(output).show();          
                }   );
}
    function get(){
        $.post('jqtest/test',{name: $('form[name="myform"] input[name="myname"]').val()},
        function (output){
            $('#username').html(output).show();          
        }    
    );
    }

Replace

myform.myname.value

with

$('input[name="myname"]').val()

You can simply do this :

document.myform.myname.value

Form should have an id <form name="myform" id="myformid">

$.post('jqtest/test',{name: myform.myname.value},

would replaced with the below code :)

$.post('jqtest/test',{name: $( 'input[name="myname"]' ).val()},

If u need to send the all form value then u should use serialize. it's faster.

API DOC: serialize

If u use same same input name for different form. then u should have to write as below:

var $form = $( "#myformid" );
name = $form.find( 'input[name="myname"]' ).val();

and post will be:

$.post('jqtest/test',{name: name},
发布评论

评论列表(0)

  1. 暂无评论