-1

What should I do if I want to save a javascript variable value, say var a, inside a php script variable without using post/get method or ajax?

 <script>
  var count=3;
   $("#add_driver").click(function () { 
  $( "#add_driver_section").replaceWith( "<div class='wrap-input100 validate- 
 input bg1 rs1-wrap-input100' > <span class='label-input100'>Gender</span> 
 <div class='contact100-form-radio m-t-15'> <input class='input-radio100' 
  id='male-radio"+ count +"' type='radio' name='type-product"+ count +"' 
    value='male' checked='checked' > <label class='label-radio100' for='male- 
      radio"+ count +"'> Male </label></div><div class='contact100-form- 
      radio'> <input class='input-radio100' id='female-radio"+ count +"' 
      type='radio' name='type-product"+ count +"'  value='female' > <label 
      class='label-radio100' for='female-radio"+ count +"' > Female </label> 
       </div></div>");  
  count++;}
   );
</script>

i have edited my question to give you guys a little more perspective as to what i want to acheive. Here i first want to check if type-product"+ count + is set or not. if it is set then i want the radio button's selected attribute to have the value selected.

3 Answers3

0
<script>
    var a = "variable a";
</script>

<?php

    $a = "<script>document.write(a)</script>";
    echo $a;
?>

try this code to save js variable in php variable

0

Cookies can be used to share data between server and browser because these are accessible from sides. Data can be set to cookie using JAVASCRIPT (Set and get Cookies in JS) and can be accessed at server using $_COOKIE variable in PHP.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
0

you can use php variable in your javascript, but, dont put it on .js file instead you should put in on .php or .html file.

<script>
var a = '<?php echo myvariable ?>';
</script>

but if you want to dynamically store your variable for future use, you can use cookies to store the variable.

function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name) {   
    document.cookie = name+'=; Max-Age=-99999999;';  
}

and you can use the code like this :

function createhtmlcode(){
    var a = getCookie('typeprod');

    var myhtml = '<p>html code here';
    if (a) {
        myhtml += <input type='radio' name='type-product"+ count +"'  value='female' selected >
    }else{
        myhtml += <input type='radio' name='type-product"+ count +"'  value='female'>
    }
    myhtml += 'your next code here</p>';
    return myhtml;
}
$(document).ready(function(){

    setCookie('typeprod',true,7); //7 day, use this to store your variable to memory/cookies.

    $("#add_driver").click(function () { 
        var htmlcode = createhtmlcode();
        $( "#add_driver_section").replaceWith(htmlcode);
    };
});
Arief Setiabudi
  • 101
  • 1
  • 4