How to Pass Jquery Variables to PHP Variable

Pass a php variable to a jQuery function

Suppose your $variable has value "Hello".

Then this code:

echo 'wholesection('.$variable.');',

is rendrered in html like

wholesection(Hello);

See? You're passing Hello to a function. Not "Hello" but Hello.
And Hello is considered a javascript variable. I bet you don't have it.

So, the fix is - add quotes:

echo 'wholesection("'.$variable.'");',

which will be rendered as:

wholesection("Hello");

Pass a jQuery variable into a PHP variable

It's not possible in the way you're trying to do it. Javascript (and thus JQuery) work on the client machine, after the page was sent and the connection with server closed. To get data from client machine your best bet would be an AJAX call which opens a new connection to the server and allows you to load (and send) additional data between the client and the server.

Whole AJAX tutorial is out of scope here, but maybe this link will help you.

If you only want the current year though, you can skip the javascript and do it in PHP, like so:

$purchaseYear = date("Y");

How to pass jQuery variables to PHP variable?

Ajax can do this. Google it, and check out api.jquery.com and look at the ajax functions, .ajax(), .post(), .get(), .load(), etc.

As for your specific question, here is what you would do:

//Javascript file
$("input[type=checkbox]").click(function () {
$.post('my_ajax_receiver.php', 'val=' + $(this).val(), function (response) {
alert(response);
});
});

//PHP file my_ajax_receiver.php
<?php
$value = $_POST['val'];
echo "I got your value! $value";
?>

Passing Jquery variable to php url

The variable you are sending via AJAX isn't a variable but the string 'title'. Instead use + to add the variable in the URL.

$('.dislike_box a').click(function(event){
event.preventDefault();
var title = $(this).data('title');
alert(title);
$.ajax({
url: 'vote.php?vote=dislike&title='+title,
success: function(result){
$('.dislike_box p.vote_text').text('Dont Like it.');
}});
});

To retrieve the js variable in PHP file :

<?php

if(isset($_REQUEST['title'])) {
$title = $_REQUEST['title'];
}

How to pass a PHP variable to a .JS file using JQUERY?

Echo it to HTML temaplate. If it's some structure/array use for example json format.

<script>
<?php $variable = $fetch['username']; ?>
var js_variable = <?php echo json_encode($variable); ?>
</script>

or
echo some hidden element like

<input type="hidden" id="custId" name="custId" value="<?php echo($fetch['username']); ?>">

and grab if with Jquery like

<script>
$( "#custId" ).value();
</script>

with your code
JS:

$(document).ready(function(){ var flwbtn = $(".findrowpplfollowbtn");

flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();

$.post("../php/flw.php",{$( "#custId" ).value()})

});
});

How to pass php variable's value to jquery

It really depends if you are using some sort of a template engine.

  1. If you're using plain PHP, the only option for you is to echo the variable:

    var current page = "<?php echo $your_var; ?>";
  2. Twig engine:

    var current page = "{{ your_var }}";
  3. Smarty and RainTPL engines:

    var current page = "{$your_var}";

As you can see, there are other ways. All of them work fine. It really depends on how you'd like to write and organize your code. I personally use Twig and find it really easy,fast and straightforward.

Also, as others have pointed out, you can do AJAX calls to the server and fetch the variables like that. I find that method time-consuming, inefficient and insecure. If you choose this method, you will be posting requests to a script. Everybody will be able to do post/get requests to that script which opens your doors to some bots and DoS/DDoS attacks.



Related Topics



Leave a reply



Submit