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.

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 Commented May 4, 2018 at 19:15
  • Difficult. In developer tools, How can I test the value of variable in JS? Commented May 4, 2018 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); Commented May 4, 2018 at 19:30
  • 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 Commented May 4, 2018 at 19:41
  • 1
    yes, thats right. you can follows this link stackoverflow.com/questions/2470089/… Commented May 4, 2018 at 19:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

you found your own way :D, happy coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.