-1

I have php inside javascript, and I made a query inside php, but the query cannot be fetched even in php area itself, why? I have tried that query in PHPmyAdmin manually and it works fine. The "ca" field value is 1 not 0, but after i apply on code the result of "ca" is 0. What is wrong exactly? And there's no error message shows in console log.

image: enter image description here

simple code:

<?php
    include('ckcon.php');
    include('logincheckmember.php');
?>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <style>
    </style>
    <script>
        function kampret(){
            var ssloginmember = document.getElementById('id1').textContent;
            var ambilun = document.getElementById('id2').textContent;
            <?php
                $ssloginmember=ssloginmember;
                $ambilun=ambilun;

                $q=mysql_query("SELECT COUNT(ai) AS ca,unnum FROM t_follow WHERE username='$ssloginmember' AND username2='$ambilun'");
                $f=mysql_fetch_object($q);
                $unnum2=$f->unnum;
                $ca=$f->ca;
                if($ca==0){
                    $status='follow';
                    $unnum=0;
                }else{
                    $status='unfollow';
                    $unnum=$unnum2;
                }
            ?>
            var status='status = '+<?php echo json_encode($status);?>;
            var unnum='unnum = '+<?php echo json_encode($unnum);?>;
            var output1='output1 = '+<?php echo $ssloginmember;?>;
            var output2='output2 = '+<?php echo $ambilun;?>;
            document.getElementById('status').innerHTML = status;
            document.getElementById('unnum').innerHTML = unnum;
            document.getElementById('output1').innerHTML = output1;
            document.getElementById('output2').innerHTML = output2;
        }
    </script>
</head>
<body>
    <div id="status">status</div>
    <div id="unnum">unnum</div>
    <div id="output1">output1</div>
    <div id="output2">output2</div>
    <br>
    <button onClick="kampret()" >button</button><br>
    <br>
    <div id="id1">melisavirgi</div>
    <div id="id2">ririnputrian</div>
    <br>
    <?php
        echo "ssloginmember=$_SESSION[ssloginmember]";
    ?>
</body>
</html>

result:

enter image description here

Juna serbaserbi
  • 205
  • 2
  • 12

1 Answers1

3

The problem seems to be that you are trying to get a javascript value in the PHP, ie $ssloginmember=ssloginmember;.

The PHP code is run before the page is loaded in the browser and generates parts of your javascript code. Your script seems to be acting as if it is run every time the javascript function is called, and as if both languages are the same.

AJAX is probably going to be your best solution - post the data to a PHP script that returns the values you need.

I suspect you may need to brush up on the difference between client side and server side code, too.

RichardB
  • 2,615
  • 1
  • 11
  • 14
  • but i can grab it smootly as you can see in line 32-33 and 36-37, and the result is fine, why? – Juna serbaserbi Nov 02 '15 at 00:18
  • I can't imagine why they are working, as even if you could pass variables between the two, you are not outputting text inside quotes. I suggest you view the source when the page has loaded and add what it is outputing to your question - as why that is working seems a bigger question than why the other is not working! – RichardB Nov 02 '15 at 00:27
  • Also, I'd turn on PHP error messages when you are working on a site - that should help point to problems easier. – RichardB Nov 02 '15 at 00:28
  • Thinking on it $ssloginmember=ssloginmember, is presumably setting ssloginmember as the text string 'ssloginmember', rather than either the value of the javascript variable or falling down for having errors (as I'd expected it would). If so, var output1='output1 = '+; would output the javascript var output1='output1 =' + ssloginmember. As ssloginmember has a value in javascript, it will work as a javascript function. – RichardB Nov 02 '15 at 00:36
  • Lines 32 and 33 really don't need any php in them var output1='output1 =' + ssloginmember; should work well enough. – RichardB Nov 02 '15 at 00:37
  • That's why @Richard, I'm also confused because of it. It supposed to be not running well in line 15-16, wasn't it? i knew that. But the fact, it worked fine, why ? If it worked fine, the problem is become in line 20-21 then. – Juna serbaserbi Nov 02 '15 at 00:39
  • As I mentioned above, though may have been unclear, line 15 and 16 are most likely setting $ssloginmember as the text "ssloginmember" and $ambilun as "ambilun" rather than "melisavirgi" and "ririnputrian" as I assume you expect it to. View the source code it outputs to see what I mean. This would create valid javascript on lines 20 and 21 but the wrong mysql on line 18. – RichardB Nov 02 '15 at 00:49
  • you are right Richard. After I changed become **var output1='output1 = '+ssloginmember;** In line 32-33, and it shows fine, it means that it came from line 12-13 not from line 15-16, right?? hahahahahaha, I understand right now. Oooo, I fooled by this schema. – Juna serbaserbi Nov 02 '15 at 00:59
  • Ok @richard, thank you for your explanation, it's clear now. Maybe I gave up with this way, and starting to switch to an ajax post. – Juna serbaserbi Nov 02 '15 at 01:05
  • please help me again in [here](http://stackoverflow.com/questions/33669459/confusing-about-this-cookies-in-redirecting-system) – Juna serbaserbi Nov 12 '15 at 12:36