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)?

If you don’t need to get anything back from PHP, you could just create a fake image:

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”?

Here’s what I went with:

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?

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?)

Off Topic:

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

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

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.

Thanks for your help sdleihssirhc, AussieJohn , and webdev1958.

I appreciate your direction very much.