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!