0

index.php

<?php
    //grab product id's who need to be showed
    $pids = '1,2,3';
?>

<html>

<head>
    <?php include 'lol.js.php'; ?>
</head>


<body onload="update_timers();">


    <div id="timer_3183" class="timer countdown clearfix" title="1301994024"></div>

lol.js.php

<script type="text/javascript">

function update_timers()
{
    check_detail('<?=$pids?>', 'ajax.php');
}




function updatepage(str, pids)
{
      //loop through all the products, change time
      document.getElementById("timer_3183").innerHTML = document.frm.hide.value;
}

function check_detail(pids, strURL) 
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() 
    {
        if (self.xmlHttpReq.readyState == 4)
            updatepage(self.xmlHttpReq.responseText, pids);
            //document.getElementById("myDiv").innerHTML = self.xmlHttpReq.responseText;
    }
    self.xmlHttpReq.send("pids=" + pids); //this goes to PHP script via POST
}


</script>

ajax.php

<?php

    echo '
<form name="frm">
    <input type="hidden" name="hide" value="Some random text">
</form>';
?>

I'm trying to get the div in index page set to the value in ajax.php.

The line that's not working is: document.getElementById("timer_3183").innerHTML = document.frm.hide.value;

If i replace it with a literal string, it works: document.getElementById("timer_3183").innerHTML = "rand text";

But when i want to set it to "document.frm.hide.value;" expression, the value doesn't change when the page loads.

Any ideas what I'm doing wrong?

1
  • But ... that form is not part of the document yet when you're trying to add it ... Commented Apr 5, 2011 at 13:38

2 Answers 2

1

Well, I don't know what that "loop through ..." comment means, but:

  document.getElementById("timer_3183").innerHTML = str;

seems like it'd do something more interesting. The form you want to add is in the XMLHttpRequest response text, right? That's what your "ajax.php" file returns to the browser. At the point that the request succeeds, it's still just plain un-parsed text. By stuffing that text into the "innerHTML" property of the target "drop zone" <div>, you give it to the browser for parsing. When that's done, the form will be there inside that <div>.

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

Comments

0

Use getElementById again? I.e. assign some id, say hide to input:

<input type="hidden" name="hide" value="Some random text" id="hide">

And write the JS code like this:

document.getElementById("timer_3183").innerHTML = document.getElementById('hide').value;

Comments

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.