简体   繁体   中英

How to get echoed value from javascript to PHP variable?

On the following website http://www.e-domov.cz/oblozkove-zarubne when you look to table "TYP"(TYPE in English) and change the radio button to the second one the javasscript dynamicaly generate text which you can see on the right panel.

It is happend by this part of code:

right3.php

Typ: <span id="zarubneStena"><?= $_SESSION['zarubneStena']; ?></span><br>

script.js

function nastavZarubneTyp2(value)
 {
$('#zarubneStena').html(value);
$.get("/C/AJAX/typZarubne2.php", { zarubne: value});
}

typZarubne2.php

<?php
     session_start();

      $_SESSION['zarubneStena']=$_GET['zarubne']; 
?>

oblozkove-zarubne.php

<td><input type="radio" name="typ<?php $uid=uniqid();echo $uid;?>" checked="checked" onClick="nastavZarubneTyp2('Na stěnu')"></td><td>Klasická obložková zárubeň</td>

<td><input type="radio" name="typ<?php echo $uid;?>" onClick="nastavZarubneTyp2('Do stavebního pouzdra');"></td><td>Obložková zárubeň pro posuvné dveře do stavebního pouzdra</td>

I don´t understand too much to this record:

 Typ: <span id="zarubneStena"><?= $_SESSION['zarubneStena']; ?></span><br>

So i want some advice how to dynamicaly get the value of $_SESSION['zarubneStena'] which is generated in real-time by javascript/AJAX. I know, it's difficult to understand what i want, but i hope, that somebody helps. Thanks too much.

You could have something as follows:

<?php session_start(); ?>

<div id="hiddenDiv" style="display:none">
    <?php echo $_SESSION['zarubneStena']; ?>
</div>

<script>
    var text; 
    text = $('#hiddenDiv').text();
</script>

Its kinda cheating mixing two technologies but it works ;-)

Also I wouldn't recommend the habit of

<?= $_SESSION['zarubneStena']; ?> <?= $_SESSION['zarubneStena']; ?> is being generated by php on the server side. the session is storing a key name 'zarubneStena' - but this is not what you asked for.

The code in the html page is sending the data:

<td><input type="radio" name="typ5090ee43d6d70" checked="checked" onClick="nastavZarubneTyp2('Na stěnu')"></td><td>Klasická obložková zárubeň</td>
<td><input type="radio" name="typ5090ee43d6d70" onClick="nastavZarubneTyp2('Do stavebního pouzdra');"></td><td>Obložková zárubeň pro posuvné dveře do stavebního pouzdra</td>

Notice the onClick="nastavZarubneTyp2('Na stěnu')" - this is what updates the text.

Its in oblozkove-zarubne.php and is the GET value of the key 'zarubne' - that SESSION['zarubneStena'] is set from.

Hope that helps. Maybe it just confuses.

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