My form action page is PHP but I want to echo a clickable link and I think PHP doesn´t support this. I've embed this PHP code in HTML tags but it echos the tags in the page.
Code:
<?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.
";
$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.
";
$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.
";
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.";
}
}
?>
The problem is here:
echo "View your data in http://localhost/public_html/PAD/index.php?user=$file_name";
How can I make this link clickable?
Thanks!
Your Question seems to have answered itself. All you have to do is just wrap the link in an Anchor Tag like so
<?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.
";
$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.
";
$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.
";
//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.";
}
}
Check your headers:
You have
header('content-type: application/json; charset=utf-8');
application/json
headers cannot return HTML
. They will return a JSON
string. In JSON the tags will be returned as they are, they won't be rendered.
You need:
header('content-type: text/html; charset=utf-8');
And then, when you wish to display the link enclose the link in an A
tag:
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>';
Try it
<?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.
";
$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.
";
$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.
";
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.";
}
}
?>