Question

i m writing a script using javascript ajax request and php.. i make an ajax call to verify that a user is recognised or not and according to the "response" i want to make a second ajax request either letting him either make his choices if his is already a registered user, or display the already submitted choices

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    var script = document.createElement('script');
        script.innerHTML = xmlhttp.responseText;
        document.getElementsByTagName('head')[0].appendChild(script);
        removeOnclick();
        //document.getElementById("txtHint").innerHTML=
    }
  }
xmlhttp.open("GET","submit_votes.php?q="+str+string,true);
xmlhttp.send();
}

on my php side

i have these lines in case he is recognised

if (mysqli_num_rows($result) > 0) {

while($row = mysqli_fetch_array($result))
  {
    echo "document.getElementById('".$row['option1']."').className += ' colour1';";
    ....
}

but if he is not recognised i have set this check

if (!isset($_GET['option1'])){ //option1 is one of the parameters i pass through "string"
    echo "nodata";
    }

the first call is made passing only the string is set ""

 q="+str+string

while on the second call string is set as option1=A&option2=B etc etc...

the issue is, that i try to alert the xmlhttpresponse to check if the response has been altered, but the resposne remains the same..for instance if he is not recognised i ll get in both responses "nodata" while if i refresh the page and he is recognised, i ll get the document.getElementById('".$row['option1']."').className += ' colour1'; response..

the reason i need this is because i want this part

    var script = document.createElement('script');
        script.innerHTML = xmlhttp.responseText;
        document.getElementsByTagName('head')[0].appendChild(script);
        removeOnclick();
        //document.getElementById("txtHint").innerHTML=

to be executed only if he is recognised..

Was it helpful?

Solution

after considering moonwave99 's comment as well, i ended up with a different solution..

first of all i found out, that condition check failed, because in the responseText there was an extra white space..so initially i used

xmlhttp.responseText.trim();

in order to remove unwanted characters...but it still didn't feel right the fact that i was passing js code as string from php back to js.. and i decided to use json.. so on the php side, i used json encode

$options = array($row['option1'],$row['option2'],$row['option3'],$row['option4'],$row['option5'],$row['option6'],
                    $row['option7'],
                    $row['option8'],
                    $row['option9'],
                    $row['option10']
                    );
echo json_encode($options);

and on the javascript side i used

    var jsObject = JSON.parse(xmlhttp.responseText);
    var jslength = jsObject.length;
    for (i=0; i<jslength; i++){
        document.getElementById(jsObject[i]).className += ' colour';
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top