Jump to content


Photo

Php Code Needs Ajax To Prevent Page Reload

php ajax code jquery serial

  • Please log in to reply
No replies to this topic

#1 strydom

strydom

    Member

  • Members
  • PipPip
  • 10 posts

Posted 18 November 2012 - 03:24 PM

Hi there,
Basically what i need is for my site to not refresh every time i click one of the buttons. I have got as far as finding out that it requires AJAX but i don’t have a clue how to use it :confused:

This is some of my code...it basically covers the main parts but if you need more then please ask...
<html>
<head>
<script>
$(function() {
	 var tabs = $( "#tabs" ).tabs({collapsible: true, active:"flase"});
	 tabs.find( ".ui-tabs-nav" ).sortable({axis:"x", stop:function() {tabs.tabs( "refresh" );}
	 });
});

$(function() {
	 $( ".box" ).draggable({containment: "#restrictor", scroll: false, snap: true, opacity: 0.35,
cancel: ".box-content"
});

	 $( ".box" ).addClass( "ui-widget ui-widget-content ui-corner-all" )
		 .find( ".box-header" )
		 .addClass( "ui-widget-header ui-corner-all" )
		 .prepend( "<span class='ui-icon ui-icon-minusthick'></span>")
		 .end()
		 .find( ".box-content" );

	 $( ".box-header .ui-icon" ).click(function() {
		 $( this ).toggleClass( "ui-icon-minusthick" ).toggleClass( "ui-icon-plusthick" );
		 $( this ).parents( ".box:first" ).find( ".box-content" ).toggle();
});

$( "#container" ).disableSelection();
});

$(function() {
	 $( "input[type=submit], #Buttons, button" )
		 .button()
		 .click(function( event ) {
			 event.preventDefault();
		 });
});

setInterval(function()
{
$('#timeval').load('Home_Automation/Includes/time.php');
}, 1000);
</script>
</head>
<body>


<!------------------------Serial Port/Commands/Functions--------------------------->
<?php

if (isset($_GET['action'])) {

require("Home_Automation/Includes/php_serial.class.php");

$serial = new phpSerial();

$serial->deviceSet("COM3");

exec('mode COM3 BAUD=96 PARITY=n DATA=8 STOP=1 xon=off octs=off rts=on');

$serial->deviceOpen();

if ($_GET['action'] == "on") {
$serial->sendMessage("L");
$file = fopen("Home_Automation/Includes/lights.txt","w");
fwrite($file, "1");
fclose($file);

} else if ($_GET['action'] == "off") {
$serial->sendMessage("l");
$file = fopen("Home_Automation/Includes/lights.txt","w");
fwrite($file, "0");
fclose($file);

} else if ($_GET['action'] == "unlock") {
$serial->sendMessage("D");
$file = fopen("Home_Automation/Includes/doors.txt","w");
fwrite($file, "1");
fclose($file);

}

$serial->deviceClose();
}
?>

<!--------------------------------Main Container----------------------------------->
<div id="restrictor" class="ui-widget-content">

<!--------------------------------Controll Switches-------------------------------->
<!-------------------------------------Doors--------------------------------------->
<div class="box">
<div class="box-header">Doors</div>
<div class="box-content">
<a class="button" href="<?php echo $_SERVER['PHP_SELF'] . '?action=unlock' ?>">Unlock</a>
<div id="PHP">
<?php
$file = fopen("Home_Automation/Includes/doors.txt", "r") or exit("Unable to open file!");
	 if (fgetc($file)==1)
			 echo "<img src=\"Home_Automation/Images/Door_Open.gif\" />";

	 else
			 echo "<img src=\"Home_Automation/Images/Door_Closed.gif\" />";
fclose($file);
?>
</div>
<a class="button" href="<?php echo $_SERVER['PHP_SELF'] . '?action=lock' ?>">Lock</a>
</div></div>
</div>
</html>

It’s not really in order as i cut bits out but that covers the basic code...

If someone could please show me some code snippets that i would need to include or something that would be great :happy-04:

Thanks,
Chris

Edited by strydom, 18 November 2012 - 03:25 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com