Hi. I am trying to send the name of a file which is going to be launched for execution using a script from a *.js file. So in the original *.php file i have imported the *.js file like this:

<script type = "text/javascript" src = "getListsInfo.js"></script>

Then, i use the onclick event on a radio button to call the function form *.js file:

<label>
        <input type="radio" name="name1" value="value" id="name1_0" onclick="function(param1, param2)"/>
        Description</label>

Now this is the script which calls the file which is going to be executed

function FUNCTION(param1, param2){	
	var strURL = "file.php?var1="+val1+"&var2="+val2;
	var req = getXMLHTTP();
	if(req){
		req.onreadystatechange = function(){
			if(req.readyState == 4){
				if(req.status == 200){
					document.getElementById(var2).innerHTML = req.responseText;
				}else{
					alert("There was a problem while using XMLHTTP:\n"+req.statusText);
					}
			}
		}
		req.open("GET", strURL, true);
		req.send(null);
	}
}

The question is how can i transfer the name of the file (file.php) which is going to be executed by the script above (in the *.js file), from the html/php from which I call the function FUNCTION, into the same script (from the *.js file) as a parameter, to make it dynamic. Now i have to write different scripts in case I want to do the same for several files.

Hope its clear want I want to do. Thank you!

Dani AI

Generated

Quick summary and practical follow-ups based on the thread: managed to pass the filename as a parameter on the onclick call, and suggested emitting JavaScript from PHP. Both approaches work, but there are cleaner and safer patterns that scale better when many files or actions are involved.

A recommended pattern is to keep markup simple (add a data-file or data-key attribute to the radio) and use a single event listener that builds the request. This removes inline handlers, centralizes logic, and makes it easy to encode parameters and handle errors. Example (client-side pattern):

/* radios carry data-file (logical key), not raw paths */
document.addEventListener('click', function(e){
  var t = e.target;
  if (!t.matches || !t.matches('input[type="radio"][data-file]')) return;
  var key = t.dataset.file;
  var url = '/ajax/handler.php?k=' + encodeURIComponent(key) + '&v=' + encodeURIComponent(t.value);
  fetch(url, { credentials: 'same-origin' })
    .then(function(r){ if (!r.ok) throw new Error(r.status); return r.text(); })
    .then(function(html){ document.getElementById('target').innerHTML = html; })
    .catch(function(err){ console.error('AJAX error', err); });
});

Server-side must never trust a client-supplied filename. Use a whitelist or key->file map and return proper status codes. Example (server-side idea):

<?php
$map = ['users' => 'users.php', 'lists' => 'lists.php'];
$key = $_GET['k'] ?? '';
if (!isset($map[$key])) { http_response_code(400); exit; }
include __DIR__ . '/ajax/' . $map[$key];
?>

Notes and troubleshooting tips: prefer returning JSON for structured responses when possible; set correct Content-Type headers; mirror the client-side key->file mapping on the server; add cache-control or a cache-buster for dynamic results; inspect the Network tab for HTTP status and response body; and use POST for non-idempotent actions. Generating JS from PHP (as mentioned) is valid but needs correct MIME headers and careful caching—often a single AJAX handler with mapping is simpler and safer.

Recommended Answers

All 2 Replies

You could do:

<script type="text/javascript" src="getListsInfo.php?filename=file.php"></script>

The getListsInfo.php would read the filename parameter and output (with a template so to speak) the requested js file (using echo).

Thank you. I have managed to add it as parameter in the onclick event in the html code, when i call the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.