SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Connecting Js To Php

    I need to create a dynamic link.

    My link is: <a href="javascript:void(0);" onclick="miniatures()">miniatures</a>

    My js function (so far):

    function miniatures() {
    var selection = "miniatures";
    //send js var to php var
    //call header
    }


    How do I send the value in a js var to a php var?

    How do I call a header in javascript (load a url)?

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you don't need to get anything back from PHP, you could just create a fake image:

    Code JavaScript:
    var faux = new Image();
    faux.src = 'somePage.php?name=' + selection;

    But something tells me that's not the case. What do you mean by "load a url"?
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's what I went with:
    HTML Code:
    function miniatures() {
    var ghi = "?user=jim&user2=jv";
    window.location = "http://localhost/ghi.php" + ghi;
    }
    Though I still would like to know how to turn a js var into a php var. I think AjAX will do that, but I haven't found the way. Do you have a reference for that, or can share the notation?

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    You can't directly turn a JS var in to a PHP var as one language runs on the client side and one on the server side.

    With AJAX however you could connect to a server side script and get them to "talk" to eachother.

    What are you really trying to achieve though? (i.e. what's the broader scope of your application?)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by nichemtktg View Post
    My link is: <a href="javascript:void(0);" onclick="miniatures()">miniatures</a>
    This is generally considered bad practice nowadays because a href is meant to be used to navigate to another page, not run javascript.

    A more acceptable way is to use

    Code:
    <a href="" onclick="miniatures(); return false;">miniatures</a>


    Quote Originally Posted by nichemtktg View Post
    How do I send the value in a js var to a php var?
    The short answer is you can't directly because as mentioned earlier, php code is run on the server before any html is sent down "the pipe" to the browser where your javascript is run.

    Your options include to use AJAX (if you don't want a page refresh) or send the variable's value in a query string to a php script and assign the sent value to a php variable and then do whatever processing you need server side before redirecting to wherever. But your options will depend on what you are trying to do overall.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    446
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help sdleihssirhc, AussieJohn , and webdev1958.

    I appreciate your direction very much.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •