简体   繁体   中英

Pass Javascript value as part of php url parameter

I want to pass a JavaScript variable as comment parameter in disableReason function in redirectLocation as part of URL. I have got text area value from

var disableComment = document.getElementsByClassName("disable-comment")[0].value;

I am not able pass the javascript var into the url as parameter.

$html .= '<button type="button" class="scalable back" onclick="showDiv()">' . __($sellerText) . '</button>';
$html .= '<script>function showDiv() {document.getElementById("disable-reason").style.display = "block";}</script>';    
$html .= '<div style="display:none" id="disable-reason"><label for="disable-reason">Disable Reason</label><textarea class="disable-comment" name="disable-comment" rows="2" cols="25"></textarea><button type="button" class="scalable back" onclick="disableReason()">' . __($sellerText) . '</button><script>function disableReason() {var disableComment = document.getElementsByClassName("disable-comment")[0].value;redirectLocation(\'' . $this->getUrl('mpadmin/marketplace/sellerAccountEnable', ['id' => $data['entity_id'],'status'=>$sellerStatusData,'comment'=>'**variable here** ']) . '\');}</script></div>';

Can you please help.

we can access Php variable using JavaScript. but we can't directly access JavaScript variable value using Php because php, python, java that are Server Side Languages. and JavaScript, jQuery is a Client Side Languages...

for Pass Value of Javascript Variable to Server Side languages you need to Call AJAX Request

$.ajax({
   url: example.com/test.php, //pass here Url...
        type: 'GET', //method type ... GET, POST
        dataType: 'html', //(html or JSON) not necessary but if you want to need pass then you can..
        headers: {
            // if you need pass authentication tokens then you can do it using headers in AJAX request
        },
        data: { 
            id: id; // pass here your data which you want to pass
        },
        success: function (data) {
            here you will be get success Response
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM