通过PHP传递变量 - > Javascript - > AJAX - 选项?

时间:2022-09-26 10:56:51

I am currently working on a CMS where there are a lot of communications between PHP and JavaScript (+ AJAX). I am having difficulties storing variables all through the flow.

我目前正在开发一个CMS,PHP和JavaScript(+ AJAX)之间有很多通信。我在整个流程中存储变量时遇到困难。

For instance: after login you can select a site to edit. When on click, I store the site_id in a POST-variable. After that I redirect to the pages-overview-page where I read out all my pages from the database by using this site_id.

例如:登录后,您可以选择要编辑的站点。单击时,我将site_id存储在POST变量中。之后,我重定向到pages-overview-page,在那里我使用这个site_id从数据库中读出我的所有页面。

If I add a page, the following happens:

如果我添加页面,会发生以下情况:

1.The $_POST['site_id'] is put into javascript by echoing:

1. $ _POST ['site_id']通过回显放入javascript:

echo "<script>var siteid =".$_POST['siteid'].";</script>"

2.My JavaScript then does an AJAX call:

2.我的JavaScript然后进行AJAX调用:

$(".leftpanelinner").on('click','._add-page', function(event){
    event.preventDefault();
    $(".sortable").load("ajax_scripts/page-actions.php/add_page",{siteID: siteid, callFunction:'add_page' });   
}); 

3.This on it's turn fires an php-script which adds a page to the site with a query to my database and then reloads the main div of my page:

3.这就是它发出一个php脚本,它向网站添加一个页面,查询我的数据库,然后重新加载我的页面的主div:

public function new_page($iSite_id, $sTemplate, $sTitle, $sMeta, $sCSS, $sJS, $sFavicon, $aContent, $aMenu)
{
    $oPagemodel = new page_model;
    $iPage_id=$oPagemodel->insert_pagemodel($iSite_id, $sTemplate, $sTitle, $sMeta, $sCSS, $sJS, $sFavicon, $aContent, $aMenu);
    return $iPage_id;
}

public function insert_pagemodel($iSite_id, $sTemplate, $sTitle, $sMeta, $sCSS, $sJS, $sFavicon, $aContent, $aMenu)
    {
        $this->set_pagemodel($sTemplate, $sTitle, $sMeta, $sCSS, $sJS, $sFavicon, $aContent, $aMenu);   
        $sSQL = "INSERT INTO `pages` (f_site_id, object) VALUES ('".$iSite_id.', '.serialize($this)."');";
        if (mysql_query($sSQL)) {$this->iID = mysql_insert_id();} else {return false;}
        return $this->iID;

    }

Isn't there a simpler way to store a variable through JavaScript/PHP/AJAX or is the above the best way?

是不是有一种更简单的方法来存储变量通过JavaScript / PHP / AJAX或以上是最好的方法?

1 个解决方案

#1


0  

You can use cookies for this.

你可以使用cookies。

<?php
    $foo = 'lorem';
    setcookie('the_foo',$foo,time() + (86400 * 7)); 

    echo $_COOKIE['the_foo']; 
?>

You can get and set cookies with js as well.

您也可以使用js获取和设置cookie。

#1


0  

You can use cookies for this.

你可以使用cookies。

<?php
    $foo = 'lorem';
    setcookie('the_foo',$foo,time() + (86400 * 7)); 

    echo $_COOKIE['the_foo']; 
?>

You can get and set cookies with js as well.

您也可以使用js获取和设置cookie。