Is Wordpress hijacking my Ajax form post? - javascript

I'm new to Wordpress and new to JQuery, so let me start off explaining what I am trying to do.
I have an admin page, inside this page I'm giving the user the ability to upload an image. I want this done using Ajax (independent from the general form update).
Here is the code I have so far:
At top of page - script includes:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js">
I can confirm these scripts are "pingable" and work.
Now the HTML code :
<table width="100%">
<tr>
<td width="100" style="padding:10px" valign="top">Email Image (180x180):</td>
<td style="padding:10px"><img id="previewEmailImage" width=180 height=180>
</td>
</tr>
<tr>
<td></td>
<td>
<div id='emailpreviewloader'>
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<form id="imageform" method="post" enctype="multipart/form-data" action="/ajaximage.php">
<input type="file" name="photoimg" id="photoimg" />
</form>
</td>
</tr>
</table>
The key things in the HTML is a) a form and b) The div emailpreviewloader.
Now just after the html table, inline I have the following js:
<script type="text/javascript">
$(document).ready(function()
{
$('#photoimg').live('change', function()
{
$("#emailpreviewloader").html('');
$("#emailpreviewloader").html('<img width="180" src="/loader.gif" alt="Uploading...."/>');
$("#imageform").ajaxForm(function(result)
{
alert("Thank you for your comment!");
});
});
});
</script>
for testing purposes ajaximage.php just contains 1 line: Echo "It worked";
So assuming I've done my job right, and the html + js above is correct, it would seem Wordpress might be hijacking the Ajax somehow and preventing it from working as expected. Is this possible?
All I want to do is have a regular Ajax post, how is this possible?
EDIT:
What is working:
The change event for the file upload control is firing. I've confirmed this with an alert, and the loader.gif is visible. But it would seem the form isn't firing, or not firing correctly. The inner alert, never fires.

If you don't want to use wordpress's functions for ajax calls, try sending the form to action="<?php bloginfo('template_url');?>/ajaximage.php" and make sure the file is in the root of your wordpress instalation.

Related

Progress bars while jsp page is loading

