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);
}

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

agree, the php code is executed on the server and the javascript is executed on the client side, this is done in different times.
0

If I'm understanding you correctly:

newdiv.innerHTML = 'select name="idp[]">' + $polozky[typ] + '</select>';

2 Comments

Then is $polozky a JS variable, or PHP variable? Your question is very poorly worded.
Sorry for my poorly worded question. $polozky is PHP variable :)
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.

1 Comment

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 <selects> (in $polozky there are <options>) ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.