Shashank Surwase
Shashank Surwase

Reputation: 63

html - send link with multiple parameter using share via whatsapp and sms

I am trying to send a message with a link to different products on my site using share via whatsapp and sms in mobile view. I am able to open both the apps and populate the text area.
The problem is that my link contains two parameters and everything from & till the end doesn't get populated in the text field for both sms and whatsapp.
This is my code -
HTML -

<div class="form-group mshare">
    <a id="wshare" class="btn btn-default btn-block" data-action="share/whatsapp/share">
        <i class="fa fa-whatsapp"></i> Whatsapp
    </a>
</div>
<div class="form-group mshare">
    <a id="sshare" class="btn btn-default btn-block">
        <i class="fa fa-envelope-o"></i> SMS
    </a>
</div>

Javascript -

$(document).ready(function(){
            $(document).on("click","#wshare",function(){
                var id = $('#shprodid').val();
                var name = $('#shprodnm').val();
                var text = "Hey! Check out this "+name+" on mysite - http://example.com/single.php?pid="+id+"&category=<?php echo $cat; ?>";
                alert(text);
                $(this).attr("href","whatsapp://send?text="+text);
            });
         });

        $(document).ready(function(){
            $(document).on("click","#sshare",function(){
                var id = $('#shprodid').val();
                var name = $('#shprodnm').val();
                var text = "Hey! Check out this "+name+" on mysite - http://example.com/single.php?category=<?php echo $cat; ?>&pid="+id;
                $(this).attr("href","sms:?body="+text);
                alert('clicked');
            });
         });

The desired output is -
Hey! Check out this watch on mysite - http://example.com/single.php?category=watches&pid=1234

The current output -
Hey! Check out this watch on mysite - http://example.com/single.php?category=watches

Upvotes: 0

Views: 4882

Answers (1)

Carlos Carvajal
Carlos Carvajal

Reputation: 76

you need encode "&" for this "%26" (http://www.w3schools.com/tags/ref_urlencode.asp) or whatever for your language UTF or others, but only for Twitter and WhatsApp, others understand "&":

var text = "Hey! Check out this "+name+" on mysite - http://example.com/single.php?pid="+id+"%26category=<?php echo $cat; ?>%26utm_campaign=Mobile";

Upvotes: 3

Related Questions