私はCakephp 2.xで作業しています。私はAjax Postを使って私のコントローラにデータを送りますが、データはデータベースに保存されませんでしたか?cakephp 2.0を使ってデータがデータベースに保存されていません
フォームとAjax
$('body').on('submit','.contact_form', function() {
var action = $(this).attr('action');
var postdata = $(this).serialize();
$.ajax({
type: 'POST',
url: action,
data: postdata,
dataType:"html",
success: function(data, textStatus, xhr) {
},
error: function(xhr, textStatus, error) {
alert(textStatus);
}
});
return false;
});
});
<?php echo $form->create('Listingbookmark',array('action'=>'add/','class'=>'contact_form'));?>
<input type="hidden" name="listingid" value="<?php echo $listingData['Listing']['id'] ?>">
<input type="hidden" name="userid" value="<?php echo $user_id ?>">
<input type="submit" value="Bookmark">
<?php echo $form->end(); ?>
モデル
class Listingbookmark extends AppModel
{
var $name = 'Listingbookmark';
var $useTable = 'listingbookmark';
var $order = array('Listingbookmark.id asc');
}
コントローラ機能
function add()
{
$this->request->data['Listingbookmark']['listingid'] = $_POST['listingid'];
$this->request->data['Listingbookmark']['userid'] = $_POST['userid']; = $_POST['listingid'];
$this->Listingbookmark->create();
$this->Listingbookmark->save($this->request->data);
}
私は012にしてみてください両方の変数には値を示しているが、あなたは一つだけのテーブルにデータを挿入する必要がある場合は、テーブルにデータを挿入するためのCakePHPのSAVEALL方法を使用している理由は、データベースに
echo $this->request->data['Listingbookmark']['listingid'] = $_POST['listingid'];
echo $this->request->data['Listingbookmark']['userid'] = $_POST['userid'];
$this->Listingbookmark->create();
$this->Listingbookmark->save($this->request->data);
コントローラーにデータを送信していますか?あなたの 'add'アクションで' $ this-> data'の内容は何ですか? – bill
@bill 'data'はデータをデータベースに保存するためのデフォルト関数です?? –
@billだから、ajaxを使ってデータを格納するもう1つのソリューションは何ですか? –