How to post jquery to current file - php

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

Related

jQuery AJAX Onchange Redirect Issue

I have my bit of jQuery AJAX code, to send a file to a PHP script, update/insert to db and continue to edit by ID as redirect. Works fine.
Problem is.. every other page except homepage (no rewrites, base url) the script will break the onchange event listener, and skip upload then continue to editing a broken upload.
Every other page, this code works perfect and does as intended. But homepage, it does not listen. I can not figure it out. Only recently we got the last insert ID working for redirect to edit entry.
$(document).ready(function(a) {
$("#form-fileUpload").on("change", function(a) {
var file_data = $('#uploadFile').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
alert(form_data);
$.ajax({
url: '/ajax_file_upload.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
if (data.redirect == true) {
window.location.href = '/file-edit.php?id=<?php echo mysql_insert_id(); ?>';
}
//alert(php_script_response);
}
});
});
});
How can I prevent this from ignoring the onchange submit action for our form? Is this a root domain thing? Didn't think the PHP code was relevant, it works fine as intended.
So user browses for file, and form automatically uploads and redirect happens sending them to edit and add more details in a form. Perfect on all pages but homepage. Is the JS at fault? Is there handling for this in this situation away from sub pages?
The PHP code that plays this role (AJAX script):
$id = $conn->lastInsertId();
// Redirect & complete details
header("Location: https://www.website.com/editfile?id=".$id.");
You should use full url to direct in jQuery like below
window.location.href = 'https://www.website.com/file-edit.php?id=<?php echo mysql_insert_id(); ?>';

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,
...

AJAX, SEO and PHP folder trouble

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.

Send "iframe.contents()" to PHP Script through Ajax any ideas ?

In my code I have an iFrame which loads dynamic content it's like a webpage(B.html) inside a page(A.php). in "A.php" user can edit inline the "B.html" once the process of editing has completed. In my submission I am sending iframes information to another page (script.php). I tried everything but content is not comming up in "script.php".
In nutshell, I want to tranfer my big html text with all stuff to a PHP via AJAX. I have no idea how to do it... my code would be something like below :-
Code for "A.php" inscript :
"myframe" is the iframe which contains the big chunk of HTML.
sendString = $("#myframe").contents();//Tried everything here[JSON as well]
$.ajax({
url: "script.php",
type: "POST",
data: sendingString,
cache: false,
success: function (html) {
return html;
}
});
Any help would be appreciated.
Regards,
Amjad
$("#myframe").contents() will get you it's nodes as a jQuery object. Try $("#myframe").html() instead to get the contents as a string.
EDIT: Oh, and it also helps if you fix your variable names. Change data: sendingString to data: sendString.

jQuery ajax post dynamic url insertion

$.ajax({ type: "POST", url: "OMFG.php", data: info, success: function(){ }});
is what I'm using atm as a test and it works fine.
I need to get the url from the link I'm clicking, so I do:
var url = $(this).attr("href");
which works fine if I alert it out(the link includes http://samedomain.com/etc.php), but the ajax function doesn't post if I insert it into the ajax code:
$.ajax({ type: "POST", url: url, data: info, success: function(){ }});
Please help, as I'm screwed without this working.
You are using the id attribute. For getting the link you need to fetch the href attribute:
var url = $(this).attr('href');
EDIT: See you just changed to href - if that still does not work try debugging by using
alert($(this).attr('href'));
and see if it alerts an url and if it is correct.
EDIT 2: Well, check in firebug in FF or Javascript Console in Chrome for errors or what url the script really is trying to post to. Depending on the rest of the code your url-variable might be out of scope if it is defined within some function or something.

Categories