[英]Form submission in cakephp using JQuery
我已經創建了像這樣的表格
<?php echo $form->create('Result',array('action'=>'submit'));?>
//some input text fields,texarea fields
<?php echo $form->end('submit');?>
在我的JQuery代碼中,我這樣寫,
<script>
$(document).ready(function(){
var str,fields;
function showValues() {
str = $("form").serialize();
$("#results").text(str);
}
$(".submit").click(function (){
alert(str);
$.ajax({
type: "POST",
url:"/results/submit"
data: "str="+str,
success: function(msg){
alert( "Data Saved: " + msg);
}
});
return false;
});//submit click
});//document ready
</script>
編輯:將網址添加到Ajax,
Now when i click the submit button it alerts str correctly..
Like my alert value is
_method=POST&name1=value1&name2=value2
But in the next alert of Data saved it shows only the _method=POST
在我的結果控制器中
<?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;
// echo $this->params['form']['str'];
}
}
and my Results Table is having (id,form_id,attribute_id,label(eg .name1,name2),value(eg...value1,value2) )
請給我建議。有什么建議嗎?
幾個問題:
$(".submit").click(function (){ // do something .. in your case Ajax request return false });
因此,現在提交表單時,將發送Ajax請求,但無論如何您都將重定向到/ submit頁面,我認為這已填充了GET數組而不是POST。
此外,Ajax請求中缺少該URL
嘗試使用Jquery Form插件,它使工作變得更加輕松http://malsup.com/jquery/form/
Ajax請求中缺少URL。 應該:
$(".submit").click(function (){
alert(str);
$.ajax({
url : "submiting.php"
type: "POST",
data: "str="+str,
success: function(msg){
alert( "Data Saved: " + msg);
}
});
});//submit click
更新后:
我不確定,但是您不應該在您的Submit.php文件中創建控件實例並顯式調用Submit函數嗎?
嘗試在click函數中使用添加e,然后在第一行添加e.preventDefault(); 以防止發生默認操作。
如果要使用ajax提交表單。 Cakephp為此具有烤箱機制。 檢查下面的代碼
<?php
$data = $this->Js->get('#yourFormId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#yourFormId')->event(
'submit',
$this->Js->request(
array(
'data' => $data,
'async' => true,
'dataExpression'=>true,
'method' => 'POST'
)
)
);
echo $this->Form->create("myForm", array('default' => false));
echo $this->Form->input('name');
echo $this->Form->end();
echo $this->Js->writeBuffer();
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.