2013-02-20 19 views
0

慢慢地但肯定地,我要去获取AJAX。我有一个表单将文本字段和文件上传到数据库。我早些时候在PHP中使用过查询,但没有使用AJAX。现在AJAX正在工作,但不是PHP。而且我知道有些人会发现将图像加载到BLOB是令人反感的,但查询本身是有效的,所以我想专注于让我的javascript与我的PHP交谈的问题。我已经研究了这个问题,并且尝试了很多东西,但是我所做的事情是上传文件非常复杂。PHP和AJAX:AJAX正在工作,PHP没有实现查询

问题 1.纠正我,如果我错了,但如果JavaScript和jQuery实现“POST”调用,传递的参数不应该显示在页面的URL?因为他们是。
2.为什么我的PHP文件没有解析出发送的数据并将其发送到数据库?我可以在URL和Firebug中看到(虽然我也在慢慢学习Firebug),但是数据正在通过。我运行了一个测试php文件,并用该文件连接数据库。

谢谢!

HTML

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
    <script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script> 
    <script src="jquery.validate.js"></script> 
    <script src="jquery.form.js"></script> 
    <script> 
     $(document).ready(function(){ 
      $('#addForm').validate(); 
      function addRecord() { 
       $("#aTable").hide('slow', function() { //this is not working 
        alert('Working on it.'); 
       }); 
       $("#tableText").hide('slow', function() {//this is not working 
         alert('Working on it.'); 
       }); 
       var output = document.getElementById("message"); 
       var nAname = document.getElementById("aname"); 
       var nAInfo = new FormData(document.forms.namedItem("addForm")); 
       nAInfo.append('aname', nAname); 

       $.ajax({ 
       type: "POST", 
       url: "addPhoto.php", 
       data: nAInfo 
      }); 
     }); 
    </script> 
</head> 
<body> 

<form id="addForm" name="addForm" onsubmit="addRecord()" enctype="multipart/form-data"> 
    <label>Name: </label> 
    <input type="text" id="aname" name="aname" class=required/> 
    <br> 
    <br> 
    <label>Photo: </label> 
    <input type="file" id="aimage" name="aimage" class="required"> 
    <br> 
    <br> 
    <input type="submit" value="ADD" /> 
    <br> 
</form> 

<div id="message" name="message"></div> 
<br> 
<br> 
<div id="image_display"></div> 
</body> 
</html> 

PHP

<?php 
ini_set('display_errors', 'On'); 
ini_set('display_startup_errors', 'On'); 
error_reporting(E_ALL); 

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); 

if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error; 
} 

if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 

     echo $_SERVER['REQUEST_METHOD']; 

    $aname = $_POST['aname']; 
    $errorinfo = $_FILES["aimage"]["error"]; 
    $filename = $_FILES["aimage"]["name"]; 
    $tmpfile = $_FILES["aimage"]["tmp_name"]; 
    $filesize = $_FILES["aimage"]["size"]; 
    $filetype = $_FILES["aimage"]["type"]; 

    $fp = fopen($tmpfile, 'r'); 
    $imgContent = fread($fp, filesize($tmpfile)); 
    fclose($fp); 

    if (!($filetype == "image/jpeg" && $filesize > 0)) { 
     echo "Import of photo failed"; 
    } 

    if ($filetype == "image/jpeg" && $filesize > 0 && $filesize < 1048576) { 

     if (!($stmt=$mysqli->prepare("INSERT INTO actor_info (aname, aimage_data) VALUES (?,?)"))) { 
      echo "Prepare failed: (" . $mysqli->errno . ")" . $mysqli->error; 
     } 

     if (!$stmt->bind_param("ss", $aname, $imgContent)) { 
      echo "Binding parameters failed: (" . $stmt->errno .") " . $stmt->error; 
     } 

     if (!$stmt->execute()) { 
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
     } 
    $stmt->close(); 
    } 
    else { 
     echo "Image must be under 1 MB"; 
    } 

echo mysqli_error(); 
mysqli_close($mysqli); 
?> 
+1

停止。从这个代码退后一步。看看它有多疯狂。你正在使用jquery ...但实现你的** OWN ** ajax代码? **为什么**? jquery内置了精彩和简单的ajax接口:http://api.jquery.com/jQuery.post/ – 2013-02-20 17:35:36

+0

同样,如果你的表单参数出现在url中,那么你可能不会做POST,这很可能是一个GET。你可以通过在你的php代码中执行'echo $ _SERVER ['REQUEST_METHOD']'来进行简单的调试。你的文件处理代码很残酷。 'addslashes()'是如此蹩脚,它甚至不希望被称为残暴。 **从不**使用它,特别是在数据库代码中。 **从不*假设文件上传成功。 ** **从来没有在$ _FILES – 2013-02-20 17:39:18

+0

@Marc B I已经尝试没有成功$就以下信任数据({ 类型: “POST”, 网址: “addActorPhoto.php”, 数据:nAInfo }) ; – user1852050 2013-02-20 17:44:41

回答

0
$("#aTable").hide('slow', function() { //this is not working 
      alert('Working on it.'); 
}); 

$("#tableText").hide('slow', function() {//this is not working 
      alert('Working on it.'); 
}); 

上面的代码不工作,因为你没有用ID的任何元素 '#aTable' 或“# tableText'在你的html代码中。

尝试print_r($ _POST)以查看所有值是否到达服务器。

同样使用jQuery的Ajax的功能将会使你的代码更容易...下面是一个示例

$.ajax({ 
     url : $domain + "/index/email/" + "?" + $arguments, //add your args here... 
     cache : false, 
     beforeSend : function(){ 
      alert('sending....'); 
     }, 
     complete : function($response, $status){ 
      if ($status != "error" && $status != "timeout") { 
       if($response.responseText == "200"){ 
     alert('done'); 
       } else { 
     alert($response.responseText); 
       } 
      } 
     }, 
     error : function ($responseObj){ 
      alert("Something went wrong while processing your request.\n\nError => " 
       + $responseObj.responseText); 
     } 
    }); 
+0

原谅我的新手,但是'$ status'是我必须定义的或者是服务器的响应,而且没有其他需要做的事情?响应函数作为将返回的消息放在指定位置的变量,即'var response = document.getElementById(“message”);'? – user1852050 2013-02-20 18:02:58

+0

在我的代码中有这些id的元素,但为了简洁起见,我没有发布它们。 – user1852050 2013-02-20 18:04:36

+0

您不必定义$状态它会自动返回。此外,响应将在$ response对象中返回,因此您可以简单地将响应称为$ response。responseText并将其加载到你的div中,像这样$('#message')。html($ response.responseText); – 2013-02-20 18:10:10