PHP动作表单嵌入HTML

时间:2016-05-02 12:01:35

标签: php html

我的表单操作页面是PHP,但我希望回显一个可点击的链接,我认为PHP不支持这一点。我已经将这个PHP代码嵌入到HTML标记中,但它回显了页面中的标记。

代码:

<?php
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");

include 'connect.php';

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Allow certain file formats
if($imageFileType != "sql") {
    echo "Sorry, only SQL files are allowed.\n";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.\n";
        $sqlfile = fopen($target_file, "r") or die("Unable to open file!");
        $contents = file($target_file);
        foreach($contents as $line) {
            $result = mysql_query($line) or die('Query failed: ' . mysql_error());
            $file_name= str_replace("'", "", basename($target_file,".sql"));


        }


        echo "The new data was inserted in database.\n";
        echo "View your data in http://localhost/public_html/PAD/index.php?user=$file_name";
        fclose($sqlfile);

    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}


?>

问题在于:

echo "View your data in http://localhost/public_html/PAD/index.php?user=$file_name";

如何才能点击此链接?

谢谢!

3 个答案:

答案 0 :(得分:4)

检查标题:

你有

header('content-type: application/json; charset=utf-8');

application/json标头无法返回HTML。他们将返回JSON字符串。在JSON中,标签将按原样返回,它们不会被渲染。

你需要:

header('content-type: text/html; charset=utf-8');

然后,当您希望显示链接时,将链接括在A标记中:

echo 'View your data in <a href="http://localhost/public_html/PAD/index.php?user='.$file_name.'">http://localhost/public_html/PAD/index.php?user='.$file_name.'</a>';

答案 1 :(得分:0)

试试吧

<?php

标题(&#39; content-type:text / html; charset = utf-8&#39;);     标题(&#34; access-control-allow-origin:*&#34;);

include 'connect.php';

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Allow certain file formats
if($imageFileType != "sql") {
    echo "Sorry, only SQL files are allowed.\n";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.\n";
        $sqlfile = fopen($target_file, "r") or die("Unable to open file!");
        $contents = file($target_file);
        foreach($contents as $line) {
            $result = mysql_query($line) or die('Query failed: ' . mysql_error());
            $file_name= str_replace("'", "", basename($target_file,".sql"));


        }


        echo "The new data was inserted in database.\n";
        echo '<a href="http://localhost/public_html/PAD/index.php?user=$file_name">View your data in </a>';
        fclose($sqlfile);

    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}


?>

答案 2 :(得分:0)

你的问题似乎已经回答了。您所要做的就是将链接包装在Anchor Tag中,如此

aBar.getTemporaryObject2()

}