0

I have a AJAX script which i was using previously and was able to retrieve data from viewCommentsJson.php as such[{"comments":"Greta"},{"comments":"John"}]. I was wondering if its able to decode the return value such that it display it properly ?

Thanks in advance

Greta
John

Main.php

<a onclick="showUser('.$row['ID'].')" method = "POST" action= "viewCommentsJson.php">Show Comments</a> <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("Post","viewCommentsJson.php?q="+str,true); xmlhttp.send(); } </script> 

viewCommentsJson.php

$com = $_REQUEST["q"]; include 'connectDatabase.php'; //opening sql table $selected = mysql_select_db("2000",$dbhandle) or die("Could not select 2000"); $arr = array(); $data = mysql_query("SELECT comments FROM comment WHERE ID = '$com'"); $rows = array(); while($r = mysql_fetch_assoc($data)) { $rows[] = $r; } print json_encode($rows); 

2 Answers 2

1

Since you don't mind a jQuery solution, here is how it can be done

function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } $.ajax({ type:'post', url: 'viewCommentsJson.php', data:{q:str}, success:function(data) { data = $.parseJSON(data); var response; $.each(data, function(index, value){ response += value+'<br />'; }); $('#txtHint').html(response); } }); } 

You can refer $.ajax and $.parseJSON for more information.

Note: Your SELECT comments FROM comment WHERE ID = '$com' is prone to SQL injection. At the very least, you should sanitize all incoming data before using it in your query directly.

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

7 Comments

Hey thanks for the help. However i ran into a problem after replacing with your script. ReferenceError: showUser is not defined. I do not understand why showUser is not defined given that the function is being declared.
Try putting your <script> tag above your <a onclick..... tag. Also, an anchor tag shouldn't have method and action attributes so you can remove them from the <a> tag too
I am sorry i cant add the <script> tag above <a onclick..... it will render my button invisible. Also i will be needing to pass the value .$row['ID']. into $com = $_REQUEST["q"];
Why would it make your button invisible? It won't and it shouldn't
I am assuming you are talking about encapsulating the <a onClick...> with the script tag as such right ? <script><a onclick="showUser('.$row['ID'].')">Show Comments</a></script>
|
1
<a onclick="showUser('1')" href="#?">Show Comments</a> <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { data = eval(xmlhttp.responseText); for(i=0;i<data.length;i++){ document.getElementById("txtHint").innerHTML = document.getElementById("txtHint").innerHTML + data[i].comments; } } } xmlhttp.open("Post","viewCommentsJson.php?q="+str,true); xmlhttp.send(); } </script> <div id="txtHint"></div> 

First of all remove method and action from A tag and used above code now. Removed some typos

1 Comment

Thank you very much for helping me out. I got an Uncaught syntaxerror: unexpected identifier error.Any idea where i might have screw up again ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.