0

when i used jquery ajax for fetching a data from database the response text contains html codes also. my code so far is:

$("#onclick").click(function() {
     $.ajax({
        async:"true",
        type:"POST",
        url:"process.php",
        dataType: "text", 
        data:"tempid="+tid,
        success:function(dat) {
           $("#templ").html(dat);                
        }
    });      
});

The file process.php contains only the below code

include "connect.php";
if(isset($_POST['tempid'])&& $_POST['tempid']>0)
{
    $temp_id= $_POST['tempid'];
    $sql="SELECT template FROM templates WHERE tid='".$temp_id."'";
    $res=  mysql_query($sql);
    if((mysql_num_rows($res))>0)
    {
        While($row = mysql_fetch_array($res))
        echo '<b>'.$row['template'].'</b>';
    }
    else
    {
        echo 'NOT';
    }
}

the outptut i got is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
</head>    
<body>
</body>
</html>
<b>/* The mysql value */</b>

Is there any mistakes in my code? i want to show only that mysql value in my textbox with id templ. How to avoid all that html code from response? Please tell me if any further details are required..

Adrian Forsius
  • 1,437
  • 2
  • 19
  • 29
Zammuuz
  • 708
  • 4
  • 18
  • 43
  • Something is wrong if that php is generating that output. It normally shouldn't happen. Without enough information to reproduce the problem, it's tough to guess what. – recursive Oct 09 '14 at 06:31
  • Something happens in your php code. If it is not your `process.php` or `connect.php`, then may be it is something that `connect.php` includes. – kyo Oct 09 '14 at 06:55
  • `connect.php` contains only database connect codes like `mysql_connect` and `mysql_select_db`. – Zammuuz Oct 09 '14 at 07:08
  • You are missing the #templ-div, in this code. What does you console say about the response? – Adrian Forsius Oct 09 '14 at 08:05
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Oct 09 '14 at 08:15
  • keep in mind that everything not included in `` tags is echoed as it is. But if that html code is not inserted by any of your PHP scripts, another possible, if rather exotic, source for this may be that your web server is set up to automatically include this snippet to every request. – cypherabe Oct 09 '14 at 08:46

1 Answers1

0

Just Put ob_clean(); before echoing in process.php it will solve your problem but I quite agree with Quentin's Point

cypherabe
  • 2,562
  • 1
  • 20
  • 35