javascript - PHP 提交后 jQuery 加载同一页面

标签 javascript php jquery ajax

我目前正在努力,因为我想加载由 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 等,在提交评论之前它也运行得很好。页面加载内容没有任何问题等

插入_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();

当我执行此操作时,我在 google chrome 控制台上收到以下错误:

Uncaught TypeError: Cannot call method 'trigger' of undefined

指向线:

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'}); 
});

评论页面出现又消失。但是,如果我删除“对话框”,页面将保留并正常工作。但我需要将其显示在对话框中。

最佳答案

您的$.mobile.changePage()需要包含一个网址..

所以这个:

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

应该是这样的:

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

关于javascript - PHP 提交后 jQuery 加载同一页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688082/

相关文章:

javascript - For 循环 - 条件中断 - 内部异步函数

javascript - 在 Jquery 中创建的变量未出现在调试器中

javascript - html Canvas 中图像的主色转换

php - Wordpress 组合查询

php - array_filter 将索引数组转换为关联数组

jquery - 根据单击的 anchor 链接更改 URL

javascript - jQuery - 链接多个 .next() 方法的替代方法

javascript - 如何使用 javascript/axios 访问服务器的错误响应

php - 我可以 POST 和 GET 到同一个 PHP 页面吗

javascript - 如何在 slimScroll 中设置最小高度和最大高度?