Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
html file:
<head> 
	  <script type="text/javascript src="sendreq.js"></script>
	  </head>
	  <div id="button">
	    <form>
         <input type="button" id="req" value="chiedi amicizia" onclick="mandarichiesta()" />
        </form>
	  </div>

sendreq.js:
<script>
 export function sendRequest(method,url,params,keys){
		  var data=new FormData()
		  for (let i = 0; i < params.length; i++) {
			data.append(keys[i], params[i]);
          } 
		  var http = new XMLHttpRequest();
          http.open(method, url, true);

          http.onreadystatechange = function() {//Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
				return http.responseText;
            }
			
          }
          http.send(data);
	  }
		export function richiediamicizia(){
		  var scelta="richiestamicizia";
          var dest="'.$_GET["username"].'"		  
		  var k=["scelta","dest"];
		  var p=[scelta,dest];
		  return sendRequest("POST","messages.php",p,k);
		}
		export function mandarichiesta(){
			alert("tasto premuto");
			document.getElementById("button").innerHTML=richiediamicizia();
		  }
		 
</script>


What I have tried:

i tried to use import function from jsfile.js
Posted
Updated 1-Apr-22 4:56am
v2
Comments
Amarnath S 1-Apr-22 10:47am    
Looks like you have an extra closing bracket } just before the line http.send(data);

Just delete that } and check.
Riccardo Miccinilli 1-Apr-22 11:35am    
it's not extra, it closes sendrequest

1 solution

export is used for JavaScript modules[^], not regular scripts.

If you want to load your script as a module, you need to use type="module", not type="text/javascript". You'll need to use Javascript to attach your event handler - onclick won't be able to find the export.

If you want to use a regular script file, remove the export keywords from your file.
 
Share this answer
 
Comments
Riccardo Miccinilli 1-Apr-22 11:37am    
i used type="module" and result its the same
Richard Deeming 1-Apr-22 12:45pm    
Read my answer again; you cannot use onclick to reference an exported function from a module.

Either use Javascript to attach your event handler, or switch to using a normal script instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900