I got the action "pega" in controller Posts:
public function pega($id = null)
{
$posts = $this->Post->findById($id);
foreach($posts as $pok)
{
$foda = $pok['love'];
}
$this->set('foda', $foda);
$this->set('_serialize', array('foda'));
}
In my layout I try to do a requestto catch the data from function "pega" and put inside tag html:
<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:81/booklandia/posts/pega/<?php echo $post['Post']['id'];? >.json";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var out = JSON.parse(xmlhttp.responseText);
function loap (){
var arr = out[0];
document.getElementById("id01").innerHTML = arr;
}
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
I have asked a question like this before, and the answer was great but the more I looked at my code the more confused I got, and after 10hrs my brain is shot and I'm in need of help. So all my content is loaded dynamically via Jquery's $.post and loaded into #content-container. Now I included my entire Jquery file to show you why I am getting so confused as how to get this to work without re-writing all the code. As you can see, and what I'm thinking, is that the history plugins such as BBQ, and history.js seem to be out of the question because I don't use anchor tags and the multitude of .live('click', funcition() { I have use different classes and id's for various reasons. I also believe that rules out using the hash and linking it to a single class via .trigger(), executing the AJAX(hope that makes sense). The only option I have left is actually kinda my question. Would it be possible to load the dynamic content into #content-container still using the $.post with $.load() or window.location and actually have the previous content loaded when the browser back button is clicked.
I greatly appreciate the help, really.....
$(document).ready(function() {
$(window).load(function () {
});
$('#map').hide();
$('#informational-container').hide();
$('.clickable').click(function(){
$('#map').hide();
});
function getTotalDealCount() {
$.post('../service/service.getTotalDealCount.php', {}, function(data) {
if(data.success) $('#deal-counter').append(data.results);
}, 'json');
return false;
}
function loadMap(lat, lon) {
var latlng = new google.maps.LatLng(lat, lon);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
}
function getLatLon(address, zipcode) {
$.post('../service/service.getLatLon.php', { address: address, zipcode: zipcode }, function(data) {
if(data.success) {
var res = data.results;
var coordinates = res.split('_');
loadMap(coordinates[0], coordinates[1]);
} else {
return false;
}
}, 'json');
return false;
}
function getAddressZipcode(id) {
$.post('../service/service.getAddressZipcode.php', { id: id }, function(data) {
if(data.success) {
var res = data.results;
var location = res.split('_');
getLatLon(location[0], location[1]);
} else {
return -1;
}
}, 'json');
return false;
}
$('#how-it-works').live('click', function() {
$('#content-container').load('../module/module.string.php #string-m1');
});
$('#why-post-a-deal').live('click', function() {
$('#content-container').load('../module/module.string.php #string-m2');
});
$('#who-are-we').live('click', function() {
$('#content-container').load('../module/module.string.php #string-m3');
});
$('#contact-us').live('click', function() {
$('#content-container').load('../module/module.string.php #string-m4');
});
$('#post-deal-img').live('click', function() {
$('#content-container').load('post.php');
});
$('.post-deal-inputs').live('click', function() {
$('.post-deal-inputs').live('click', function(e) {
$(this).val('');
$(this).removeClass('post-deal-inputs')
.addClass('post-deal-inputs-clicked');
});
});
$('#df5').live('click', function() {
$('#df7').val('');
$('#df7').hide();
});
$('#df6').live('click', function() {
$('#df7').show();
});
$('#post-how-to-link').live('click', function() {
apprise();
});
$('#terms').live('click', function() {
$('#content-container').load('../module/module.string.php #string-m5');
});
$('#disclaimer').live('click', function() {
$('#content-container').load('../module/module.string.php #string-m6');
});
$('#post-deal-button').live('click', function() {
var dealForm = $('#deal-form').serialize();
var company = $('#df1').val();
var description = $('#df2').val();
var zipcode = $('#df4').val();
var starts = $('#df7').val();
var ends = $('#df8').val();
if(company == '' || company == 'Company') {
apprise('A company name is required!');
return false;
}
if(description == '' || description == 'Deal') {
apprise('A deal is required!');
return false;
}
var swear = swearFilter(description);
if(swear != false) {
apprise('Please remove the naughty word `' + swear + '` from your deal.');
return false;
}
if(zipcode == '' || zipcode == 'Zipcode') {
apprise('A zipcode is required!');
return false;
}
if($('#df7').is(':visible') && typeof(ends) != 'undefined') {
var res = validateDate(starts);
if(res == false) {
apprise('Please select a valid start date!');
return false;
}
}
if(typeof(ends) != 'undefined') {
var res = validateDate(ends);
if(res == false) {
apprise('Please select a valid end date!');
return false;
}
}
if(ends == '') {
apprise('A deal end date is required!');
return false;
}
if($('#df7').is(':visible') && $('#df7').val() == '') {
apprise('If its not a one day sale a start date is required!');
return false
}
$.post('../service/service.postDeal.php', { dealForm: dealForm }, function(data) {
if(data.success == true) {
apprise('Your deal has been posted, thank you!');
window.location = '../root/index.php';
return true;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured and your deal has not been posted. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('The zipcode you supplied was not in our database. Please enter the city ' +
'and state/province in the following prompts to add your location.' , {'confirm':true}, function() {
insertNewLocation(zipcode);
});
}
if(data.fail == 3) {
apprise('A zipcode is required!');
}
}
}, 'json');
return false;
});
function validateDate(string) {
if(string.toLowerCase().indexOf('-') >= 0) {
var dateArray = string.split('-');
var year = dateArray[0], month = dateArray[1], day = dateArray[2];
if(year.length == 4 && month.length == 2 && day.length == 2) return true;
else return false;
}
return false;
}
function swearFilter(string) {
var ret = false;
var filter = [];
var stringArray = string.split(' ');
for(var i = 0; i < stringArray.length; i++) {
if(jQuery.inArray(stringArray[i], filter) > -1) {
ret = stringArray[i];
break;
}
}
return ret;
}
function insertNewLocation(zipcode) {
var city = '';
var state = '';
if (/[^a-zA-Z 0-9]+/g.test(zipcode)) {
apprise('The zipcode should contain only numbers, letters or both!');
return false;
}
apprise('Enter the name of the city:', {'input':true}, function(_city_) {
if(/[^a-zA-Z]+/g.test(_city_)) {
apprise('The city should contain only letters!');
return false;
}
city = _city_;
apprise('Enter the state in 2 letter abbreviated format:', {'input':true}, function(_state_) {
if (/[^a-zA-Z]+/g.test(_state_)) {
apprise('The state should contain only letters');
return false;
}
if(_state_.length > 2) {
apprise('The state should only be 2 letters in length');
}
state = _state_;
$.post('../service/service.insertNewLocation.php', { city: city, state: state, zipcode: zipcode }, function(data) {
if(data.success == true) {
apprise('Thank you, you may now submit you deal!');
return true;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured and your location has not been added to our database. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('A zipcode is required!');
return false;
}
}
}, 'json');
return false;
});
});
}
$('#search-type-button').live('click', function() {
var search = $('#search-type-text').val();
var keyword = $('#keyword-type-text').val();
if(search == '') {
apprise('A zipcode or city and state combo to search for deals in your area!');
return false;
}
if(keyword == 'Keyword') {
apprise('If you want to add a keyword to you deal, make sure its unique!');
return false;
}
$.post('../service/service.loadDealsByCityOrZipcode.php', { search: search, keyword: keyword }, function(data) {
if(data.success) {
$('#content-container').html(data.results);
return true;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured locating the deals. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('There are no deals within the zipcode you provided, be the first to add one!');
return false;
}
if(data.fail == 3) {
apprise('There are no deals matching the tag you supplied!');
return false;
}
if(data.fail == 4) {
apprise('A zipcode or city and state combo to search for deals in your area!');
return false;
}
}
}, 'json');
return false;
});
$('.deals-queried-deal').live('click', function() {
var id = this.id;
$('#comment-link').show();
$.post('../service/service.loadDealDescription.php', { id: id }, function(data) {
if(data.success == true) {
$('#content-container').html(data.results);
var res = getAddressZipcode(id);
if(res != -1) $('#map').show();
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured loading the deal you selected. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
} else if(data.fail == 2) {
apprise('The deal you selected does not exist! ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
} else if(data.fail == 3) {
apprise('An error has occured loading the deal you selected. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
}
}
}, 'json');
return false;
});
$('.vote-down').live('click', function() {
var id = this.id;
var vote = 0;
$.post('../service/service.tallyVote.php', { id: id, vote: vote }, function(data) {
if(data.success == true) {
apprise('Thank you for your vote!');
return true;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured when voting for this deal. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('You have already voted for this deal!');
return false;
}
}
}, 'json');
return false;
});
$('.vote-up').live('click', function() {
var id = this.id;
var vote = 1;
$.post('../service/service.tallyVote.php', { id: id, vote: vote }, function(data) {
if(data.success == true) {
apprise('Thank you for your vote!');
return true;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured when voting for this deal. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('You have already voted for this deal!');
return false;
}
}
}, 'json');
return false;
});
$('#email-button').live('click', function() {
var emailAddress = $('.email-textbox').val();
var from = $('#from-textbox').val();
var message = $('#email-div').html();
var atpos = emailAddress.indexOf("#");
var dotpos = emailAddress.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailAddress.length) {
apprise('Please enter a valid email address!');
return false;
}
if(emailAddress == '') {
apprise('An email address is required!');
return false;
}
if(from == '') {
apprise('Your name is required!');
return false;
}
$.post('../service/service.emailDeal.php', { emailAddress: emailAddress, from: from, message: message}, function(data) {
if(data.success == true) {
apprise('The deal has been sent, we hope they enjoy it!');
return true;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured sending the email. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('An email address is required!');
return false;
}
}
}, 'json');
return false;
});
$('.comment-link').live('click', function() {
var id = this.id;
$.post('../service/service.loadComments.php', { id: id }, function(data) {
if(data.success == true) {
$('#content-container').html(data.results);
return true;
}
if(data.success == false) {
apprise('An error has occured retrieving the comments for this deal. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
}, 'json');
return false;
});
$('.post-comment-button').live('click', function() {
var id = this.id;
var comment = $('#comment-textarea').val();
var author = $('#post-comment-whois').val();
var swear = swearFilter(comment);
if(swear == true) {
apprise('Please remove the word "' + swear + '" from your comment.');
return false;
}
if(comment == '') {
apprise('Please fill out a comment first!');
return false;
}
$.post('../service/service.postComment.php', { id: id, comment: comment, author: author }, function(data) {
if(data.success == true) {
$.post('../service/service.loadComments.php', { id: id }, function(data) {
if(data.success == true) {
$('#content-container').html(data.results);
return true;
}
if(data.success == false) {
apprise('An error has occured retrieving the comments for this deal. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
}, 'json');
return false;
}
if(data.success == false) {
if(data.fail == 1) {
apprise('An error has occured while attempting to post your comment. ' +
'The error has been emailed to our support department to have the issue fixed, we apologize :(');
return false;
}
if(data.fail == 2) {
apprise('Please fill out a comment first!');
return false;
}
}
}, 'json');
return false;
});
});`
Use history.js to pushState and then window.history.back()
My Ajax works just fine until I add a conditional status property.
Here's the snippet
if (XMLHttp.readyState==4 && XMLHttp.status==200){
// do something
}
and here's the complete code
function getXMLHttp()
{
try
{
var xmlhttp = new XMLHttpRequest();
// document.getElementById("Content").innerHTML="<h1>Using XMLHttpRequest Object</h1>";
//alert('Mozilla XMLHttpRequest Obeject Created Successfully');
}
catch(err1)
{
var ieXmlHttpVersions = new Array();
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";
var i;
for (i=0; i < ieXmlHttpVersions.length; i++)
{
try
{
var xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
// var catatan = "<h1>Using " + ieXmlHttpVersions[i] + "</h1>";
break;
}
catch (err2)
{
var xmlhttp = null;
//alert(ieXmlHttpVersions[i] + " not supported.");
}
}
}
if (typeof xmlhttp == "undefined" || xmlhttp == null){
//document.getElementById("Content").innerHTML="<h1>XMLHttp cannot be created!</h1>";
alert('XMLHttp Request Object Is not Supported Somehow');
}
return xmlhttp;
}
var XMLHttp = getXMLHttp();
function loadData(url, targetID){
if(!url) {var url="data.txt";}
if(!targetID){var targetID='ajaxID';}
XMLHttp.onreadystatechange = function (){getResponse(targetID)};
XMLHttp.open('GET', url, true);
XMLHttp.send(null);
}
function getResponse(targetID){
var data = XMLHttp.responseText;
var ajaxContent=document.getElementById('ajax_content');
if(XMLHttp.readyState == 4){
// This works Just fine, data from data.txt actually fetched
// BUT When i Add this if statement with " && XMLHttp.status==200" It's not returning data from data.txt
if(data.length > 0){
fill(targetID,data);
}
}
}
function fill(ID,data){
hideLoading();
document.getElementById(ID).innerHTML = data;
}
function showLoading(){
document.getElementById('loading').style.visibility='';
document.getElementById('loading_text').innerHTML = '....Loading Please Wait....';
}
function hideLoading(){
document.getElementById('loading').style.visibility = 'hidden';
document.getElementById('loading_text').innerHTML = '';
}
My question is why I can't get the data from data.txt when I add && XMLHttp.status==200 statement ?
Could your web-server be returning one of the other 'success' status codes from the HTTP specification?
Try testing if (XMLHttp.status >= 200 && XMLHttp.status < 300).
Can you tell us whether you know or don't know if execution-flow is getting to the fill(targetID,data) line? It would be very unusual if the XMLHttp.status check were interfering in the actual data retrieval step.
From what you've told us, it seems that XMLHttp.readyState must be reaching 4 without XMLHttp.status == 200. Have I misinterpreted your question?
I think its the problem in creating the XMLHTTP object. try use the basic syntax that has been provided by w3schools.
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
its work for me or use jquery or prototype.
I am using window.onbeforeunload in my javascript.
This works perfectly in IE but is not triggered in Firefox.
I checked on different links in stackoverflow.... In it I read that window.onbeforeunload is not supported by firefox. Is this true?
If yes, can you please tell me a different way to call app.deleteAccount(key) on close of browser. Here is my javascript. Please look at the deleteFile() and dontDeleteFile() methods.
<script type="text/javascript">
//Event handler for body onload
function confirmDeleteFile(){
var secured =document.r_form.Secure[1].checked;
alert("confirmDeleteFile : "+secured);
if(secured){
var agree=confirm("Are you sure you wish to continue?");
if (agree){
//document.form1.submit();
return true;
}
else
return false ;
}
// submitForm();
return true;
}
function deleteFile() {
alert('inside deleteFile() but outside window.onbeforeunload');
window.onbeforeunload = function(){
var key = DOMAIN+'::' + elem('userName').value;
alert('inside deleteFile()');
app.deleteAccount(key)
alert('Unloading!');
}
}
function dontDeleteFile() {
alert('inside dontDeleteFile() but outside window.onbeforeunload');
window.onbeforeunload = function(){
alert("Not deleting");
}
}
function validateFormOnSubmit(theForm) {
var reason = "";
var userName = theForm.username.value;
var pin = theForm.pin1.value;
var PAM = theForm.pam.value;
var organization = theForm.organization.value;
//reason += validateUsername(theForm.username);
reason += validateEmpty(theForm.pam);
reason += validatePinMatch(theForm.pin1,theForm.pin2);
reason += validatePin(theForm.pin1,theForm.pin2);
if (reason != "") {
if(!confirmDeleteFile()){
return false;
}
alert("Some fields need correction:\n" + reason);
return false;
}
else{
if(!confirmDeleteFile()){
return false;
}
<% String url = request.getServerName().toString();
int port = request.getServerPort();
String contextPath = request.getContextPath();
%>
var servlet = "arcotebank.az"; //AroctEBanking Servlet
var url = BASE_URL + '/' + servlet;
var query = 'lang=en&reqid=1&version=1.1';
query += '&device=' + urlEncode(navigator.userAgent);
query += '&uid=' + urlEncode(userName);
query += '&code=' + urlEncode(PAM);
query += '&pin=' + urlEncode(pin);
query += '&usePin=' + usePin+'&method=arcotOTPEnroll&organization='+organization;
//alert("url=>"+url + '?' + query);
var xml = app.openUrl(url + '?' + query) + '';
//alert("xml=>"+xml);
if(appError()){
alert("applet error");
}
var domain = getDomain(url);
app.provisionAccount(domain, xml);
if(appError()){
alert("applet error");
}
var acc = app.getAccount(DOMAIN + '::' + userName);
if(acc!=null){
<%String formSubmitAction1 =
URLEncoderDecoder.encodeURL(
formAction,
"Action.2FA.Arcot.Navigation.LogoutActionCalled=Y",cm);%>
theForm.action ='<%=formSubmitAction1%>';
var secured =document.r_form.Secure[1].checked;
alert("line 131 "+secured);
if(secured){
deleteFile();
}else{
dontDeleteFile();
}
theForm.submit();
}else{
document.getElementById("error").innerHTML = "Failed to Create ArcotOTP";
}
}
}
function resetForm(){
var form = document.forms[0];
form.username.value = '';
form.pam.value = '';
form.pin1.value = '';
form.pin2.value = '';
}
function validateUsername(fld) {
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter a username.\n";
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
fld.style.background = 'Yellow';
error = "The username should contain more than 4 characters.\n";
} else if (illegalChars.test(fld.value)) {
fld.style.background = 'Yellow';
error = "The username contains illegal characters.\n";
} else {
fld.style.background = 'White';
}
return error;
}
function validateEmpty(fld) {
var error = "";
if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "You didn't enter Personal Assurance Message \n"
} else {
fld.style.background = 'White';
}
return error;
}
function validatePin(pin1,pin2){
var error="";
if(pin1.value!=pin2.value){
pin1.style.background = 'Yellow';
pin2.style.background = 'Yellow';
error += "Pin numbers dont match\n";
//alert("Pin numbers dont match");
}
return error;
}
function validatePinMatch(pin1,pin2){
var error="";
if(pin1.value==""){
//elem('otp').style.background = 'Yellow';
pin1.style.background = 'Yellow';
error += "Pin number cannot be empty\n";
//alert("Pin number cannot be empty");
}
if(pin2.value==""){
//elem('otp').style.background = 'Yellow';
pin2.style.background = 'Yellow';
error += "Confirm Pin number cannot be empty\n";
//alert("Pin number cannot be empty");
}
return error;
}
</script>
Note that in Firefox 4 and later the returned string is not displayed to the user. See Bug 588292.
https://developer.mozilla.org/en/DOM/window.onbeforeunload
Maybe this is what's causing your problem in part. Also the example on the page might be better suited for cross-browser compatibility?
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
window.onbeforeunload is supported by Firefox and all major browsers. It should be implemented as a function that returns a string, which will be used as the message in the confirmation dialog.
function getXMLHTTP() {
var xmlhttp=false;
try
{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function display_state(countryIdTEXT)
{
var countryId = countryIdTEXT;
alert(countryId);
if(countryId !='')
{
var strURL="select_state.php?con_name="+countryId;//alert(cate_id);
alert(strURL);
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
document.getElementById('content').innerHTML=req.responseText;
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
}
HTML
<select name="country" id="country" onchange="display_state(this.value)" style="width:225px;" >
<option value="selectCountry">Select Country</option>
{section name=view loop=$country}
<option value="{$country[view].Ident}" {if $country[view].Ident eq $smarty.request.country } selected="selected" {/if} >{$country[view].Country_name}</option>
{/section}
</select>
Forget to give 777 permission for templates_c folder. Now its working well..