Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    innerHTML and AJAX

    Im trying to use Ajax to validate a form field as soon as the user blurs from the field.
    Here is what the code looks like:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD><TITLE>Demo 2 - Getting a page's content</TITLE>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252">
    <SCRIPT type="text/javascript">
    function alrt(){
    alert("sup baby!")
    }
    function HttpRequest(url){
    var pageRequest = false //variable to hold ajax object
    /*@cc_on
       @if (@_jscript_version >= 5)
          try {
          pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
          }
          catch (e){
             try {
             pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
             }
             catch (e2){
             pageRequest = false
             }
          }
       @end
    @*/
    
    if (!pageRequest && typeof XMLHttpRequest != 'undefined')
       pageRequest = new XMLHttpRequest()
    
    if (pageRequest){ //if pageRequest is not false
       pageRequest.open('GET', url, false) //get page synchronously 
       pageRequest.send(null)
       embedpage(pageRequest)
       }
    }
    
    function embedpage(request){
    //if viewing page offline or the document was successfully retrieved online (status code=2000)
    if (window.location.href.indexOf("http")==-1 || request.status==200)
       document.getElementById("contentdiv").innerHTML = request.responseText
    //document.write(request.responseText)
    }
    
    </SCRIPT>
    
    </HEAD>
    <BODY>
    <DIV id=contentdiv></DIV><INPUT onclick="HttpRequest('ds.php?staff_id=7');" type=button value="Get content"> 
    </BODY></HTML>
    Where ds.php goes this way:
    PHP Code:
    <?php
    include("db.inc");

    $staff_id $_REQUEST['staff_id'];

    $query "select staff_id from personnel_master where staff_id='$staff_id'";
    $result mysql_query($query);

    if(!
    $result)
    echo 
    mysql_error();

    if(
    mysql_num_rows($result) < 1)
    echo 
    "<div style='border: 1px solid red'>Staff with staff id: <b>$staff_id</b> not found!</div>";
    ?>
    This works fine but when i tried echoing back a script
    this way,
    PHP Code:
    if(mysql_num_rows($result) < 1)
    echo 
    "
    <script>
    alert('Staff not found!');
    </script>
    "

    it doesnt work. does anyone know how to get round this?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Try putting the echo statement on one line and use \n to represent the new line characters in the output.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    doesnt work. :-(


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •