Search the Community
Showing results for tags 'realtimeapp'.
-
Hello I have this Ajax code witch working good, but I'm not really good at javascript I just started 1 month ago, so I'm not sure if this code is perfect or not, or is there any way to improve this AJAX code. HTML/AJAX <input type="text" id="text_1" /> <button name="js_btn_func" id="js_btn_func" onclick="ajax()" > Execute JavaScript / AJAX Function </button> <div id="success_2"></div> <div id="result_2"></div> <div id="error_2"></div> <script> try { var results_area_2 = document.getElementById("result_2"); var error_area_2 = document.getElementById("error_2"); var success_area_2 = document.getElementById("success_2"); function ajax(str) { var xhttp; var str = document.getElementById("text_1").value; if (str == "") { document.getElementById("result_2").innerHTML = ""; return; } xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { results_area_2.innerHTML = xhttp.responseText; success_area_2.innerHTML = ('Success: Script is <b>ON</b>'); } } xhttp.open("POST", "action.php?name="+str, true); xhttp.send(); } } catch(e){ alert('An error has occurred: '+e.message) error_area_2.innerHTML = ('Error: Script is <b>OFF</b>'); } </script> PHP <?php $random_query = mt_rand(1, 9999); if (isset($_REQUEST["name"])){ $q = $_REQUEST["name"]; echo "Hello <b>$q</b>! random number : $random_query "; } ?> Thanks in advance.
- 4 replies
-
- php
- javascript
-
(and 2 more)
Tagged with: