Jump to content

Php Code Needs Ajax To Prevent Page Reload


strydom

Recommended Posts

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.