Jump to content

sorenchr

Members
  • Posts

    86
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

sorenchr's Achievements

Member

Member (2/5)

0

Reputation

  1. Okay I'm reopening this one becuase now I've run into a problem I really can't figure out. My index.php looks like this <script language="javascript" type="text/javascript"> $(document).ready(function(){ $(".ajaxlink").click(function() { callServer(); return false; //Stop link from redirecting }); }); var test = { "testName": "testValue" } var testJSON = JSON.stringify(test); function updatePage(data) { document.getElementById("testDiv").innerHTML = data; } function callServer() { $.ajax({ type: "POST", url: "ajax/server.php", data: testJSON, success: function(data) { updatePage(data); }, //Upon error, output message containing a little info on what went wrong error: function (XMLHttpRequest, textStatus, errorThrown) { alert('An Ajax error occured\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown + '\nstatus = ' + XMLHttpRequest.status); } }); } </script> <div id="testDiv">Something here</div> <a href="test1.htm" class="ajaxlink">Link!</a> <br> This basically runs the callServer() function when you click the "Link!". It then sends the test json data, that is { "testName": "testValue" } to server.php. Firebug reports that the json-data is indeed sent to the server.php. My server.php looks like this: <?php print_r($_POST); ?> This returns: Array ( ) Which should be a simply name/value pair array with the JSON data instead. What am I doing wrong here? I'm using Apache 2.2 and PHP 5.3.1
  2. Nevermind, I found out how to handle this with jQuery. Marking it solved.
  3. Hey, using your code outputs: Array ( ) 1 My index.php looks like this: <script language="javascript" type="text/javascript"> //Setup xmlHTTP object var xmlHTTP = new XMLHttpRequest(); var test = { "test":"test2" } function callServer() { var url = "ajax/server.php"; xmlHTTP.open("POST", url, true); xmlHTTP.onreadystatechange = updatePage; xmlHTTP.send(test); } function updatePage() { //Check for a valid readyState if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) { var response = xmlHTTP.responseText; document.getElementById("testDiv").innerHTML = response; } } </script> <div id="testDiv">Something here</div> <a href="javascript:callServer();">Call server</a> And my server.php looks like this: <?php echo "<pre>".print_r($_POST)."</pre>"; ?>
  4. Hi there Say I request some data from a file called server.php like so: var url = "server.php"; request.open("POST", url, true); request.onreadystatechange = updatePage; request.send(JSONencodedString); How do I grab that JSON data in server.php? (I know I could do it easily using a GET-method but I specifically want to use POST) Many thanks!
  5. Yes. A simple view source would reveal that to be the case. No, the iFrame is solely used to load content from an external site (something about Ajax not supporting external sites in certain functions). What Facebook does to keep the chat-bar static, is to load each new page's content into the "main" div on the page, thereby creating a sort of iframe-like effect. If you look at the address-bar when you navigate around, you can see the hash (#) changing with every link you click, that data after the hash is used to tell their Ajax scripts where to get the content from.
  6. Is it solely to protect the data within that class from being overridden, or is there a security aspect to it as well? Best regards sorenchr
  7. Hi, I'm currently considering implementing an Ajax script a lá: http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/ And http://beski.wordpress.com/2009/04/21/ajax-pagination-with-anchor-navigation/ Both of the scripts use a setInterval function, set to every 300ms. I was wondering if this would make my website slow or anything in that manner (particularly slow/old computers) to a noticeable degree? Thanks for your time
  8. The div usually contains some other content, which is displayed to the user in case their is no hash.
  9. Hi, this thread is a bit ajax-related, but should be readable to the javascript-only enthusiast. I'll start off by showing the source code for two pages, page1.html and page2.html: Page1.html: <script type="text/javascript"> //Detect hash if(window.location.hash.length > 0) { var hash = window.location.hash; //Strip hash of # and parse through getContent getContent(hash.replace("#", "")); } function getContent(URL) { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4 && xmlhttp.status==200) { var newContent = xmlhttp.responseText; var newElement = document.createElement("div"); newElement.innerHTML = newContent; for (var i = 0; i < newElement.childNodes.length; i++) { if(newElement.childNodes[i].id == "content") { document.getElementById("content").innerHTML = newElement.childNodes[i].innerHTML; break; } } } } xmlhttp.open("GET", URL, true); xmlhttp.send(null); } </script> <body> <div id="content">Page1 div still here!</div> </body> Page2.html: <div id="block1">aaa</div> <div id="content">bbb</div> <div id="block2">ccc</div> What basically happens, is that Page1.html requests some new content via ajax from a given URL(derived from the window.location.hash). It then selects a specific portion of that requested content do be displayed in the "content" div(namely the "content" div from page2.html. God, I hope this is readable... Anyway, the script works fine, however page1.html renders the initial div content, "Page1 div still here!" and leaves it there until the ajax-request is through, which results in a quick flash of "Page1 div still here!"(0.1 ms) and then the requested content replaces it. Is there any way I can prevent this? Thanks for your time!
  10. Hi, suppose I have a string called "string1" with the following content: <div id="block1">aaa</div> <div id="block2">bbb</div> How do I obtain the content of block2? Using something like string1.getElementById("block2").innerHTML doesn't work since getElementById only works with the document object Thanks for your time.
  11. Hi guys I have a html document called test1.html, with the following code: <html> <body> <script type="text/javascript"> function ajaxFunction(userstr) { var xmlhttp; xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { document.content.innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "test2.html",true); xmlhttp.send(null); } </script> <div id="content"></div> </body> </html> And another document called test2.html with the following code: <html> <body> <div id="block1">aaa</div> <div id="block2">bbb</div> <div id="block3">ccc</div> </body> </html> The script loads all of the divs from test2 into the "content" div on the test1 page at the moment. What I need it to do however, is to load only the content from the div "block2", so that the "content" div will read "bbb". How do I do that? Thanks!
  12. Hi guys I'm about to create a sort-of, online community, where security is my main concern. So my question is, will a framework (such as Zend) be secure and reliable enough to implement? Best regards sorenchr
  13. Hey there. I'm having a hard time figuring this one out. Say I want to output "2 Hammers in toolshed", when I call the $toolshed->inventory() function, how should the code then look? class toolshed { function inventory() { echo $amount->hammers()." ".$toolname->hammer()." in toolshed"; } } class amount { function hammers() { return "2"; } } class toolname { function hammer() { return "Hammers"; } } $toolshed = new toolshed(); $amount = new amount(); $toolname = new toolname(); echo $toolshed->inventory(); This currently outputs: Fatal error: Call to a member function hammers() on a non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\test.php on line 6 I know the functions seem retarded, but I'm trying to illustrate a much more complex problem I'm currently experiencing with my code. Thanks for your time
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.