Set php variable in jquery

In same page i set the jquery value while click the button. i want pass the value to php variable in same page without form submitting.

<input type="button" name="next"  class="next btn btn-primary" value="Proceed To Checkout Page" />

Jquery

$(".next").click(function(){
       <?php $var1="1";?>
 }

While check the php value

<?php if(isset($var1)){
     echo $var1;
 }else{
    echo "NULL";
   }?>

Every time time i got the null value. Where i getting mistake. Ps: I cant able to send the ajax call to get the value

asked Mar 16, 2016 at 4:13

1

You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.

You can use cookies to achieve this.

In Javascript:

<script type="text/javascript">
    document.cookie = "var1=1";
</script>

And in PHP

<?php 
   $phpVar =  $_COOKIE['var1'];
   echo $phpVar;
?>

answered Mar 16, 2016 at 4:16

Set php variable in jquery

If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.

Do some thing like this.

$(".next").click(function (){

var php_val=$("#php_val").val();//it is getting value from hidden filed whose id is 'php_val'.

//make ajax call with this as follows

$.post("a.php",{php_val:php_val},function(data){

//a.php is page name where u want to send php value.
})


});
<?php $var=1; ?>
<input type="hidden" id="php_val" value="<?php echo $var; ?>"/>

I hope this answers your questions

answered Mar 16, 2016 at 4:30

It will help you ,

 <script>
 $(document).ready(function() {
 $(".next").click(function() {
    <?php echo $var     = 1 ; ?>
 });
});
</script>

<?php
  if (!empty($var)) {
     echo $var;
 }
 ?> 

answered Mar 16, 2016 at 5:15

In this post we will give you information about How to access PHP variables in JavaScript or jQuery?. Hear we will give you detail about How to access PHP variables in JavaScript or jQuery?And how to use it also give you demo for it if it is necessary.

In this post, I will tell you how to access PHP variables in JavaScript or jQuery.

Most of the time you will need to access php variables inside jquery or javascript.

If you have simple string value then you can echo this in javascript directly but if you have array type of value and you want to get it in javascript then you have to use json encode method.

See the example below :

  1. <?php
  2. $simple='demo text string';
  3. $complex=array('demo','text',array('foo','bar'));
  4. ?>
  5. <script type="text/javascript">
  6. var simple ='<?php echo $simple; ?>';
  7. console.log(simple);
  8. var complex =<?phpechojson_encode($complex);?>;
  9. console.log(complex);
  10. </script>
<?php
    $simple = 'demo text string';
    $complex = array('demo', 'text', array('foo', 'bar'));
?>
<script type="text/javascript">
    var simple = '<?php echo $simple; ?>';
    console.log(simple);
    var complex = <?php echo json_encode($complex); ?>;
    console.log(complex);

</script>

This code will work in php file.

Hope this code and post will helped you for implement How to access PHP variables in JavaScript or jQuery?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

How set PHP variable value in jquery?

If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.

Can we use PHP variable in jquery?

Most of the time you will need to access php variables inside jquery or javascript. If you have simple string value then you can echo this in javascript directly but if you have array type of value and you want to get it in javascript then you have to use json encode method.

Can I use PHP variable in JavaScript?

Inside the JavaScript, we can use the PHP tag to echo the PHP variable assigning it to a JavaScript variable. For example, assign a value Metallica to a variable $var inside the PHP tags. Write a script tag and inside it, write a PHP tag. Inside the PHP tag, echo a JavaScript variable jsvar .

How do you declare a variable in jquery?

Declare a variable using the var keyword. Initialize using the = symbol. Example: var hText = "This is just some text." - Click on the heading to see the text displayed in the <p> elements.