7 Replies - 12487 Views - Last Post: 15 September 2011 - 09:10 PM

#1 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

JavaScript to load/run/call php file

Posted 13 September 2011 - 07:35 PM

First let me clarify I know that Java runs client side and php runs server side.

I have setfmode.php files set up to send commands to a wifi thermostat. What I'm trying to get here is to call a php file in the same directory as the index.php when a javascript button is clicked. I have the below index.php, which shows an alert just fine, but doesn't want to load the php as requested. when I go directly to the setfmode.php site it sends the command to the tstat as designed.

<html>
<body>

<div id="myDiv"><h2>Let AJAX set your fan mode</h2></div>
<button type="button" onclick="SetAuto()">Auto</button>
<button type="button" onclick="SetOn()">On</button>

</body>
</html> 

<head>
<script type="text/javascript">
function SetAuto()
{
	alert("Auto");
}
function SetOn()
{
	//alert("On");
	loadurl('/setfmode.php');
}
</script>
</head> 


At the moment I don't even care if I get a response from the php as long as it actually executes server side. Thanks for any help here.

Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript to load/run/call php file

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1937
  • View blog
  • Posts: 4,022
  • Joined: 11-December 07

Re: JavaScript to load/run/call php file

Posted 13 September 2011 - 07:38 PM

I was about to write about how Java can be server side too... but then I realised you were not talking about Java at all. I'll move your topic somewhere more webby for you. ;)
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,367
  • Joined: 27-December 08

Re: JavaScript to load/run/call php file

Posted 13 September 2011 - 07:38 PM

This seems more like a Javascript question, not a Java question. They are two different languages. I'll move this to Javascript. :)
Was This Post Helpful? 1
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1937
  • View blog
  • Posts: 4,022
  • Joined: 11-December 07

Re: JavaScript to load/run/call php file

Posted 13 September 2011 - 07:40 PM

Do I get the credit if I post about moving it first even if macosnerd101 actually does the heavy lifting?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,367
  • Joined: 27-December 08

Re: JavaScript to load/run/call php file

Posted 13 September 2011 - 07:41 PM

We'll go with yes on that one. ;)
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: JavaScript to load/run/call php file

Posted 13 September 2011 - 09:03 PM

loadurl? I've never came across a function like that and I don't see any external Javascript files being loaded either.

Anyways I'd suggest using Ajax to do this. Ajax allows you to asynchronously send data to the server without having to reload a web page. You have the option to get the response which is encourage so you'll know if the script was executed or not.
Was This Post Helpful? 0
  • +
  • -

#7 SpiderSpartan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 120
  • Joined: 06-February 08

Re: JavaScript to load/run/call php file

Posted 14 September 2011 - 03:55 PM

man, I thought I was posting in javascript. Sorry about that.

As far as my question, thanks for the suggestion on AJAX. I was able to get it working using the example on W3C to load my php script. Now I just have to figure out how to pass variables to my php so I can change multiple settings calling the same php script. Anyway, here's what I've got going now.

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","setfmode.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX set your fmode</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: JavaScript to load/run/call php file

Posted 15 September 2011 - 09:10 PM

View PostSpiderSpartan, on 14 September 2011 - 11:55 PM, said:

Now I just have to figure out how to pass variables to my php so I can change multiple settings calling the same php script.


Send variables using which method? Post or get?

To send a get variable using Ajax, you would just have to do something like this with your call:
xmlhttp.open("GET","setfmode.php?variable1=variable1value",true);


but for post variables, you have to do a little more, setting request headers "Content-type" and then send the parameters when you send it, so it would look something like this: xmlhttp.send(params); where the parameters are formatted as "var1=val&var2=val2".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1