0

Trying to do my first Ajax call. Users can upload a file to my server. I want to add the feature of scanning for nudity and not accepting if nudity is found. I found a javascript scanner nude.js that will scan pics for nudity. I comes with demos and all. I am trying to apply it to my site.

<script type="text/javascript" src="nude.js">
function CheckNude() {
    nude.load("pics/". <?
        $q         = "select max(id) from $table";
        $result    = mysql_query($q);
        $resrow    = mysql_fetch_row($result);
        $id        = $resrow[0] + 1;
        $file      = $_FILES['file'];
        $file_name = $_FILES['file']['name'];
        $picextorg = substr($file_name, -3);
        $picext    = strtolower($picextorg);
        $picfile   = "pics/" . $id . "." . $picext;
        echo $picfile;
    ?>);
    nude.scan(function (result) {
        if (!result) {
            nude = false;
        } else {
            nude = true;
        }
    });
    nude.load( <? $_FILES['file']['tmp_name'] ?> );
    nude.scan(function (result) {
        if (!result) {
            var nude = "false";
        } else {
            var nude = "true";
        }
    });
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (xmlhttp.responseText == true) {
                alert("Nudity was found in your picture. Please try again.");
            }
        }
    }
    xmlhttp.open("GET", "new2.php?nude=" + nude, true);
    xmlhttp.send();
}
</script>
<tr>
    <td width="150">
        &nbsp;
        <!--<INPUT type="image" name="search" src="../images/formbutton.gif" border="0" >-->      
    </td>
    <td width="119">&nbsp;</td>
    <td width="147">&nbsp; <button type='submit' name='submit' value='Submit'       onclick="CheckNude()">
        <img src='../images/formbutton.gif' width='40' height='40' />        <label> 
        Submit photo</label>
    </td>

Here is the PHP:

$email    = $_POST['email'];
$aim      = $_POST['aim'];
$icq      = $_POST['icq'];
$yahoo    = $_POST['yahoo'];
$homepage = $_POST['homepage'];
$myip     = $_POST['myip'];
if (!$myip)
    $myip = $ip;
$email2      = $_POST['email2'];
$password    = $_POST['password'];
$title       = $_POST['title'];
$download    = $_POST['download'];
$approved    = $_POST['approved'];
$allowdelete = $_POST['allowdelete'];
$author      = $_POST['author'];
$facebook    = $_POST['facebook'];
$piclink     = $_POST['piclink'];
$domain      = $_POST['domain'];
$option3     = $_POST['option3'];
$secret      = $_POST['secret'];
$q           = "insert into $table values('', '$email', '$aim', '$icq', '$yahoo', '$homepage',    '0', '0', '0', '0', '0', '0', '',   now(),'$myip','$email2','$password','$title','$download','$approved','$allowdelete','$autho     r','$facebook','$piclink','$domain','$option3','$secret')";
$result = mysql_query($q) or die("Failed: $sql - " . mysql_error());
$q      = "select max(id) from $table";
$result = mysql_query($q);
$resrow = mysql_fetch_row($result);
$id     = $resrow[0];
$file   = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], "pics/" . $id . "." . $picext);
$picfile = $id . "." . $picext;
$nude    = $_REQUEST['nude'];
$alert   = "false";
if ($nude === "false") {
    $q      = "update $table set picfile = '" . $id . "." . $picext . "' where id='$id'";
    $result = mysql_query($q);
    Header("Location: index.php?id=$id");
}
if ($nude === "true") {
    $alert  = "true";
    $q      = "delete from $table where id='$id'";
    $result = mysql_query($q);
    unlink("pics/" . $picfile);
    Header("Location: new2.php");
}
echo $alert;

My page just loads to a blank white page with this code. What am I doing wrong?

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
Alex Bridges
  • 67
  • 1
  • 2
  • 10
  • Is you php code wrapped in `` ? If yes, you probably have an error in your code, follow [this answer](http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php) to enable errors and debug. – i-- Jan 16 '14 at 04:42

1 Answers1

0

You have written the script tag to src="nude.js", which will make the scripts non-executable. You should seperate the scripts in 2 <script> tag. You are not receiving nothing, but you have not send the XMLHttpRequest.

<script src="nude.js"></script>
<script>
    your ajax code...
</script>
  • Thank you! Now the code is just changing the page to say "false". Any idea what could cause that? – Alex Bridges Jan 16 '14 at 05:40
  • Do you mean that you do not want the page to be changed but just showing "false" on current page? –  Jan 16 '14 at 05:44
  • Actually what I want is if the picture has nudity the php code under if (nude === "true") to execute and for the alert box in the javascript to execute. And if no nudity is found I want the php code under if (nude === "false") to execute. – Alex Bridges Jan 16 '14 at 05:49
  • Maybe it is related to the object `xmlhttp.responseText`. This will never return a boolean so you need to add quotes. –  Jan 16 '14 at 05:52
  • Some little suggestion: Do not use `echo` after the function `header("Location: url")`. In addition, you are recommended to use lowercase for the function name. For your ajax code, use another variable name instead of `nude` for the get data. –  Jan 16 '14 at 08:17
  • Thank you for your help. Now it just changes my page to a blank page. – Alex Bridges Jan 16 '14 at 13:15