如何从更改页面停止提交按钮?

时间:2016-06-10 16:26:38

标签: php wordpress woocommerce

我的单个产品页面上有一个表单,用于提交带有订单的图像。当按下按钮时,它应该上传一个图像,但它会一直导航到“找不到页面”页面。这就是它的样子:

function add_checkout_notice() {
     echo '<form action="accept-file.php" method="POST" enctype="multipart/form-data">';
        echo    '<input type="file" name="photo[]" id="photoUpload" />';
        echo    '<input type="submit" name="submitPhoto" value="Submit">';
        //echo    '<input type="text" name="test_field" value="stuff">';
     echo '</form>';

}
add_action( 'woocommerce_single_product_summary', 'add_checkout_notice');

accept-file中的代码如下所示:

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["photo"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$testLog = fopen(plugin_dir_path( __FILE__ )."log.txt","w") or exit ("Unable to open file!");
fwrite ($testLog ,pathinfo($target_file,PATHINFO_EXTENSION));
fwrite ($testLog ,$_FILES["photo"]["name"]);
fwrite ($testLog ,$target_dir);

// Check if image file is a actual image or fake image
if(isset($_POST["submitPhoto"])) {

    fwrite ($testLog ,"set");
    $check = getimagesize($_FILES["photo"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
    fwrite ($testLog ,"is image");
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
    fwrite ($testLog ,"not an image");
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    fwrite ($testLog ,"already exists");
    $uploadOk = 0;
}

任何建议?

1 个答案:

答案 0 :(得分:0)

如果您的accept_file.php代码根本没有运行,那么这意味着您需要更改表单上的ACTION属性,以确保它实际指向服务器上的正确文件。你现在正在使用相对路径,这可能需要是绝对的(以/)开头。

如果您不希望浏览器在提交表单时执行默认操作(即“更改页面”),则需要使用javascript发布表单并使用{{3}停止默认表单行为}。

相关问题