2013-12-19 60 views
2

我目前正在掙扎,因爲我想加載PHP提供的同一頁面。爲了使這個更清楚,這是一個評論部分,在人們提交他們的信息後,我想檢索相同的信息並加上新的信息。jQuery在PHP提交後加載相同的頁面

我有問題,因爲我沒有通過AJAX來做這件事。

因此,index.php文件有以下幾點:

/* Before HTML tag */ 
if(Cookie::Exists('comment')){ 
    $data = unserialize(stripslashes(Cookie::Get('comment'))); 
    if($data['status'] == 1){ 
     $page = "<script type='text/javascript'>set_product_id('" . $data['id'] . "')</script>"; 
    } 
    Cookie::Delete('comment', time() - 3600); 
} 

/* Further ahead.. (inside body) */ 
<form method="POST" action="insert_info.php" data-ajax="false"> 
    <input type="hidden" id="product_id" name="product_id"/> 
    <input type="text" id="name_" name="name_" required="required"/> 
    <input type="text" id="comment" name="comment" required="required"/> 
</form> 

/* After body */ 
<script type="text/javascript"> 

$(document).on("pagebeforeshow", "#page-comments", function() { 
    $(function(){ 
     get_comments(objs.product_id); 
    }); 
}); 

var objs = { 
    product_id : '' 
} 

function set_product_id(id){ 
    objs.product_id = id; 
    $.mobile.changePage('#page-comments', {role: 'dialog'}); 
} 
</script> 

/* After the HTML tag */ 
<?php echo $page; ?> 

我收到正確的ID等,還它submiting評論之前完美的作品。頁面加載沒有任何問題的內容,等等。

insert_info.php

new_comment($host, $user, $pass, $db); 

$arr = array("status" => 1, "id" => $_POST['product_id']); 
Cookie::set('comment', serialize($arr), time()+60); 

header("Location: ../index.php"); 
exit(); 

當我執行此操作,我不接受谷歌瀏覽器的控制檯上的以下埃羅:

Uncaught TypeError: Cannot call method 'trigger' of undefined 

Poiting到線:

function set_product_id(id){ 
    objs.product_id = id; 
    $.mobile.changePage('#page-comments', {role: 'dialog'}); 
} 

我錯過了什麼?謝謝。

編輯:改變set_product_id()函數,這樣:

$('#page-main').on('pagecreate', function(event) { 
    $.mobile.changePage('#page-comments', {role: 'dialog'}); 
}); 

出現的評論頁面,再次消失。但是,如果我刪除「對話框」,頁面將保持正常工作。但我需要這個對話。

回答

2

$.mobile.changePage()需要包含一個網址..

所以這個:

$.mobile.changePage('#page-comments', {role: 'dialog'}); 

應該是這樣的:

$.mobile.changePage('../index.php', {role: 'dialog'}); 
+0

我想你錯過了什麼。我想開該頁面的評論是#頁面註釋不是index.php再次..因爲我已經做了PHP側,重定向時。編輯:順便說一句,看到我編輯的職位。 – user3065191

+0

我只是在裏面放了'index.php'來表明這個需要是一個''url'',所以在這種情況下,#page-comments的URL是什麼?它是'index.php#page-comments'嗎?或'someurl.html#page-comments'。你明白我的意思了嗎?它需要是一個'url'。 – MElliott

+0

啊,好的。不起作用。我得到了同樣的錯誤。如果我把我的編輯帖子放到你的解決方案中,頁面就會出現並消失,而且我沒有錯誤。 – user3065191

相關問題