Passing a JavaScript Value to a PHP Variable (With Limitation)

前端 未结 3 1638
悲&欢浪女
悲&欢浪女 2020-12-17 08:15

I understand that there are existing questions and answers that have already addressed this matter. I\'ve looked through them but my situation doesn\'t allow me to apply the

相关标签:
3条回答
  • 2020-12-17 08:41

    What I'm really trying to achieve:

    PHP variables getting JavaScript values in the same page in a Facebook iFrame. It is all right for the page to reload to pass parameter to itself.

    Easiest way: Generate a form element, populate it with some (hidden) input fields, and POST it to your server.

    AJAX is also possible, and maybe nicer for the user as it doesn’t require a reload.

    Since your’re not describing any problem in detail, I can only assume it’s mostly due to a general lack of knowledge/experience in these matters on your part. So maybe look for some tutorials first, to get a general understanding of the techniques involved.

    0 讨论(0)
  • 2020-12-17 08:44

    It's still not clear whether you want to:

    1. pass data that you receive in PHP via app_data to JavaScript, or

    2. pass data from JavaScript to PHP.

    Pass data from PHP to JavaScript

    <head>
    <script type="text/javascript">
    var app_data = <?php echo json_encode($appData); ?>;
    </script>
    </head>
    <body>
    ...
    

    Pass data from JavaScript to PHP

    $.ajax({
        url: location.pathname, // current page
        type: 'POST',
        data: {
            vara: 'hello',
            varb: 'world'
        },
        success: function() {
            // ...
        }
    });
    
    0 讨论(0)
  • 2020-12-17 08:52

    You can do that easily with javascript for example HTML code:

    <select id = "Select" onchange = "FijarPrecio (this.id);" >
        <option value = "10"> product 1 </ option>
        <option value = "20"> product 2 </ option>
        <option value = "30"> product 3 </ option>
    </select>
    <input type = "text"  name = "Price" id = "Price"/>
    <script type = "text/javascript" >
        FijarPrecio function (id) {
           PrecioSel var = document.getElementById (id);
           PrecioActual var = document.getElementById ('Price');
           PrecioActual.value = PrecioSel.value;
        }
    </script>   
    
    0 讨论(0)
提交回复
热议问题