Asi
Asi

Reputation: 415

Post Javascript variable to PHP

I use Facebook JavaScript SDK. I want to know how to post the Javascript variables to another page with GET or POST or any other way. 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

Upvotes: 0

Views: 1122

Answers (2)

Nirali Joshi
Nirali Joshi

Reputation: 2008

Better use ajax for this ..

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.

Upvotes: 1

uzyn
uzyn

Reputation: 6693

That looks okay. You should be able to obtain info.id via $_GET['info'] from PHP.

However, that's assuming userInfo is a simple string.

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().

Upvotes: 0

Related Questions