Passing Javascript vars in a URL.

Discussion in 'JavaScript' started by nfzgrld, Nov 21, 2008.

  1. #1
    Is this possible? I need to pass some vars generated by a js function to a php file that puts the data into a database. I'm using 'onmouseup' to activate a window.open and I want to put the results of two js functions in the query string. So far all I get is the name of the function I'm calling. How do I actually get the return value of the functions into the query string?
     
    nfzgrld, Nov 21, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    var ret1 = function1 (p1, p2);
    var ret2 = function2 (p3, p4);
    
    [url=jquery.com]jQuery[/url].ajax ({
      url : 'http://yourserver.com/php/script.php?ret1='+ret1,
      type : 'POST',
      data : {
         ret2 : ret2
      },
      success : function (data, textStatus) {
         //do something with the return value from PHP
      },
      error : function () {
         //whine and complain here
      }
    };
    
    Code (markup):
    In this example, one parameter is passed on the URL, (will show up in $_GET), and one in POST DATA (will show up in $_POST).. if you change the 'type' parameter to 'GET', then everything in 'data' will also be put on the url string..

    good luck
     
    rene7705, Nov 22, 2008 IP
  3. nfzgrld

    nfzgrld Peon

    Messages:
    524
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok, I've been going through this for many hours over the past couple of days. So far I haven't gotten an answer that works. There are large gaps in the information. I suspect it is because everyone responding assumes I know as much about javascript as they do. I don't. I don't work with js very much, and then just to make a tweak to a page here or there. Now I'm trying to do something that actually shouldn't be very difficult.

    I need to take the x and y coordinates of an element on the screen and get them to a PHP script. I've tried to do this two ways. First, I tried to pass the coordinates as vars in a query string on a url using window.open. That doesn't work. There is NO WAY to pass the variables. Also, if I try to use window.open from within a function it simply doesn't work. I'm sure it is supposed to, but I don't know how to do it, or how to determine what the problem is. So far all of the "fixes" I've been shown by people here and elsewhere don't seem to have anything to do with my problem.

    The second thing I tried to do is send the vars to a cookie. Once there I can just pull them from within PHP and move on from there. Unfortunately that doesn't work either. None of my functions seem to be returning anything, or even executing at all. So here is the question: How do I get this to set the cookies and return ANY value:
    
    var yco=parseInt(this.element.left);
    var xco=parseInt(this.element.top);
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+1);
    document.cookie=c_xco+ "=" +escape(xco)+";expires="+exdate.toGMTString();
    document.cookie=c_yco+ "=" +escape(yco)+";expires="+exdate.toGMTString();
    return true;
    
    Code (markup):
    I appreciate all of the info I've gotten up to now, but nothing is I've been shown so far works. There has to be a reason. The code above DOES NOT work. What's wrong with it. I need the answer to that question. Not necessarily some code snippets. Why doesn't this work!
     
    nfzgrld, Nov 22, 2008 IP
  4. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There's nothing wrong with using window.open() inside a function, I bet it was something else you've mistaken when you were trying to do it... window.open() is not be the best way to do what you want, but till you're asking, here's what can be wrong - depending on the type of data you are sending to the browser, it may need to be encoded before sending and then decoded by the php script. For encoding you can use encodeURI() and encodeURIComponent(). This will encode the variables so they will be send properly to the browser. When receiving the data, you have to make sure you decode it before use. You can do that with urldecode() inside the php script that you are referring to.
    You can try that and share the results. If it still doesn't work, post the code you're using and we'll see what's wrong :)
     
    xlcho, Nov 24, 2008 IP