Home > Backend Development > PHP Tutorial > js里写php代码

js里写php代码

WBOY
Release: 2016-06-23 13:22:56
Original
1384 people have browsed it

common.jsfunction goUrl(lang){...//   $_SESSION['language']=lang   这句代码在js里要怎么写?...}
Copy after login


回复讨论(解决方案)

js和php的交互最好用ajax来完成

一般是php里嵌js,哪里是js里写php呀,要服务器支持的。

function goUrl(lang){
...

...
}

js里要跟PHP关联,一般都是要用到AJAX请求,如果是在JS加载时就得到(取)session的值,可以在js 代码块里写 如果是想在加载页面时设置session,这个跟js就没关系了,可以直接

main.inc.php<?php...$language = 'tc';if (isset($_G['lang']) && in_array($_G['lang'], array('tc', 'sc'))) {    $language = $_G['lang'];    $_SESSION['language'] = $language;} else if (isset($_SESSION['language']) && !empty($_SESSION['language'])) {    $language = $_SESSION['language'];} else {    $_SESSION['language'] = $language;}...?>index.php<?phprequire_once dirname(__FILE__) . '/includes/main.inc.php';    $tpl->display('index.html');?>index.html[code=php]<html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <script type="text/javascript" src="js/common.js"></script>    </head>    <title></title>    <body>        <li>            <a class="lang_tc" onclick="goUrl('tc')" href="javascript:;">繁</a>        </li>        <li>            <a class="lang_sc" onclick="goUrl('sc')" href="javascript:;">简</a>        </li>        <li>            <a href="a.php">New</a>        </li>        繁,繁,繁,繁,繁    </body></html>common.jsfunction goUrl(lang){    var url = document.location.href;    var query = window.location.search.substring(1);    var urls = url.replace(window.location.hash, '').split("?");    var vars = query.split("&");    var query = "";    for (var i=0;i<vars.length;i++) {        var pair = vars[i].split("=");        if (pair[0] != "lang" && pair[0] != "") {            query = query + pair[0] + "=" + pair[1] + "&";        }    }    query = query + "lang=" + lang;    location.href = urls[0]+"?"+query+window.location.hash;}
Copy after login

点击  index.html  “繁” 切换语言  ()
我在调用 goUrl() 的同时改变  $_SESSION['language'] 
所以我就想在js里添加php代码,如果像下面那样又会报错,求解决方法
function goUrl(lang){
...

...

1、session 变量存储于服务端,客户端不能主动修改
2、你在 goUrl() 中有 location.href = urls[0]+"?"+query+window.location.hash; 跳转
所以你应在 urls[0] 指向的程序文件中做 session 变量设置

1、session 变量存储于服务端,客户端不能主动修改
2、你在 goUrl() 中有 location.href = urls[0]+"?"+query+window.location.hash; 跳转
所以你应在 urls[0] 指向的程序文件中做 session 变量设置



跳转后也是到index.php ->index.html   执行的goUrl()时在原本的链接上添加  ?lang=XX 来改变语言

function goUrl(lang){
    var url = document.location.href;
    var query = window.location.search.substring(1);
    var urls = url.replace(window.location.hash, '').split("?");
    var vars = query.split("&");
    var query = "";
    for (var i=0;i        var pair = vars[i].split("=");
        if (pair[0] != "lang" && pair[0] != "") {
            query = query + pair[0] + "=" + pair[1] + "&";
        }
    }
    query = query + "lang=" + lang;
    location.href = urls[0]+"?"+query+window.location.hash;
}
我想创建goUrl.php 来代替上面 在php里怎么实现这段js的功能

改变语言后,页面需要刷新,你原来的写法并不需要改变
你的 goUrl.php 总还是要像  index.php 那样输出页面内容的。不显得累赘吗?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template