I've a jsp page which internally calls few java classes before it renders the content on the browser. The whole process is taking 1-2 minutes, so I want to show some progress bars or animations (preferably progress bar). Have gone through few posts in Stackoverflow and couldn't get much.
Have tried pace.js and few javascript and jquery techniques but couldn't get the desired effect.
It would be great if someone here help me with some ready made code snippets or the links or some tricks which help me in solving this problem.
Note: I've not used any servlet, completely used scriptlets .
Thanks in advance.
The easiest way to add a simple loading item to the page is with a gif image. I would use jQuery to show the gif and when the content is ready hide it.
I found this snippet, it could be helpful: http://jsfiddle.net/msjaiswal/FAVN5/
.loadinggif {
background:url('http://www.hsi.com.hk/HSI-Net/pages/images/en/share/ajax-loader.gif') no-repeat right center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='inputsource' />
<br>
<button onclick="$('#inputsource').addClass('loadinggif');">Show Loading</button>
<br>
<button onclick="$('#inputsource').removeClass('loadinggif');">Hide Loading</button>
I've got one on my JSP.
<form enctype="multipart/form-data" name=uploadSapFile action="sapFileUploader.jsp" method="POST" onsubmit="block();">
<table border=0 width="50%">
<tr>
<td colspan="2" align="center"><input id="process" type="submit" value="PROCESS"></td>
</tr>
</table>
<img id="ajax-loader" src="../../../images/ajax-loader.gif" style="display: none;" />
<script type="text/javascript">
function block() {
// This will add a "processing" animation while the system is busy with the file and also makes a "PROCESS" button deselected
document.getElementById('ajax-loader').style.display='block';
}
</script>
</form>
U are interested in the first line where onsubmit form action is performed, img tag and javascript function and process button.
And big choice of nice animations here

Submit form to email, then redirect to new page (without php)

The client has a section of her site that is not navigable through any other pages. To get to these pages she wants the user to fill out this form. It's a non-for-profit, so she basically just wants to see how many people are getting to these pages that have a product available for free so she can report on that. So there is a page with a form. She wants the user to submit the form and then be re-directed to another page. Normally I would do this really easily with PHP but unfortunately she hosts the site through some local fools and they don't support PHP
So the question is, how do I get this form to submit to her email, then redirect to a different page (videos.html) from the current page with the form (video-form.html)?
I have this form:
<form id="myForm" action="mailto:mail#mail.net" method="post" name="VideoFormAction">
<table cellspacing="1" cellpadding="1" width="46%">
<tr>
<td valign="top" class="shade1"><font color="red"><b>*</b></font> Email Address</td>
<td valign="top"><input type="text" name="Mail" value="" size="30"></td>
</tr>
<tr>
<td valign="top" class="shade2">Name</td>
<td valign="top"><input value="" name="Name" type="text" size="30" maxlength="100"></td>
</tr>
<tr>
<td valign="top" align="center">
<font color="red"><b>*</b></font> = required field
</td>
<td valign="top">
<button class="button">Submit</button>
</td>
</tr>
</table>
</form>
I have tried some javascript:
<script>
document.getElementById("myForm").onsubmit = function() {
setTimeout(function(){
window.location.href = "http://www.funducate.net/videos.html";
}, 1);
};
</script>
Which for whatever reason will not work. I have found multiple suggestions through search to try many different variations of the above, all of which failed. The last ditch effort was even putting a timeout into it as you see above. I imagine it doesn't work because the submit button performs the action="" and then doesn't continue.
Are there any other alternatives? Remember, I can't use PHP (i know it's ridiculous but it's what I got)
You could read the form values via either jQuery or javascript, save them to variables, construct a message body and email the form contents.
See below for more information:
How to send an email from JavaScript
More information on Mandrill
After sending the email, redirect the page as you have done (correctly) in your code -- but without the setTimeout.

Submitting form via jQuery (Javascript) failing

What am I missing here? If I try to submit the form via javascript it doesn't work.
Error (submitting with a regular JS reference and jQuery reference returns the same error)
SCRIPT3: Member not found.
Code:
Refresh customer data
<script type="text/javascript">
function refresh_customer_data()
{
$("#post-form").attr("action", "../scripts/refresh-customer-data.asp");
$("#post-form").submit();
}
</script>
<form method="post" action="" id="post-form">
<input type="hidden" name="fromsubmit" value="true" />
<table class="form" style="height:50px;">
<tfoot>
<tr>
<td><span class="required">*</span> Accessible by administrators only</td>
<td><input type="submit" name="submit" value="" style="display:none;" /></td>
</tr>
</tfoot>
</table>
</form>
Thanks!
Why not use jQuery post to send your data to server page like this
Refresh customer data
Javascript:
$(function(){
$("#aRefresh").click(function(e){
e.preventDefault();
$.post("../scripts/refresh-customer-data.asp", $("#post-form").serialize(),function(data){
//do whatever with the response from server page
})
});
});
Instead of inline calling the function, why not use jQuery to do it for you:
http://jsfiddle.net/8XdwS/
I know this is an old thread but for everybody's information:
I have come across this issue as well, and the problem was having the submit button named submit.
Renaming the submit button ie. to submit2 solved the issue.

Hide login <form> using Javascript

I'm having problems with getting a login box I made to hide. Here is the HTML code I use to make the login box:
<center>
<form name=login>
<table width=225 border=1 cellpadding=3>
<tr><td colspan=2><center><font size="+2"><b>Login</b></font></center></td></tr>
<tr><td>Username:</td><td><input type=text name=username></td></tr>
<tr><td>Password:</td><td><input type=password name=password></td></tr>
<tr><td colspan=2 align=center>
<button type="button" onclick="loginNow()">Login</button>
</td></tr>
</table>
</form>
</center>
When the "Login" button is clicked, it runs some Javascript code. The loginNow() function runs, which verifies the password. Once the password is verified, I need this form to hide.
Can anyone give me some code that will work in this situation?
Wrap your form in a div with an id:
<div id="loginform">
<form>...</form>
</div>
Then, in the loginNow() function, use document.getElementById('loginform').style.visibility = 'hidden'
Add an id to the center tag like : center id="login_box"
And write this script inside the success block of the loginNow() code
$('#login_box').hide('fast');
Or if you cannot add id to the center tag then add this script inside the success block of the loginNow()code
$('center').hide('fast');
But it will hide all the center tags present in the code.

Jquery ajaxForm() doesn't fire 'success' although it works successfully

Today I was trying to send files via Ajax request using the JQuery AjaxForm plugin. However, it doesn't fire the success function as it uploads the file without any javascript run time or compile time error.
Here is my code:
<table id="haberresmiekle">
<form action="action.asp?kmt=resimyukle" method="post" enctype="multipart/form-data" id="haberresmiekleform">
<tr>
<td><input type="file" name="file"></td>
</tr>
<tr>
<td><input id="b" type="submit" value="Gonder"></td>
</tr>
</form>
</table>
and
<script>
$("#haberresmiekle").dialog({autoOpen:false,title:"Resim Yukle"});
var options={
target:"#haberresmiekle",
success:function(){alert('x');$("#haberresimekle").dialog('close');}
};
$("#haberresmiekleform").ajaxForm(options);
$("#b").button();
</script>
Thanks for any help
Note: It has been resolved.
Yor script will do nothing(except in IE) as long as you dont use valid selectors.
$(haberresmiekleform)-> $('#haberresmiekleform')
$(haberresmiekle) -> $('#haberresmiekle')
$(button) -> $('#button')

Categories

Resources