#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    37
    Rep Power
    2

    Code preview using jQuery not working


    I've provided the three codes that does the job of displaying the syntax highlighted equivalent of an entered string. But, for some reason, they ain't working. Can anybody solve this problem?
    index.html :
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     <
    html>
     <
    head>
     <
    script type="text/javascript" src="preview.js"></script>
     <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>Test Code Preview</title>
     </head>
     <body>
     <textarea id="pastecode" rows="20" cols="50" name="pastecode"></textarea>
     <br /><input type="text" id="language" name="language"/>
     <br /><input type="button" onclick="process()" value="Preview"/>
     <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
     <div id="previewcode"></div>
     </body>
     </html> 
    preview.js :
    javascript Code:
    function process(){
     $.ajax({
     url: "preview.php",
     type: "post",
     data: {
     pastecode: $("pastecode").val(),
     language: $("language").val()
     },
     success: function(data){
     $("#previewcode").html(data);
     //Or handle data with jQuery.parseXML()
     },
     error:function(jqXHR, textStatus, errorThrown){
     alert("The following error occured: " + textStatus + " " + errorThrown);
     }  
    }); }


    preview.php :

    PHP Code:
    <?php
    header
    ('Content-Type: text/xml');
    echo 
    '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

    echo 
    '<response>';
       if(!empty(
    $_POST['pastecode']) && !empty($_POST['language']))
       {
           
    $code=$_POST['pastecode'];
           
    $language=$_POST['language'];
           include(
    "geshi/geshi.php");
           
    $path     '';
           
    $geshi    = new GeSHi($code$language$path);
           
    $geshi->set_overall_style('background-color: #ffffee;'true);
           
    $out $geshi->parse_code();
           echo 
    htmlentities($out);
       }
       else
       {
           echo 
    htmlentities("<p>Nothing to display</p>");
       }
    echo 
    '</response>';
    ?>
    Whats wrong with the code?
    I'm filling up the textarea and the language box and pressing the preview button. But no response.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    27
    In your call to $.ajax() you aren't grabbing the values from your form elements - the selectors are missing a "#" at the beginning.

    $("#pastecode")
    $("#language")

    Comments on this post

    • The Alchemist agrees : thanks for helping
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    37
    Rep Power
    2
    Originally Posted by mod_speling
    In your call to $.ajax() you aren't grabbing the values from your form elements - the selectors are missing a "#" at the beginning.

    $("#pastecode")
    $("#language")
    I changes the entire PHP file and the jQuery file to :
    preview.php :
    PHP Code:
    <?php
        
    if(!empty($_POST['pastecode']) && !empty($_POST['language']))
        {
            
    $code=$_POST['pasteode'];
            
    $language=$_POST['language'];
            include(
    "geshi/geshi.php");
            
    $path     '';
            
    $geshi    = new GeSHi($code$language$path);
            
    $geshi->set_overall_style('background-color: #ffffee;'true);
            
    $out $geshi->parse_code();
            echo 
    $out;
        }
        else
        {
            echo 
    "<span style=\"color: red\">Nothing to display</span>";
        }
    ?>
    and preview.js :
    javascript Code:
    function process(){
        pastecodeVal = $("#pastecode").val();
        languageVal = $("#language").val();
        $("#previewcode").html("Fetching preview..."+"<hr />")
     
        $.post("preview.php", { pastecode: pastecodeVal, language: languageVal },
                 function(data) {
     
                    $("#previewcode").html(data+"<hr />");
     
              });
    }


    Now, everything's working fine.
    Thanks for helping.

IMN logo majestic logo threadwatch logo seochat tools logo