Reputation: 509
I am trying to send a JS a variable to a php file. It is not happening. And there are many questions answered out there on similar situation. I probably am making some similar mistake. Though, I am not able make out what it is.
The Codes are as follows;
main.php
<script type="text/javascript"> var socialurl= "<?php echo $url;?>";</script>
JS
jQuery(function(){
alert('socialurl');
$("#smshare").click(function(){
$("#smp").slideToggle("fast");
$.post("social-media-share.php", {socialurl: socialurl });
$("#smp").load("social-media-share.php");
});
});
social-media-share.php;
<?php
$socialurl=$_POST['socialurl']
echo $socialurl."<br>";
include('conn.php');
$socialurl = $conn->real_escape_string(trim($_POST['socialurl']));
?>
<a href="whatsapp://send?text=<?php echo $socialurl;?>" data-text="<?php echo $socialurl;?>" data-href="" class="wa_btn wa_btn_s"><img src='images/share.png' width="30" height="30" alt='WhatsApp Share'></a>
The file social-media-share.php is called into the main.php with JS. Prior to which the JS variable socialurl is defined in the main.php
The variable value pops up in alert in the JS but it is not getting posted to social-media-share.php
I am sure I must be making some silly mistake. But for the life of me am not able to find out what.
Any help will be greatly appreciated.
Upvotes: 0
Views: 70
Reputation: 9648
I think the problem is you're posting to the page, but then loading it again via a GET request and so you're not loading the results of the post request itself.So unless the GET on social-media-share.php is displaying a database result I'm not sure you're actually loading what you want.
jQuery(function(){
alert('socialurl');
$("#smshare").click(function(){
$("#smp").slideToggle("fast");
$.post("social-media-share.php", {socialurl: socialurl });
$("#smp").load("social-media-share.php");
});
});
This bottom line is doing the wrong thing. $("#smp").load("social-media-share.php");
you actually want a callback function in you .post that loads the response from that
http://api.jquery.com/jQuery.post/
$.post( "social-media-share.php", {socialurl: socialurl })
.done(function( data ) {
//you might need to do some other stuff to data here
$("#smp").html(data);
});
Alternatively you might have an error in your social-media-share.php script so you could try doing a var_dump($_POST);
to see what variables are actually being posted
Upvotes: 1