我正在使用JQuery的Form插件.. 我添加了Form插件的js文件。
我的代码中已经有一个表单..beforeSubmit正确提醒实际的表单内容,我想把这个值放到我的服务器端..我尝试了以下,但没有工作...
我的代码是
<?php echo $form->create('Result',array('action'=>'submit'));?>
//some input text fields,texarea fields
<?php echo $form->end('submit');?>
<script>
$(document).ready(function(){
var options = {
beforeSubmit: showRequest, // pre-submit callback
success: showResponse, // post-submit callback
url: "http://localhost/cake_1.2.1.8004/index.php/results/submit1",
type: 'POST',
resetForm: true // reset the form after successful submit
};
$('#ResultSubmit1Form').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
});//ready
// pre-submit callback
function showRequest(formData, jqForm, options) {
var queryString = $.param(formData);
alert('About to submit: \n\n' + queryString);
$.ajax({
type: "POST",
url: "http://localhost/cake_1.2.1.8004/index.php/results/submit1",
data: "str="+queryString,
success: function(msg){
alert( "Data Saved: " + msg);
}
});
return true;
}
// post-submit callback
function showResponse(responseText, statusText) {
alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}
在我的CakePHp控制器中
<?php
class ResultsController extends AppController
{
var $name = 'Results';
var $helpers=array('Html','Ajax','Javascript','Form');
var $components = array( 'RequestHandler','Email');
var $uses=array('Form','User','Attribute','Result');
function submit($id = null)
{
$str=$_POST['str'];
echo "POSTED value ".$str;
}
}
答案 0 :(得分:2)
尝试使用URL参数的相对URL:
url: "/cake_1.2.1.8004/index.php/results/submit",
在服务器端回显一些文本也是一个好主意,只是为了确保它不是服务器输出失败。
编辑:我已经看到你已经这样做了,抱歉没注意到它。