[英]PHP action form embed in 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";
如何使该链接可点击?
谢谢!
检查标题:
你有
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>';
试试吧
<?php
header('content-type:text / html; 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 '<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.";
}
}
?>
您的问题似乎已经回答了自己。 您所要做的就是将链接包装在Anchor Tag中,就像这样
<?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";
//THIS IS THE ONLY PLACE YOU NEED TO ADDRESS: WRAP URL IN <A> TAGS:
echo "<a href='http://localhost/public_html/PAD/index.php?user={$file_name}' class='view-data-link'>View your data</a>";
fclose($sqlfile);
} else {
echo "Sorry, there was an error uploading your file.";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.