之前我在这里发布了问题,但我现在找到了解决方案我现在有奇怪的问题我使用FormData()并使用ajax将其传递到另一个php页面,但我的所有变量都是未定义的,即使它在范围内但仍然是我我在这里得到这个错误是我的PHP代码
<?php
session_start();
if(isset($_POST['recaptcha'])){
$secret = "**********";
$response = $_POST['recaptcha'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
$content = json_decode($url, TRUE);
if($content['success'] ==1){
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = strtolower($data);
return $data;
}
$discount = test_input($_POST["discount"]);
$discountitem = test_input($_POST["discountitem"]);
$website = test_input($_POST["website"]);
$expirydate = test_input($_POST["expirydate"]);
$desc = test_input($_POST["desc"]);
$filename;
if(isset($_FILES['logouploader']['name'])){
$filename = basename($_FILES['logouploader']['name']);
$filename = test_input($filename);
}
$dir = "img/uploads/";
$ext = strtolower(pathinfo($_FILES['logouploader']['name'], PATHINFO_EXTENSION));
$allowed = array('jpeg','png' ,'jpg');
if(!in_array($ext,$allowed) ) {
echo "wrongext";
$uploadOk = 0;
exit;
}
if ($_FILES["logouploader"]["size"] > 600000) {
echo "large";
$uploadOk = 0;
exit;
}
$uploadOk = 1;
if ($uploadOk == 0) {
echo "Sorry";
exit;
}
if ($uploadOk == 1) {
$emailfromdb = $_SESSION['email']; //getting session variable
function logoset (){
$servername = "localhost";
$username = "*****";
$password = "*******";
try {
$conn = new PDO("mysql:host=$servername;dbname=dineandparty_dineandparty", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$fkid = "SELECT id from dineOwnerUser WHERE email = ?";
$statement = $conn->prepare($fkid);
$statement -> execute (
array(
$emailfromdb
));
$ownerid = $statement->fetch(PDO::FETCH_ASSOC);
$query = "INSERT INTO webpromo (discount, dealitem, website, expirydate, description, ,logouploader, ownerid) VALUES (?, ?, ?, ?, ?, ?, ?)";
$statement = $conn->prepare($query);
$statement->execute(
array(
$discount, //undefined variable
$discountitem, //undefined variable
$website, //undefined variable
$expirydate, //undefined variable
$desc, //undefined variable
$filename, //undefined variable
$ownerid //undefined variable
) );
move_uploaded_file($_FILES["logouploader"]["tmp_name"], $dir.$filename);
$conn = null;
echo "done";
exit;
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
function nologoset() {
$servername = "localhost";
$username = "************";
$password = "*************";
try {
$conn = new PDO("mysql:host=$servername;dbname=dineandparty_dineandparty", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "INSERT INTO webpromo (discount, dealitem, website, expirydate, description, logouploader, ownerid) VALUES (?, ?, ?, ?, ?, ?, ?)";
$statement = $conn->prepare($query);
$statement->execute(
array(
$discount,
$discountitem,
$website,
$expirydate,
$desc,
$filename
) );
$conn = null;
echo "done";
exit;
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
if(isset($_FILES['logouploader']['name'])){
logoset();
}
if(!isset($_FILES['logouploader']['name'])){
nologoset();
}
}
}
if($content['success'] !=1){
echo "notok";
$conn = null;
exit;
}
$conn = null;
exit;
}
?>
我的jquery代码是为了防止您需要查看代码
$("#promotionsubmit").click(function(){
if((discounterr != "no") || (discountitemerr != "no") || (expirydateerr != "no") ||(descerr != "no")){
errbox = $("#robotError");
errcontent = "All * fields are mandatory";
showerror(errbox, errcontent);
}
if((discounterr == "no") && (discountitemerr == "no") && (expirydateerr =="no") && (descerr == "no")){
errbox = $("#robotError");
errcontent = " ";
hideerror(errbox, errcontent);
var fd = new FormData();
fd.append("discount",discount);
fd.append("discountitem",discountitem);
fd.append("website",website);
fd.append("expirydate",expirydate);
fd.append("desc",desc);
fd.append("logouploader",logouploader);
fd.append("recaptcha",grecaptcha.getResponse());
$.ajax({
url:"promotionform.php",
method:"POST",
data : fd,
contentType: false,
processData: false,
cache: false,
success: function(data){
if(data == "notok"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = "please verify you are not robot";
showerror(errbox, errcontent);
}
if(data == "large"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = "image is larger than 600 kb. Allowed size is 600 kb for logo";
showerror(errbox, errcontent);
}
if(data == "wrongext"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = "only jpg, jpeg and png are allowed for logo";
showerror(errbox, errcontent);
}
if(data == "notimage"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = "file is not an image";
showerror(errbox, errcontent);
}
if(data == "Sorry"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = "Sorry, your file was not uploaded.";
showerror(errbox, errcontent);
}
if(data == "done"){
grecaptcha.reset();
errbox = $("#robotError");
errcontent = " ";
hideerror(errbox, errcontent);
window.location='http://domainname.com/';
}
}
});
}
});
我已经多次检查了我的编码,但我真的不知道为什么我的php变量未定义。 提前致谢
答案 0 :(得分:0)
你可能会混淆使用PHP的Javascript范围。您抱怨的变量在logoset
函数的范围内不,因为它们是先前声明的,是的,但在外面。
要让他们进入范围,您需要使用 use
来吸引他们:
$logoset = function() use ($discount, $discountitem, ...) {
或声明他们global
(不推荐 - 谷歌&#34; 污染全球范围&#34;)。
function logoset () {
global $discount, $discountitem, ...
你可以像这样尝试范围界定:
$iAmGlobal = true;
function test() {
$iAmLocal = true;
$used = true;
$inside = function() use ($used, $usedAfter) {
print_r(get_defined_vars());
};
$usedAfter = true;
call_user_func($inside);
}
test();
结果是:
PHP Notice: Undefined variable: usedAfter on line 9
Array
(
[used] => 1
[usedAfter] =>
)
因此,必须已经定义了使用子句中的变量,如果不是,则通过强制定义 - 但它是空的。
$iAmGlobal
变量不可见,$iAmLocal
也不可见。但是如果你将global $iAmGlobal;
放在内部函数中,那么你会看到$iAmGlobal
变得可见。使用$iAmLocal
执行此操作不起作用(因为该变量不是全局变量),除非您在test()
内强制使用第二个全局声明。