How to use window.location.href after a post submission? - syntax

I have tried window.location.href without doing any submission it works
window.location.href="http://www.google.com";
But how about if I have to do a post submission, then if the data is valid, I would like to perform a redirect using window.location.href Can somebody explain to me how this can be done? Thanks!

If you're using PHP you can simply use a header.
<?php header("Location: http://google.com"); ?>
As for doing the redirect with just JavaScript...
<script> window.location = 'http://google.com'; </script>

Related

Magento Autmatically redirect to a template file from another template file

I have a custom template file. There i am displaying a message. So after 10 second from the page load, i need to redirect to the admin login page automatically. I tried $this->_redirect('');. But that did not work.
Can anyone help me please. Thank You.
<script type="text/javascript">
setTimeout(function(){
window.location='<?php echo Mage::getBaseUrl().(string)Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName')?>';
}, 10000);
</script>
Put this script in your file it will works for you
The snippet below will resolve your question. If your site uses jquery, you may just paste this in your template. Otherwise, you need to include jquery in your Magento header.
<script>
jQuery(document).ready(function(){
setTimeout(function() { document.location = '<?php echo Mage::getUrl('adminhtml'); ?>'; }, 10000);
});
</script>

I need some help about ajax in wordpress always return 0

This is the first time I try ajax in wordpress. I need some help.
in my theme's functions.php, I have:
function returnRandomPosts(){
echo '123';
die();
}
add_action('wp_ajax_returnRandomPosts', 'returnRandomPosts');
add_action('wp_ajax_nopriv_returnRandomPosts', 'returnRandomPosts');
and in my single.php file, I have
<script type="text/javascript">
jQuery(document).ready(function($){
$.post("<?php echo admin_url("admin-ajax.php"); ?>", {"action": "returnRandomPosts"}, function(response) {
alert('response: ' + response);
});
});
</script>
When I run the web page, it always alert "response: 0",
I hope you can give me some hints or tips, thank you.
update:
I checked the has_filter function,
the new wp_ajax_XXXXX has successfully add into $wp-filters,
BUT when I call AJAX into wp-admin/admin-ajax.php, has_filter function return false with wp_ajax_XXXXX
Anyone can give me some hint?
Finally, I solved the problem.
I have a plugin, the function is, to preview another theme by adding "theme=name_of_theme" argument to url.
( For example, preview my homepage with "xxx.xx.xx/?theme=name_of_theme" )
The ajax failed in preview.
While I add the same ajax function code to the online page (ie. activated theme), it successes.
I hope my experience may help somebody.

Wordpress FrontEnd Ajax return 0 using Theme

Hi I know this has been asked a few times but I have been through every answer and tried them all with no luck.
I am trying to use Ajax in a Wordpress front end page, I have security in the page to ensure the user is logged in before they can view this page too.
No matter what code i enter my ajax call always returns 0.
function ajaxfoodlookup()
{
echo "ajax fired";
die();
}
add_action('wp_ajax_nopriv_ajaxfoodlookup','ajaxfoodlookup');
add_action('wp_ajax_ajaxfoodlookup','ajaxfoodlookup');
This is what I have in my functions.php (i have also tried exit(); and die($results) where $results = 'ajax fired'; nothing seems to work.
This is what I have in the page to call the ajax;
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
data: { action:'ajaxfoodlookup'},
success: function (data) { alert(data);}
});
The only thing that I have different to the other questions/answers on here is that I am using a bought Theme which does have some code added, is that theme able to hijack my ajax calls? I thought wordpress would execute the ajax call depending on the 'action' in the data supplied?
Please help its driving me crazy?
Thanks
Official WordPress recommendation is to use the ajaxurl variable instead of hard coded one. Insert this before your JS code and change the url of your jQuery.ajax to use ajaxurl.
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
You can also use wp_localize_script to define the ajaxurl

wordpress create user after form submit with ajax call

i am submitting on form in my wordpress template which goes to remote website now i want to create user with that from fields.but if the form submitted normally then user will not created and form will be redirect to remote url so i use ajax call for this,how ever the user is created but after success function it shows 404 file not found and form does not get submitted.here are the codes first one is ajax request from form fields
<script language ="javascript" type = "text/javascript" >
$na= jQuery.noConflict();
$na(document).ready(function(){
$na('#infuse').click(function(){
alert('start');
var name=$na('#inf_field_FirstName').val();
var password=$na('#inf_field_Password').val();
var email = $na('#inf_field_Email').val();
$na.ajax({
type: "POST",
url: '<?php bloginfo('template_url')?>/user_create.php',
data: 'name='+name+'&email='+email+'&password='+password,
cache: false,
success: function(){
jQuery("#inform").submit();
}
});
});
});
</script>
here is code in user.php file
<?php require('./../../../wp-blog-header.php'); ?>
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$password=$_REQUEST['password'];
wp_create_user( $name, $password, $email );
?>
how ever user created in admin that means ajax works but after that it shows 404 error in console and the form submit process stops
use
<?php require('./../../../wp-load.php'); ?>
in place of
<?php require('./../../../wp-blog-header.php'); ?>
Use:
<?php require('../../../wp-load.php'); ?>
Instead of:
<?php require('./../../../wp-blog-header.php'); ?>

Magento redirect after logout

how can I redirect the customers after logout to default store view in magento?
In logout I redirect them another store view.
I know it's not elegant, but the easiest method I have found is to copy and modify the template file at app/design/frontend/base/default/template/customer/logout.phtml to your own theme directory.
Specifically this line:
<p><?php echo Mage::helper('customer')->__('You have logged out and will be redirected to our homepage in 5 seconds.') ?></p>
<script type="text/javascript">
//<![CDATA[
setTimeout(function(){ location.href = '<?php echo $this->getUrl() ?>'},5000);
//]]>
</script>
By modifying location.href url and even the timeout you can point the user to anywhere just after logout. E.g.:
<script type="text/javascript">
//<![CDATA[
setTimeout(function(){ location.href = '<?php echo $this->getUrl('*/*/login') ?>'},500);
//]]>
</script>
Again, it's not elegant, but it should be a quick enough redirect that the quick hop on the page will then shove them to another url, in the above example, back to the login screen.
Unfortunately there's not a convenient event hook to manipulate the logout redirect location.
Mage_Customer_AccountController::logoutAction() sets a redirect to ::logoutSuccessAction() on the response object after the customer_logout event is dispatched, and it's the rendering of the customer/logout.phtml template which uses PHP to set echo a javascript param to redirect to the homepage with no OOB possibility to pass an arg for an alternate JS-based redirect.
I think the cleanest solution would be to observe controller_action_postdispatch_customer_account_logout, grab the controller object, and overwrite the location header using the response object's setRedirectWithCookieCheck() method:
public function logoutRedirect($obs)
{
$obs->getControllerAction()
->setRedirectWithCookieCheck(/* your URL param(s) */);
}
Write the following method in Your Model > Observer .
public function customerLoggedOut(Varien_Event_Observer $observer)
{
$observer->getControllerAction()
->setRedirectWithCookieCheck(CustomUrl);
}
Customurl is a url on which you want to redirect after Logged Out.
If you want complete solution for custom url redirection for your ecommerce website after logged In, Logged Out and Registration. Custom Redirection extension can help you. Click on link to get extension. http://www.magentocommerce.com/magento-connect/custom-redirection.html

Resources