Access a JavaScript variable from PHP

后端 未结 8 1051
萌比男神i
萌比男神i 2020-11-22 11:47

I need to access a JavaScript variable with PHP. Here\'s a stripped-down version of the code I\'m currently trying, which isn\'t working:

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 11:56

    try adding this to your js function:

        var outputvar = document.getElementById("your_div_id_inside_html_form");
        outputvar.innerHTML='';
    

    Later in html:

        

    this div will contain the js var to be passed through a form to another js function or to php, remember to place it inside your html form!. This solution is working fine for me.

    In your specific geolocation case you can try adding the following to function showPosition(position):

        var outputlon = document.getElementById("lon1");
        outputlon.innerHTML = '';
        var outputlat = document.getElementById("lat1");
        outputlat.innerHTML = '';  
    

    later add these div to your html form:

    In these div you'll get latitude and longitude as input values for your php form, you would better hide them using css (show only the marker on a map if used) in order to avoid users to change them before to submit, and set your database to accept float values with lenght 10,7.

    Hope this will help.

提交回复
热议问题