Loading

Author Topic: Send a friend help needed  (Read 481 times)

Benjis

  • Newbie
  • *
  • Posts: 11
Send a friend help needed
« on: September 11, 2011, 09:23:16 am »
Hello again :)
So hear i am with other question i ll try to explain :)
There is a send a friend form then u sending if sauces its redirecting u to item page is there any way that wont be redirection but u would stay on the same send a friend form page? and i would like to do that without changing core files but just template files adding some function or something  like that.
just to have the page and then u sending from form it sends in background and u have mesag that sent on the same page
I hope i explained understandable :)

garciademarina

  • Administrator
  • Sr. Member
  • *****
  • Posts: 403
    • OSClass
    • Email
Re: Send a friend help needed
« Reply #1 on: September 12, 2011, 03:15:45 pm »
Hi,

You can send the form via ajax.

Add to form this:

<form id="sendfriend" ...  onsubmit="withoutRedirect();return false;">


Just after form
Code: [Select]
<form ...>
</form>

<script type="text/javascript">
function withoutRedirect(){
    $.ajax({
        url: "<?php echo osc_base_url(true);?>",
        data: $("#sendfriend").serialize(),
        context: document.body,
        success: function(res){
            var success = '<?php echo sprintf(_m('We just send your message to %s'), "" ); ?>';
            success = success.replace(/^\s*|\s*$/g,"");
            var RegularExpression  =  new RegExp(success);
                                   
            if( RegularExpression.test(res)){
                alert('send ok');
            }else{
                alert('NO send email');
            }
        },
        error: function(){
        }
    });
}
</script>

Regards
Carlos García de Marina
carlos@osclass.org
Twitter: @osclass
http://www.osclass.org/

OSClass - Open Source Classifieds

Benjis

  • Newbie
  • *
  • Posts: 11
Re: Send a friend help needed
« Reply #2 on: September 12, 2011, 07:13:30 pm »
Thanks man u are the Star Gracias works perfect
just small thing after submitted the fields are not cleared i have clear button now but is there any way to do automatically :) ;)
« Last Edit: September 12, 2011, 07:16:11 pm by Benjis »

garciademarina

  • Administrator
  • Sr. Member
  • *****
  • Posts: 403
    • OSClass
    • Email
Re: Send a friend help needed
« Reply #3 on: September 12, 2011, 07:50:39 pm »
Hi,
Try adding this in success:

Code: [Select]
$('input').val('');
$('textarea').val('');

Regards
Carlos García de Marina
carlos@osclass.org
Twitter: @osclass
http://www.osclass.org/

OSClass - Open Source Classifieds

Benjis

  • Newbie
  • *
  • Posts: 11
Re: Send a friend help needed
« Reply #4 on: September 12, 2011, 09:11:32 pm »
Eureka U are The Best :) Thank you.

cfelino

  • Newbie
  • *
  • Posts: 1
    • Email
Re: Send a friend help needed
« Reply #5 on: February 22, 2012, 10:36:25 am »
Hi, for everyone struggling with this here is an alternate solution I used in "contact form" but should also apply to "send friend":

1. Modify form header to this:
Code: [Select]
<form id="contact_form" name="contact_form">
2. Change the submit button to this:
Code: [Select]
<button id="send_btn" type="button" onclick="submitform();"><?php _e('Send''[yourtheme]') ; ?></button>
3. Add this script
Code: [Select]
function submitform(){
    if(document.contact_form.onsubmit()){ //the condition is needed to perform my custom validations, can be ignored.
        noredirect();
    }
}

function noredirect(){
    $.ajax({
        url: "<?php echo osc_base_url(true);?>",
        data: $("#contact_form").serialize(),
        context: document.body,
        success: function(res){
            alert ( $(res).filter("#FlashMessage").html() ); // here we capture the response (FlashMessage div) and display it.
            // your custom code to handle success/error messages
        },
        error: function(){
            //TODO
        }
    });
}


This way you can take advantage of the many available response messages for the "contact_form", for example when recaptcha is wrong.

Regards.