AJAX, SEO and PHP folder trouble - php

I'm creating an AJAX script that reads a specific file, which name is identified with a GET var. The beginning of the script in index.php is:
var expFile = '<?php echo $_GET['text_name']; ?>';
$(document).ready(function () {
$.ajax({
url: expFile+'.xml',
type: 'get',
dataType: 'xml',
success: function (data, textStatus) {
// Parses the content, which is escaped into a "text" tag in the xml, and puts it
//into an html div with a "content" class"
var content = decodeURIComponent($(data).find('text').text());
$('.content').html(content);
}
});
});
And everything went ok. The file is read and the thing is shown correctly.
The XML file, that is in the same folder of the index.php file, is read directly from AJAX.
Now i'm using a mod_rewrite in order to make the URL SEO-Friendly.
When i type the dirty URL (http://www.mysite.com/index.php?text_name=name-of-the-file-to-read) it's OK.
But when i type the rewritten url (which is http://www.mysite.com/lyrics/name-of-the-file-to-read) the content is not shown.
I know that AJAX is client-side, while mod_rewrite is server-side, but I don't know how to reach a parent folder (that really doesn't exist) from the "url" parameter of the $.ajax or an absolute link like url: 'http://...' (but it goes against Same Origin Policy).
Help me please!!!

Francesco's answer, initially put into the question:
SOLVED.
THE PROBLEM was with the script tag. I had to put a ../ to the jquery href
<script type="text/javascript" src="../libraries/jquery.min.js"></script>
Then I added ../ to the URL and everything went fine.

The URL is having .xml appended by the function itself - on this line:
url: expFile+'.xml',
Is that still there when you are trying with the SEO friendly URL? If so, I'd remove that appended .xml and try again.

Related

Calling a function in php with link in html

I'm trying to call a function with a link in html. I found the following example:
click to run function!
if(isset($_POST['runfunction'])){
}
This works perfectly fine, the problem is that when I click the link, "?runfunction" keeps standing in my url bar. So when I submit a form on my page it goes totally wrong (it's way to long to upload here). I do some SQL queries and I'm getting weird values in my SQL database. When I type in just my normal url it works fine. So I'm pretty sure that's the problem. I found another example with ajax :
$("a").click(function(){
jQuery.ajax({
url: "path/to/controller",
type: "POST",
dataType: 'json',
data: {'mentod':'ExportExcel'},
success: successCallback,
error:failureCallback
});
});
I don't fully understand this example (because I never use AJAX) but my php script is included in the html page "include("")". So I can't type in url because it has to be the same page. Can someone give a little bit of info about this, or give an example of how I can fix this? Thanks in advance!
You can add a callback method then remove it from the url by javascript
function successCallback () {
url = window.location.href;
window.location = url.replace("runfunction", "");
}

How to post jquery to current file

Below is a fragment of code i'm working on, saved in index.php:
$("#login").click(function(){
username=$("#name").val();
password=$("#word").val();
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
For some reason I want the destination url to be the same as the current file(say index.php)and not login.php, how do I do this? And if i place the php script in index.php, it just runs even when the form has not been submitted, if I place a condition like if(isset($_POST['name'])) on it, it doesn't work again with jquery. However, I'll like a fallback when js is disabled in
the browser.
Try this
url : location.href
we can set url to current page

Why do I get a "Post" 404 error when submitting an ajax contact form in WP

I'm in the process of building an ajax contact-form for a WordPress theme and am running into an issue when submitting the form; the console keeps saying it can't retrieve my mail-form.php file (used to format the email) when I'm pretty sure I've got the url right.
Here's the code that I'm using:
jQuery.ajax({
type: "POST",
url: "<?php echo get_template_directory_uri(); ?>/mail-form.php",
cache: false,
data: "name=" + name + "&email=" + email + "&message=" + message,
success: function (html) {
jQuery("#contact-form").slideUp("slow");
jQuery("#contact-form").after("<p><span class='center' id='send-message'>Your message has been sent! We will reply shortly!</span></p>");
jQuery("#send-message").fadeIn("slow");
}
});
Here is a link to the form... http://wordpress-dev.designer17.com/contact/
The last time I used this it worked just fine, so I'm quite bewildered by this.
Because you're using PHP within your .js file (line 56):
url: "<?php echo get_template_directory_uri(); ?>/mail-form.php",
What you can do is add this to your main template:
var TEMPLATE_URI = "<?php echo get_template_directory_uri(); ?>";
then within your .js file use:
url: TEMPLATE_URI + "/mail-form.php",
Your JavaScript, $.ajax function call in this case, is client side and is not parsed by php, which is server side. Because of this the JavaScript parser interprets the string "" as a literal string and it is not parsed. To solve this simple use a relative url or generate the JavaScript in a php template which can parse the php code.
Remember: Code is always parsed/rendered serverside, sent back to the client's browser, then client side scripting, JavaScript code in your case, executes in the browser.
I supose you moved your javascript onto a .js file, however unless you instruct your http server to process javascript files as php any php inside them is just text.
Either move the function back into a php file or (better approach) pass the URL as an attribute, for example:
// PHP FILE
<form action="URLGOESHERE" id="form_id">
...
// JAVASCRIPT FILE
var url = $("#form_id").attr("action");
jQuery.ajax({
type: "POST",
url: url,
...

jQuery getJSON url

I am setting up a twitter feed into my site and have it working perfectly when you access it with a root page ie www.domain.com/index.php
However when you have a url that is www.domain.com/category/page it doesn't not work, however if you use www.domain.com/index.php?cat=category&page=page it works fine.
So in the javascript I have the following
jQuery(function() {
jQuery.getJSON('cache/twitter-json.txt', function(data){
jQuery.each(data, function(index, item){
jQuery('#tweet').append('code for displaying tweet');
});
});
So I have tried to change the url in the getJSON function to the full path www.domain.com/cache/twitter-json.txt but that doesn't work.
Try:
jQuery.getJSON('/cache/twitter-json.txt', function(data){
Relative URLs are interpreted relative to the directory of the page containing the reference. So when you make the above call from www.domain.com/category/page, it tries to go to www.domain.com/category/cache/twitter-json.txt.
If you want to use the full URL, you need to put // before the hostname, e.g.
jQuery.getJSON('//www.domain.com/cache/twitter-json.txt', function(data){
Otherwise, it thinks www.domain.com/ is just another level of directory.
change
jQuery.getJSON('cache/twitter-json.txt', function(data){
to
jQuery.getJSON('/cache/twitter-json.txt', function(data){
Maybe it helps to set the base in your html
<base href="http://myserver/dir1/">

CodeIgniter, PHP, jQuery and AJAX

I am wondering about how people go about using CodeIgniter and jQuery for AJAX.
In your AJAX request you have
{
url : ???,
data : {a:1,b:2},
success: ....
}
So, how do you build the URL?
Do you
have all your JavaScript in your view files, and just use site_url() to build the URL
have all your JavaScript in external js files, have a header view you include that has something like <script>var base_url = '<?php echo site_url(); ?>';</script>. Then in your external js files have url: base_url+'rest/of/path/';
some other method?
I have my all my js in an external file and load it in my template.
For specific ajax requests, just call the page as you normally would.
$.ajax({
type: 'POST',
url: '/ajax/login',
data: blabla,
success: function(data) {
// do something
},
dataType: 'json');
});
In answer to your question, I've had no need to specify the base url, as putting '/' before the controller name sets the root of the site automatically. You could also use ../ etc
if you are writing your ajax in external file then you can define your base url in the view file like
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
Then on your external ajax file write
url : base_url+'controllerName/functionName',
It can also be done by loading a view that contains your JavaScript.
I currently load JavaScript from a view at the end of the rendered page. Since it is a PHP file with html <script> in it, you can use the URL helper functions like site_url() to generate the URLs you need for each function.
An example view might contain:
<script>
$.ajax{
url : "<?=site_url("controller/function")?>",
data : {a:1,b:2},
}
</script>
That will get you CodeIgniter generated URLs for your JavaScript. You could even pass variables into the view for more control over your js.
Usually I make a small javascript in my header, in which I create a base_url and site_url variable (usually being properties of an object I name CI, but that's a personal preeference). I fill these values by echoing the values with PHP. If you make that the first loaded script, you'll always have the site_url available in JS.
Since I'm on mobile I can't post the source now.

Categories