简体   繁体   English

将Javascript变量发布到PHP

[英]Post Javascript variable to PHP

I use Facebook JavaScript SDK. 我使用Facebook JavaScript SDK。 I want to know how to post the Javascript variables to another page with GET or POST or any other way. 我想知道如何使用GET或POST或任何其他方式将Javascript变量发布到另一个页面。 For example i have: 例如我有:

userInfo = document.getElementById('user-info');

How to post it to new page ? 如何将其发布到新页面?

location.href="http://www.walla.com/?info=" + info.id;

Not working 不工作

Better use ajax for this .. 最好为此使用ajax。

here is an example. 这是一个例子。

<script type="text/javascript">
$(document).ready(function() {
   $('#submit-btn').click(function() {
     $.ajax({
       type:'POST',  //POST or GET depending if you want to use _GET or _POST in php
       url:'your-page.php', //Effectively the form action, where it goes to.
       data:$('#txtName').val(),  //The data to send to the page (Also look up form serialise)
       success:function(e) {
            // On success this fill fire, depends on the return you will use in your page.
       }
     });
     return false;
   });
});
</script>

<form>
  <input type="text" id="txtName" />
  <input type="submit" id="submit-btn" value="Send" />
</form>

Hope it works for u. 希望它对你有用。

That looks okay. 看起来还可以。 You should be able to obtain info.id via $_GET['info'] from PHP. 您应该能够通过$_GET['info']从PHP获取info.id。

However, that's assuming userInfo is a simple string. 但是,假设userInfo是一个简单的字符串。

If it isn't, you may have to convert it to string first. 如果不是,则可能必须先将其转换为字符串。 One way to do that is through JSON as JSON is decodable at PHP's side using json_decode() . 一种方法是通过JSON,因为JSON在PHP端可以使用json_decode()解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM