1

I get the error " Undefined array key "q2" in.."when ever i try to access data from $_SESSION[""] after i change the language with google translate in my php website

my code

// using google translate
<div id="google_translate_elemen" class='btn'></div>
  <script type="text/javascript">
        function googleTranslateElementInit() {
            new google.translate.TranslateElement({
                pageLanguage: 'en'
            }, 'google_translate_elemen');
        }
    </script>

    <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
// getting my data
    $req = $con->prepare('SELECT q_id,q_text FROM question where cat = ? ');
    $req->execute(array($ca));
  while ($qdata = $req->fetch()) {
        $_SESSION["qid"] = $qdata[0];
        $req2 = $con->prepare('SELECT w.wa_text,w.wa_id FROM wrong_ans w where w.cat = ? and w.q_id=?');
        $req2->execute(array($ca, $qdata[0]));
        while ($adata = $req2->fetch()) {
            echo  " <input type='radio' id='$adata[0]' name='$qdata[0]' value='$adata[1]''>
                        <label for='$adata[0]'>$adata[0]</label><br>";}

//manipulating the data i get
if(isset($_POST['submit_btn'])){
$id = "nodata";
                    if (isset($_POST[$_SESSION["qid"]])) {
                        $id = $_POST[$_SESSION["qid"]];
                        echo $id;
                        echo $_SESSION["qid"];
                    }
 array_push($_SESSION["ans"], $_POST[$_SESSION["qid"]]); //the executing stops here}
        }

the code works perfectly fine whene i don't change the language. and i don't think that the problem is the $_SESSION since echo $_SESSION["qid"] returns the correct value and echo $id returns nodata

can anyone help me fix that?

kiki
  • 11
  • 3
  • Sounds like changing language removes the session cookie. This could also happen any time so you should code defensively using the advice in the duplicate – Phil Jun 01 '23 at 23:32
  • @CBroe I'm trying to get data from post then add it to an array, the session is my array key for post but it doesn't work – kiki Jun 02 '23 at 10:57
  • Can you please clarify the context a bit here, I am not sure what exactly you are doing. It looks like you are _creating_ input fields at the same time you are processing a _previous_ form submission? Is it guaranteed then, that the query you are executing there, returns the exact same results it did the first time (when you created the _previous_ form)? – CBroe Jun 02 '23 at 11:19
  • @CBroe, the select query is in a function that i call in the submit form, after i call it once i start collecting the data in an array that is the $_SESSION["ans"]. the problem is the $_POST, because i tested the $_SESSION["qid"] and it's not null and always gives me the correct data, and the code works for a few times before it stops, and works when i don't change the language – kiki Jun 02 '23 at 14:03
  • Not sure how you lose the session here. That Google translate functionality happens "in page", via client-side JavaScript? Or is it the version that loads the site under a completely different host name, one from Google? (If the latter, it would be clear that the session cookie won't be working any more in that scenario.) – CBroe Jun 05 '23 at 05:50
  • _"and i don't think that the problem is the $_SESSION since echo $_SESSION["qid"] returns the correct value"_ - well then the POST data must be the problem. Have you checked what your form looks like after you let it get translated by Google, and what request it actually sends (browser dev tools, network panel) ...? – CBroe Jun 05 '23 at 09:22
  • @CBroe yes the name value and id stays the same, what's more confusing it works sometimes without me changing anything, is there a way from me to not play google translate on the inputs? (btw I'm a beginner at php, i don't really know much) – kiki Jun 05 '23 at 09:38
  • Then you need to go and figure out what the _exact_ conditions are, under which this occurs. – CBroe Jun 05 '23 at 09:43

0 Answers0