I have the following javascript which is intended to be used to dynamically fill another input field with the data retrieved via ajax. If I set it to fill a div it works, but changing to an input, it stops working.
My javascript is:
function showCD(str)
{
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').value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","script.php?q="+str,true);
xmlhttp.send();
}
Html is:
<input type="text" name="cds" value="" onKeyUp="showCD(this.value)">
<input type="text" name="txtHint" id="txtHint" />
As I say, if i replace the latter input with a div with the id of "txtHint" and alter the javascript to:
document.getElementById('txtHint').innerHTML=xmlhttp.responseText;
it works.
Thanks.
Related
My code does not show the correct value
function showOffice(str) {
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("selectOffice").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","officeData.php?office_id="+str,true);
xmlhttp.send();
}
this is my function for show office.
<table style="width: 100%;">
<tr >
<td><input style="width: 50%;"type="text" id="searchOffice" class="form-control" onkeypress="showOffice(this.value)" placeholder="Search Office..."></td>
</tr>
</table>
<div id="selectOffice"></div>
and I will be using the jquery for this. selectOffice table is in a different file.
If my code or question is insufficient, please message me and I will send the files. Thank you so much
I think you need to set respond text to value of the text box.
document.getElementById("selectOffice").value = xmlhttp.responseText;
hi friends Actually i have 2 questions..
I am creating a social website like facebook...
In this website i have multiple posts which are coming from database
If anyone want to give comment on this post then only first post is working..but the comment of first post is going to the last post..
<%String sql="select * from post ORDER BY id DESC";
st = con.createStatement();
rs=st.executeQuery(sql);%>
<input type="text" id="postboxwritereview" name="postboxwritereview" placeholder="write a review" onkeypress="loadXMLDoc2()"/>
<script type="text/javascript">
document.getElementById("postboxwritereview").onkeypress = function(event){
if (event.keyCode == 13 || event.which == 13){
var xmlhttp;
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("postbox_bottom_my_review").innerHTML=xmlhttp.responseText;
}
}
var x = document.getElementById("postboxwritereview").value;
xmlhttp.open("GET", 'insertReview.jsp?user='+x+'&p_id=<%=post_id%>&p_uid=<%=post_uid%>&author_uid=<%=u_id%>', true);
xmlhttp.send();
document.getElementById("postboxwritereview").value="";
}
};
</script>
And second problem is that..
I am getting ajax response from servlet and showing that resppnse into a div
But there are many divs created dynamically with same id..so ajax response from first post is going to the last post..Plz help me
Here is the code..
AJAX CODE
<script type="text/javascript">
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("showRating").innerHTML=xmlhttp.responseText;
}
}
var username=$('input:radio[name=rating_blog]:checked').val();
xmlhttp.open("GET", 'InsertRating?user='+username+'&p_id=<%=post_id%>&p_uid=<%=post_uid%>', true);
xmlhttp.send();
}
</script>
HTML CODE
<div id="submit" onclick="loadXMLDoc()">
<div>
<input id="rating1" type="radio" name="rating_blog" value="1" onclick="this.form.submit()">
<input id="rating2" type="radio" name="rating_blog" value="2" onclick="this.form.submit()">
<input id="rating3" type="radio" name="rating_blog" value="3" onclick="this.form.submit()">
</div>
Id should be unique on page!
If You use jQuery why don't use '$.ajax()'?
You can try this:
function loadXMLDoc(event){
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
$(event.target).find("#showRating").text = xmlhttp.responseText
}
}
var username=$('input:radio[name=rating_blog]:checked').val();
xmlhttp.open("GET", 'InsertRating?user='+username+'&p_id=<%=post_id%>&p_uid=<%=post_uid%>', true);
xmlhttp.send();
}
I currently have three stored JS variables (wordChoiceOne, wordChoiceTwo, wordChoiceThree). Now that I have these stored, I want to run a function called saveUsername(). This function should do the following:
Take the jQuery variables above and pass them into PHP and load the file register-form.php into the div register-login-form
So I currently have the following AJAX request, but this only handles the reloading of the form. I've never combined jQuery with PHP before so how can I add the part in this to pass the three variables?
function saveUsername(wordChoiceOne, wordChoiceTwo, wordChoiceThree){
{
var xmlhttp;
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("login-register-form").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","register-form.php",true);
xmlhttp.send();
}
What do I add to this to pass the three variables and convert them to PHP vars like with a $ sign?
function saveUsername(wordChoiceOne, wordChoiceTwo, wordChoiceThree){
{
var xmlhttp;
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("login-register-form").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","register-form.php?one="+wordChoiceOne+"&two="+wordChoiceTwo+"&three="+wordChoiceThree,true);
xmlhttp.send();
}
Then PHP side, use $_GET['one'], $_GET['two'] and $_GET['three']
I have a website with a picture of a tree, I have then used Ajax to remove that tree and insert a number using javascript. What I used for that was;
document.getElementById("cut_oak_tree");
I have now added another tree on the page, which should have the exact same function as the first tree, except that only the tree that you clicked on, shall be removed. To avoid duplicating code, I have tried adding following:
document.getElementsByClassName("cut_oak_tree");
and then changed my div from using id to class. However, nothing happens when I click any of the trees now. My current ajax code right now, looks like this:
function loadXMLDoc()
{
var xmlhttp;
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)
{
var getClass = document.getElementsByClassName("cut_oak_tree").innerHTML=xmlhttp.responseText;//ask for this.
for(i=0;i<getClass.length;i++)
{
getClass[i].innerHTML = "";
}
}
}
xmlhttp.open("POST","xxx",true);
xmlhttp.send();
}
I have been searching a lot and found that I might need to use a for loop together with the document.getElementsByClassName("cut_oak_tree");
but I can't really get that to work. If I have figured my problem correctly, everything should be good if I could just determine which of the tree images in the div should be removed when it's pressed. Thanks in advance.
EDIT:
Html sample:
<div id "thanks">
<img class="cut_oak_tree" src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(), myFunction()">
<img class="cut_oak_tree" src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(), myFunction()">
</div>
Try the following:
document.getElementsByClassName("cut_oak_tree")[0];
If you want to apply changes to more than one elements with classname cut_oak_tree then you will have to use for loop
var getClass = document.getElementsByClassName("cut_oak_tree");
for(i=0;i<getClass.length;i++)
{
getClass[i].innerHTML = "";
}
Using your earlier HTML with slight modifications you can do the following:
HTML
<div class="cut_oak_tree">
<img src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(this.outerHTML), myFunction()" />
<img src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(this.outerHTML), myFunction()" />
</div>
Hence change your JS function to :
function loadXMLDoc(h)
{
var xmlhttp;
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)
{
var getEle = document.getElementsByClassName('cut_oak_tree')[0];
getEle.innerHTML = getEle.innerHTML.replace(h,xmlhttp.responseText);
}
}
xmlhttp.open("POST","http://www.pbpixels.com/x/post.php",true);
xmlhttp.send();
}
Demo Fiddle
Response to comment:
Inorder to uniquely identify clicked <img>s with same images just make minor change to the one of the img srcs from the two.Example give space within src src="http://www.pbpixels.com/x/images/oak.png ".Note the space after .png which will make the difference between the two
I would think that the most straightforward way to do it would be to pass the clicked element into the loadXMLDoc() as a parameter, and then get the parent of that element. Something like this:
HTML
<div id="cut_oak_tree">
<img src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(this), myFunction()">
<img src="http://www.pbpixels.com/x/images/oak.png" onclick="loadXMLDoc(this), myFunction()">
</div>
JS
function loadXMLDoc(clickedElement) {
var xmlhttp;
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) {
clickedElement.parentNode.innerHTML = xmlhttpinnerHTML = xmlhttp.responseText; //ask for this.
}
}
xmlhttp.open("POST","xxx",true); //the xxx's represent my website
xmlhttp.send();
}
NOTE: I'm not 100% sure that I have the logic right, since you changed your original post, while I was typing up my answer. :)
UPDATE #2: I found the original code in the edit history . . . my code should be correct now.
Im trying to retrieve a txt doc from server with ajax request. The name of the txt doc depends on a text input on html doc. Basically I want to append .txt to the end of the input field following an onclick event
// JavaScript Document
function getData(){
var xmlhttp;
var user=document.getElementById("nameDetails").value;
var userText = user + ".txt"; //**not the solution
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("userSubmit").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","userText",true);
xmlhttp.send();
}
If you want to append .txt to the input field itself, you could try this:
document.getElementById("nameDetails").value = document.getElementById("nameDetails").value + ".txt";
or the short form:
document.getElementById("nameDetails").value += ".txt";