Jquery form submit not hitting proper action
I am using a controller with two actions. One is just a holder to display a view and t开发者_JAVA技巧he other is an action to do en event. So my controller likes like this:
public function showAViewAction(){
//empty
}
public function addUserAction(){
//Code to create a user
}
Here is the form code:
<p>
<?php if(!empty($this->errorMessage)) : ?>
<img src="/images/view/exclamation.png" alt="error" style="vertical-align:middle;"/>
<span style="color:#f00;"><?php echo $this->errorMessage; ?></span>
<?php endif; ?>
</p>
<br />
<form id="addUserForm" method="post" enctype="multipart/form-data">
<p>This section is a disclaimer for requesting obscure data......</p>
<br />
<p><strong>username:</strong></p>
<input type ="text" name ="username" id="username"/>
<input type ="text" name ="password" id="password" />
<input type="submit" name="addUserBtn" id="addUserBtn" value="Add User" />
</form>
Now in the displayed view i have created a form. I am using jquery to send the form request to the addUser action in my controller. It looks like this:
<script type="text/javascript">
$(document).ready(function() {
newRequestValidator = $('#addUserForm').validate({
rules: {
username: 'required',
password: 'required'
},
submitHandler:
function(form) {
var options = {
url: 'add-user',
dataType: 'json',
success: showUser
};
$(form).ajaxSubmit(options);
}
});
});
function showUser(data) {
window.location = '/user/';
}
</script>
The problem is that when i click the submit button on my form it hits the showView action instead of the addUser action. What am i doing wrong?
Your actions are not correctly named?
public function showAView(){
//empty
}
public function addUser(){
//Code to create a user
}
Should be
public function showAViewAction(){
//empty
}
public function addUserAction(){
//Code to create a user
}
EDIT
In that case your accessing them incorrectly, you need to use add-user
not addUser
Try changing the "url" option from "add-user" to "/user/add-user". If you inspect the request in firebug or chrome's built in inspector then you should see the request being malformed.
Are are you adding the validation plug-in to your source code?
<script src="jquery.validate.min.js" type="text/javascript" ></script>
You can download this from here http://plugins.jquery.com/project/validate
精彩评论