How to pass JS variable to php?

前端 未结 5 766
春和景丽
春和景丽 2020-11-28 16:53

I have a javascript functions which returns a hash. I need to pass this hash to php to do stuff with it. Whats the best way to do that?

相关标签:
5条回答
  • 2020-11-28 17:23

    You could use a cookie. How the exchange takes place (AJAX, page reload, whatever) is up to you.

    in PHP: see setcookie()

    in JS: see document.cookie - or perhaps a JS library such as Dojo / jQuery.

    0 讨论(0)
  • 2020-11-28 17:27

    Assuming you mean JavaScript function returns a hash and sends it to PHP - then AJAX

    0 讨论(0)
  • 2020-11-28 17:28

    You should give more info about what exactly you are trying to do. Like this all we can do is guess and you'll get no good answers. But the usual suspects in this case are:

    • AJAX (or JSON)

    • Cookies

    • Hidden form fields, where you set the value via JS

    Give more info and we can be more specific.

    0 讨论(0)
  • Use AJAX. But remember, never trust data coming in from GET or POST and always run the data through a security check before using or storing it.

    0 讨论(0)
  • 2020-11-28 17:30

    look into jquery, this will make your this easier!

    $.get('myphp.php?senddata='+javascriptdata,function(receivedata){
    
        alert('this is what was received' + receivedata);
    
    });
    

    or you could set a hidden input's value in a form and submit.

    0 讨论(0)
提交回复
热议问题