在javascript中处理PHP变量并返回PHP

时间:2013-01-24 13:12:15

标签: php javascript jquery mysql

我正在开发一个应用程序,我从数据库中获取数据并使用javascript / jquery处理它:

    $sqlEdit = "select revisionContent from tbl_revision where revisionId='".$_SESSION['contentId']."'";      //Query to fetch the result
    $rsEdit = $dbObj->tep_db_query($sqlEdit);
    $resEdit = $dbObj->getRecord($rsEdit);
    $IdLessContent = $resEdit['revisionContent']; 

 <script language="javascript">  
    var getSavedContent = '<?php echo json_encode($IdLessContent); ?>';
    var trimmedCont=($.trim(getSavedContent).slice(1));
    //console.log(trimmedCont);
    var lengthCont= trimmedCont.length;
    var trimmedCont=$.trim(trimmedCont.slice(0,lengthCont-1));
    console.log(trimmedCont);
    var test = $('<div class="addId">');
    test.append(trimmedCont);
    //console.log(test.html());
    test.children().each(function(index, value) {
         $(this).attr('id', "com-"+randomString());
     });
   //console.log(test.html());  
    viewContent  = test.html();

我在 viewContent 中获得了所需数据。我想在此部分的页面上显示

        <div id="mainWrap" onClick="fnDestroyEditable();">
             <?php echo $resEdit['revisionContent']; ?>   //THis is the unprocessed data displayed directly from database.I want to display the processed data here
        </div> 

我知道我们无法获取PHP的javascript变量,因为两者都不同(一个服务器端和其他客户端)。但是,我怎样才能在我的场景中实现这一目标? 编辑我想补充说,返回的数据是 HTML 存储在数据库中。所以,我得到了html-&gt;处理它(添加id属性) - &gt; 希望在处理后返回

2 个答案:

答案 0 :(得分:2)

你可以使用javascript将viewContent放在#mainWrap中。 只需确保使用$(document).ready()包装你的js代码加载DOM 并添加:

$('#mainWrap').html(viewContent);

在你的功能结束时。

$(document).ready(function () {
    var getSavedContent = '<?php echo json_encode($IdLessContent); ?>';
    var trimmedCont=($.trim(getSavedContent).slice(1));
    //console.log(trimmedCont);
    var lengthCont= trimmedCont.length;
    var trimmedCont=$.trim(trimmedCont.slice(0,lengthCont-1));
    console.log(trimmedCont);
    var test = $('<div class="addId">');
    test.append(trimmedCont);
    //console.log(test.html());
    test.children().each(function(index, value) {
        $(this).attr('id', "com-"+randomString());
    });
    //console.log(test.html());  
    viewContent  = test.html();
    // put viewContent in the innerHtml of your wrapper
    $('#mainWrap').html(viewContent);

});

如果您需要将信息发送回服务器,则必须使用ajax。 我添加了一个javascript函数addId(),它将在单击其中一个元素时调用。 新代码是:

$(document).ready(function () {
    var getSavedContent = '<?php echo json_encode($IdLessContent); ?>';
    var trimmedCont=($.trim(getSavedContent).slice(1));
    //console.log(trimmedCont);
    var lengthCont= trimmedCont.length;
    var trimmedCont=$.trim(trimmedCont.slice(0,lengthCont-1));
    console.log(trimmedCont);
    var test = $('<div class="addId">');
    test.append(trimmedCont);
    //console.log(test.html());
    test.children().each(function(index, value) {
        $(this).attr('id', "com-"+randomString());
    });
    //console.log(test.html());  
    viewContent  = test.html();
    // put viewContent in the innerHtml of your wrapper
    $('#mainWrap').html(viewContent);
    $('#mainWrap .addId').children().click(function({
      addId(this);
    }));
}


addId = function(elem){
  // elem is the child element you clicked on
  // $(elem).attr('id') should be "com-[randomString]"
    $.ajax({
        type: "POST",
        url: "path/to/php/script", // update id PHP script
        data: data, // whatever you need in json format
        dataType: "json",
        error: function() {
        // error function you want to implement
            errorFunction();
        },
        success: function(resp) {
            // do whatever you need with the response from you PHP action
        }
    });
};

如果您需要在没有人工干预的情况下呼叫服务器,只需替换

$('#mainWrap .addId').children().click(function({
    addId(this);
}));

使用:

$('#mainWrap .addId').children().each(function({
    addId(this);
}));

答案 1 :(得分:1)

如果我理解你,你应该只在你的js代码的末尾加上这一行:

$('#mainWrap').html(viewContent);

如果要将JS数据发送到PHP,则应使用ajax请求。