将Javascript字符串传递给PHP [英] Passing a Javascript string to PHP
问题描述
我想将一个javascript字符串传递给php ...脚本中的代码后面是正确的..
i want to pass a javascript string to php ... WHICH is RIGHT after the code .. in the script.
<script type="text/javascript">
var myvar = "mytext" ;
<?php echo myvar ; ?>
</script>
这不起作用。
我该怎么办?
this does not work. What should i do ?
推荐答案
当有人访问网站时,通常会发生这种情况:
When someone visits a website, this is generally what happens:
- 他们的浏览器向服务器发送请求。
- 服务器评估该请求。
- 服务器意识到,Egad,他们要求的页面有PHP!
- 服务器评估PHP,只将结果发送到浏览器。
- 浏览器解析收到的内容。
- 浏览器意识到,Egad,我收到的页面有JavaScript!
- 浏览器完全在客户端的计算机上评估JavaScript。
- Their browser sends a request to the server.
- The server evaluates that request.
- The server realizes, "Egad, the page they're requesting has PHP!"
- The server evaluates the PHP, and only sends the results to the browser.
- The browser parses the content that it receives.
- The browser realizes, "Egad, the page I received has JavaScript!"
- The browser evaluates the JavaScript, entirely on the client's machine.
所以PHP和JavaScript基本上处于不同的端点这个过程。只有服务器处理PHP,只有客户端处理JavaScript。
So PHP and JavaScript are basically at different ends of the process. Only the server handles PHP, and only the client handles JavaScript.
要向PHP提供一个字符串,你必须提出PHP页面的请求,将该字符串作为GET变量发送:
To "give" a string to PHP, you'd have to make a request of the PHP page, sending that string as a GET variable:
http://www.yourdomain.com/some_php_page.php?myvar=mytext
使用JavaScript有几种方法可以做到这一点。
There are a few ways to do this with JavaScript.
-
如果您只关心在PHP页面上发出该请求,并且您不需要担心收到任何信息,您可以创建一个图像并使用URL作为来源:
If you only care about making that request on the PHP page, and you don't need to worry about receiving any information back, you can just create an image and use the URL as the source:
var fakeImg = new Image();
fakeImg.src = 'http://www.yourdomain.com/some_php_page.php?myvar=mytext';
即使您要求提供图像,服务器也不知道,并会处理您的图像请求通过调用PHP来评估它等等。
Even though you're requesting an image, the server doesn't know that, and will process your request by calling the PHP evaluating it, etc.
您可以发出实际的AJAX请求。首先创建一个XMLHttpRequest对象:
You can make an actual AJAX request. Start by creating an XMLHttpRequest object:
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
IE中有一些问题,对AJAX请求有缓存响应,所以要使网址唯一:
There are some issues in IE with cached responses on AJAX requests, so make the url unique:
var url = 'http://www.yourdomain.com/some_php_page.php?myvar=mytext&unique=whatever';
告诉你的XHR你想去哪里,以及你希望它到达那里:
Tell your XHR where you want it to go, and how you want it to get there:
xhr.open('GET', url, true);
// The "true" parameter tells it that we want this to be asynchronous
设置一个方法将检查收到回复的时间:
Set up a method that will check for when a response is received:
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status < 400) {
success(xhr.responseText);
}
};
最后,发送请求:
xhr.send(null);
// We set "null" because some browsers are pissy
要保留的一些注意事项介意:
Some notes to keep in mind:
- 你必须自己构建
success
函数来处理字符串您的PHP页面将返回。 - 如果您愿意,您可以传递该函数
xhr.responseXML
,但这通常只是麻烦我。 - 使用
onreadystatechange
我的方式(我相信)会在某些版本的IE中引入内存泄漏
- You have to build the
success
function yourself, to handle the string that your PHP page will return. - You can pass that function
xhr.responseXML
if you want, but that's usually just a hassle for me. - Using
onreadystatechange
the way I have will (I believe) introduce memory leaks in some versions of IE
这篇关于将Javascript字符串传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!