Ajax content and Css positioning - php

Hoping someone can see my error. Probably easiest if you take a look at my test page
Notice the shifting of the contents within <div id="ajax" style="background-color:red">
when clicking on a .menu {position:fixed;} element.
Strange that only one (Home) shifts not the others (Prog, Jet, Wind, Mesonet, Disc,Rad/Sat)
The PHP that the ajax calls is of similar format on all pages as follows:
<?php
// do php stuff
echo <<<HTML
<div class="content">
<!-- html stuff -->
</div>
HTML;
Don't know if its a PHP or css issue nothing I've tried has given any indication.
Appreciate your ideas... thanks
EDIT: added ajax
var options = {
success: function(result) { $("#ajax").html(result); },
beforeSend: function() {},
url: file,
type: "post"
};
$.ajax(options);
Edit: Resolved
The answer to my question is "something is inserting some whitespace before the content div" as given by Explosion Pills. Strangely that something is caused by PHP's include "file.php"; being called inside home.php
Another question is appropriate I think so I'll post the link here later.
Thanks All
follow up question here

Your success function is replacing your content:
<div id="ajax" style="background-color:red"><div class="content"></div></div>
with:
<div id="ajax" style="background-color:red">Ajax Content</div>
The class 'content' has CSS: .content {margin:auto; background-color:#eee; border-radius:15px; border:3px solid black; padding:.5em;} that is removed with the ajax success.
Just remove <div class="content"></div> from your page or modify the success function to replace the content of the first div using something like: success: function(result) { $("#ajax div:first").html(result); },...

Related

If user has clicked link add class and store using PHP/Wordpress

I have multiple divs on my website that are clickable. I simply want that when the user clicks this div, a class is then added so they know they have viewed this.
For example, we could have a list of 10 links on the site. Once they click a link, the 'viewed' class would be added, to set a background colour to green.
I know the below is a simply way to do this in jQuery, but if someone could please help with storing this in PHP it would help so much, I am quite stuck on this!
$(function() {
$('.clickable-links > div').click(function() {
$(this).addClass("viewed");
});
});
.viewed {
background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.5.0.min.js" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>
<div class="clickable-links">
<div class="link1">Lnk 1</div>
<div class="link2">Lnk 2</div>
<div class="link3">Lnk 3</div>
</div>
You could add a jquery ajax in order to save the status change in your php.
By example:
$(function() {
$('.clickable-links > div').click(function() {
link= $(this).text()
$(this).addClass("viewed");
$.ajax({
type: "POST",
url: "/url/to/php/script",
data: "link=" + link,
})
})
});
Then in your PHP script you acces the clicked link with:
$link=$_POST['link']
It's just a thought. It can be improved.

AJAX call successful, but not replacing div as expected

After much Googling, I'm on the cusp of success (I think).
I have a fairly large page, overview.php that contains the following snippets:
<script>
function getSummary(id)
{
$.ajax({
type: "GET",
url: '_table_category.php',
data: "catid=" + id,
success: function(data) {
$('#summary').html(data);
alert('Successfully called');
},
error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});
}
</script>
Down the page, I iterate over an array and populate a table. The first column is as follow:
<td> <a onclick="getSummary('<?php echo $expenses[$k]['categoryid']; ?>')"> <?php echo $expenses[$k]['shortdesc']; ?> </a> </td>
And then further down on the page, there is a div that will contain the details:
<div class="box">
<div class="box-header"> <h3 class="box-title"> Details </h3> </div>
<div class="box-body">
<div id="#summary"> Select a category to see the details. </div>
</div>
</div>
No errors upon loading the page, nor by clicking on the 'link'. If I click on the link, it pops up with the 'Successfully called' alert as expected.
Looking in the Network tab of the developer tools in Chrome, it calls the _table_category.php page (with a value for catid), and if I click on the link it lists (for example _table_category.php?catid=35), the HTML in the preview pane is correctly formatted HTML.
If I then paste the HTML inside the div manually, it looks exactly as hoped.
This makes me think I am missing something fairly obvious and I am just not actually replacing the div with the result?
Your div has a bad ID.
<div id="#summary">
It should just be:
<div id="summary">
The # is just there in the selector (it's used to specify "select by ID").

How to keep my text on the same line with AJAX call?

I'm trying to keep my ajax call on the same line, as detailed here: http://external.sidewaykill.com/versound/motd.php.
It won't, so what can I do?
$(document).ready(function() {
$.ajaxSetup ({
cache: false
});
var ajax_load = "<img src='http://static.sidewaykill.com/img/ajax-loader2.gif' alt='loading...' />";
// load() functions
var loadUrl = "playercount.php?id=2";
$("#servercount").html(ajax_load).load(loadUrl);
});
This has nothing to do with PHP but everything to do with the absolutely awful markup of your HTML and the lack of CSS. You're going to want to wrap all of this "free-text" inside of a div or two, and then make sure you're putting the wrapper div inside of the infobar div
Sample CSS:
#infobar p{
float: left;
margin-left: 5px;
}
HTML:
<div id="infobar">
<div style="float: left;">
<p>Welcome! Our server count is:</p>
<p>1 / 10</p>
<p>We hope you enjoy your stay! We are currently playing on gm_construct.</p>
</div>
</div>
its because the ajax call returning a div element,
as this :
<div id="count"> 0 / 10</div>
Since div is a block element it will always render in a new line. there are two solutions for this
one would be to return a <span> element instead of the div
second will be if u are unable to change the return value you can fix it via css
#servercount #count { display : inline }
My suggestion is to go with the first solutions

jQuery code will not work/apply to elements that are within a file called from an ajax using javascript

Hoping someone can point me in the right direction to an annoying problem.
To summarize before I put the code; I have two files.. a html file with javascript called "bookings.php", and a php file with php "getBookings.php".
The bookings.php file contains all the static information like menus, text etc along with javascript + ajax calls.
I'm calling the getBookings file successfully with javascript/ajax and works well and i don't have any problems with it.
My problem is, that from the file that is loaded (getBookings.php); there are some html elements that i need to toggle. The jQuery for it, I have included in the bookings.php file.. but the jquery won't apply or work when the ajax file is called.
All the contents on getBookings.php gets loaded into an #bookingSummary element.
Now, my code is very long, so i will include all the important parts..
// Usual Head Content, Meta, CSS, jQuery files
// Filters
<script language="javascript" type="text/javascript">
function bookings()
{
var fltr="";
if(document.frm.bkid.value != "")
fltr+='bkid='+document.frm.bkid.value+'&';
if(document.frm.customer.value != "")
fltr+='cid='+document.frm.customer.value+'&';
if(document.frm.site.selectedIndex != 0)
fltr+='sid='+document.frm.site.value+'&';
if(document.frm.status.selectedIndex != 0)
fltr+='st='+document.frm.status.value+'&';
if(document.frm.supp.selectedIndex != 0)
fltr+='sup='+document.frm.supp.value+'&';
if(document.frm.datefrm.value != "")
fltr+='dfrm='+encodeURI(document.frm.datefrm.value)+'&dto='+encodeURI(document.frm.dateto.value);
document.getElementById('bookingsummary').innerHTML="<br /><p align='center'> <img src='img/bigloading.gif'></p>";
url='get/getBookings.php?'+fltr;
getAjaxRec(url, viewbookings);
}
function viewbookings()
{
if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
{
document.getElementById("bookingsummary").innerHTML = xmlHttp.responseText;
}
}
</script>
The Above code is the javascript that all works fine.. the following is the html code that calls the above function - bookings():
<!-- Several Form fields are here..-->
<input type="button" value="Apply Filter" class="searchBtn" onClick="bookings();">
<!-- Show booking results -->
<div id="bookingsummary"></div>
Now, within the getBookings file there is an html element with a class name "hiddenDetails" that i need to toggle and the Jquery for it is:
<script type="text/javascript">
$(document).ready(function() {
$('.bookingKeyDetails').click(function()
{
$(this).next('.hiddenDetails').slideToggle('medium");
});
});
</script>
The above code all works apart from the jquery.. here is a snippet from the getBookings.php file.. the part i want to apply the jquery to..
<?php
// A while loop happens then..
$result="
<div class=\"bookingKeyInfo\">
<div class=\"lb1\"><input type=\"checkbox\" name=sup1x".$rw['id']." ></div>
<div class=\"lb2\">".$rw['id']." </div>
<div class=\"lb3\">".date('d/m/Y', strtotime($rw['pickupDate']))." <strong><font color=\"#FF0000\">".date('H:i', strtotime($rw['pickupTime']))."</font></strong></div>
<div class=\"clearLeft\"></div>
</div>
<div class=\"bookingKeyDetails\">
<div class=\"b-one\"><img style=\"margin-top:9px; padding-bottom: 5px;\" src=\"$base_dir/img/".strtolower($rw['status']).".png\"></div>
<div class=\"b-two\"><img id=\"isCustomerEmailed\" style=\"margin-top:9px; padding- bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-three\"><img id=\"isSupplierEmailed\" style=\"margin-top:9px; padding- bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-four\"><img id=\"isSupplierConfirmed\" style=\"margin-top:9px; padding- bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-trip\">".substr($rw['DA'],0,6)."..<strong> to </strong> ".substr($rw['DB'],0,6)."..</div>
<div class=\"b-pax\">".$rw['pax']." Pax</div>
<div class=\"b-cost\">€$cost </div>
<div class=\"b-supplier\">".substr($sup,0,10)."..</div>
<div class=\"clearLeft\"></div>
<div class=\"hiddenDetails\">More Booking Information...</div>
</div>";
echo $result;
?>
I know this is a very long description of the problem.. so I have tried to give my code exactly as it is..
Could anyone shed some light, as to why the jQuery isn't working.. cz it works separately, just not on any element that is called from getBookings..
The problem is, you're binding an event handler with jQuery to bookingKeyDetails before it actually exists on the page. Remember that this element is being added to your html only after the page is loaded, so any actions you make in $(document).ready(function() { will not apply to it.
Bind the event handler only after you change the html:
function viewbookings()
{
if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
{
document.getElementById("bookingsummary").innerHTML = xmlHttp.responseText;
$('.bookingKeyDetails').click(function()
{
$(this).next('.hiddenDetails').slideToggle('medium');
});
}
};
Since the HTML element you need to toggle was never on the page when the calling page instantaiate the jQuery to toggle, you need to reinitialize the function after a successful call from your ajax.
You can use jQuery .success callback function for their AJAX call tools... I use that to help with this kind of processing instead of native AJAX. Makes life simpler and better cross browser support.
Just call your toggle function after the success.
http://api.jquery.com/jQuery.post/
It's very obvious that it will not toggle because you have writtent the Jquery in the "bookings.php". During the page load of bookings.php, it will not find any content of the getBookings.php so that Jquery will not apply as no element is available with that id.
You have to bind the event for the toogle on Ajax success event of the when you are calling for getBooking.php.
$.ajax({
url: URL,
success: function(){
$('.bookingKeyDetails').click(function()
{
$(this).next('.hiddenDetails').slideToggle('medium");
});
},
error: function(){
alert('failure');
}
});
You're using single and double quotes in your function call
$(this).next('.hiddenDetails').slideToggle('medium");
user 'medium' or "medium"

Dynamic Listview to Details page (Jquery Mobile)

i have succeeded in listing my data from mysql database
in JQM.
i want to link the list item to its details.
Therefore when there is a click on a list, a new page with the data from the list is displayed.
I dont know how to do it
Heres what i have now : but the link i have isnt working, guess i am missing something.
heres the link on js fiddle http://jsfiddle.net/8WU39/16/
<script type= text/javascript>
$('#seyzListPage').live('pageshow', function(){
$.ajax({
url: "data.php",
dataType: 'json',
success: function(json_results){
listItems = $('#seyzList').find('ul');
$.each(json_results.rows, function(key) {
html = '<li <h3><a href="index1.html?id=' + [json_results.rows[key].airp_id] +'"rel="external">'+json_results.rows[key].airport_code+'</h3>';
html += '<p><br> Aiport name: '+json_results.rows[key].airport_name+'</p></a></li>';
listItems.append(html);
});
// Need to refresh list after AJAX call
$('#seyzList ul').listview('refresh');
$.mobile.pageLoading(true);
}
});
});
</script>
<div data-role="page" id="seyzListPage">
<div data-role="header" id="header">
<h1>Airports</h1>
</div>
<div data-role="content" id="seyzList">
<ul data-role="listview" data-inset="true" data-filter="true"></ul>
</div>
<div data-role="footer" data-postion="fixed">
<h3>Footer</h3>
</div>
</div>
What do i do to get the list linked to its data on another html page.
I had a very similar problem and found a fix with a simple plugin. It's called jqm.page.params.js and can be found here. Implementation was very easy. Added the plugin file to my root directory and then included it at the bottom of my index.html page.
In your js file, you then just want to place
if ($.mobile.pageData && $.mobile.pageData.color){
var color = $.mobile.pageData.color;
}
at the top of your beforepageshow event to capture the variables tacked on to the link. Replace 'color' with the variables you are looking for.

Categories