0

I'd like to know, how can I transmit the value of parameter of JS function, into a "determination" of exact field in array (JS variable type instead of × in $polozky[ × ]). How can I possibly do this please?

function items(divName, typ){ 
          var newdiv = document.createElement('div');
          newdiv.innerHTML = \"<select name=idp[]>$polozky[ × ] </select>\";
          document.getElementById(divName).appendChild(newdiv);
}
user3019672
  • 47
  • 2
  • 8

3 Answers3

2

You can't.

JavaScript is client-side.
PHP is server-side.

That means:
First, the PHP code is performed by the Apache server and outputs (most commonly) any HTML and CSS and JavaScript code to the browser. By PHP it is treated simply as text. Once the PHP sent this text to the browser, it is no longer used (in this request).
Second, the browser brings these three (HTML, CSS, JavaScript) to life. It parser the HTML to the DOM, CSS to the layout and JavaScript to the live, functional scripting language.

You can pass anything you want only this way:

server-side --> client-side

The other way is obviously impossible. JavaScript can be performed on live websites by anyone. If the client --> server traffic would be possible, anyone could mess up with data on your server.

One of the possible ways to communicate with the server without page reloading is using AJAX technology. There are plenty of resources in the Web, like this one.

Community
  • 1
  • 1
matewka
  • 9,912
  • 2
  • 32
  • 43
0

If I'm understanding you correctly:

newdiv.innerHTML = 'select name="idp[]">' + $polozky[typ] + '</select>';
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

You can directly output your PHP into your Javascript.

function items(divName, typ){ 
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<select name=idp[]><?php echo $polozky['×'] ?></select>";
          document.getElementById(divName).appendChild(newdiv);
}

I don't recommend doing it inside your method but this is in keeping with your post.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Thanks for your post, but I can't see the answer.. I'd like to use the 'typ' JS parameter, to determine which item from the PHP $polozky will be displayed between (in $polozky there are ) ;) – user3019672 Nov 25 '13 at 20:06