0

I am adding the comments input box of every XML-RSS article. When I select a RSS url, that results are commonly as below

PHP code

for ($i=0; $i<=19; $i++) {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_date=$x->item($i)->getElementsByTagName('pubDate')
  ->item(0)->childNodes->item(0)->nodeValue;

  echo ("<p><a href='" . $item_link
  . "' target=\'_blank\'>" . $item_title . "</a>");
  echo ("<br>");
  echo ($item_desc . "<br>".$item_date."</p>");

  $item_link1 = str_replace('http://','',$item_link);
  $item_link2 = str_replace('https://','',$item_link1);
  $item_link3 = str_replace('/','-',$item_link2);
  $item_link4 = str_replace('?','-',$item_link3); 

  $content = $item_title."--". $item_link."--".$item_desc."--".$item_date;   
  file_put_contents("./comments/".$item_link4.".txt",$content);

  //Next line is something wrong or not??
  echo "<option id=".$item_link4." onclick=\"showComment(this.value)\" value=".$item_link4."> Show Comments </option> <div id=\"".$item_link."\"></div>";  

Next, I can see the results as a below picture. enter image description here

And I am coding JS which has the showComment function as below.

 function showComment(str) {
    if (str.length == 0) { 
        document.getElementById(""+item_link4+"").innerHTML = "";
        return;
     } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById(""+item_link4+"").innerHTML = this.responseText;
            }
        };

         var q = document.getElementById(""+item_link4+"").value;
        xmlhttp.open("GET", "showComment.php?q=" + q, true);
        xmlhttp.send();
     }
    }

And Next, I am coding the showComment.php as below to see the 'q' value. But, Notice: Undefined index: q in E:\xampp\htdocs\code\v1\showComment.php on line 5.

The first part is like next.

// line 5 is $q = html... 
$q = htmlspecialchars($_GET['q']);
global $result;
echo $q;

Eventually, I got something wrong in JS code which received item_link4 How do I change the JS variable originated from php-HTML code? Please your Kindness.

Drohjho
  • 71
  • 9
  • have you tried to `var_dump(item_link4)` in your php code? or you can use developer tools on your browser to shown if `your option` already has value – Tobok Sitanggang May 04 '18 at 19:15
  • Difficult. In developer tools, How can I test the value of variable in JS? – Drohjho May 04 '18 at 19:25
  • you can use `console.log(your_variable);`. it will display a result in your console developer tools or u can simpli use `alert(something);` – Tobok Sitanggang May 04 '18 at 19:30
  • When I use var_dump($item_link4), the result is string(56) "www.doctorsnews.co.kr-news-articleView.html-idxno=123372" string(56) "www.doctorsnews.co.kr-news-articleView.html-idxno=123372" string(56) "www.doctorsnews.co.kr-news-articleView.html-idxno=123372 , instead of 'show comments' – Drohjho May 04 '18 at 19:32
  • console.log($item_link) VM2231:1 Uncaught ReferenceError: $item_link is not defined at :1:13 (anonymous) @ VM2231:1 console.log(item_link) VM2239:1 Uncaught ReferenceError: item_link is not defined at :1:13 – Drohjho May 04 '18 at 19:33
  • console.log(item_link4) VM2440:1 Uncaught ReferenceError: item_link4 is not defined at :1:13 (anonymous) @ VM2440:1 console.log($item_link4) VM2443:1 Uncaught ReferenceError: $item_link4 is not defined at :1:13 – Drohjho May 04 '18 at 19:35
  • First PHP variable $item_link4 sends the value to html page. But when onclick, the value of the $item_link4 doen't go to the JS. I think the option value was already parsing as string. So how can I catch the parsing string. That is a point, i think. – Drohjho May 04 '18 at 19:38
  • 1
    `string(56) ` is your length of your value where the type is string. and `q` should have int value cuz you use `document.getElementById` thats why you get that error – Tobok Sitanggang May 04 '18 at 19:41
  • 1
    yes, thats right. you can follows this link https://stackoverflow.com/questions/2470089/javascript-getelementbyid-and-convert-it-to-string – Tobok Sitanggang May 04 '18 at 19:49

1 Answers1

1

I have tried so many times. At last I got the good result for going to the better. PHP code is

 echo " <option  id=".$i." onclick=\"showComment".$i."(this.value)\" value=".$item_link4."> Show Comments </option>";  

enter image description here

And I changed JS code as below.

<script>
    function showComment0(str) {
if (str.length == 0) { 
    document.getElementById("0").innerHTML = "";
    return;
 } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("0").innerHTML = this.responseText;
        }
    };

     var q = document.getElementById("0").value;
    xmlhttp.open("GET", "showComment.php?q=" +q, true);
    xmlhttp.send();
 }
}
</script>
    ....

  <script>
    function showComment22(str) {
if (str.length == 0) { 
    document.getElementById("22").innerHTML = "";
    return;
 } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("22").innerHTML = this.responseText;
        }
    };

     var q = document.getElementById("22").value;
    xmlhttp.open("GET", "showComment.php?q=" +q, true);
    xmlhttp.send();
 }
}
</script>
  <script>
    function showComment23(str) {
if (str.length == 0) { 
    document.getElementById("23").innerHTML = "";
    return;
 } else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("23").innerHTML = this.responseText;
        }
    };

     var q = document.getElementById("23").value;
    xmlhttp.open("GET", "showComment.php?q=" +q, true);
    xmlhttp.send();
 }
}
</script>

So I made Id 0 to 18 script. That is, 19 scripts are necessary. I am waiting for better answer.

Drohjho
  • 71
  • 9