I have PHP function for URL shortening (like goo.gl or bit.ly) and it returns shortened link with echo. I want to put returned link into input without refreshing page (variable is in PHP).
I know it's a bit incomprehensible so:
- user pastes URL in input
- PHP function shortens it and returns shortened URL
- shortened URL is displayed under input (I want to display this in input without refreshing)
My code:
<?php
function shortUrl($url)
{
$id = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6);
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("Y-m-d");
$hour = date("H:i:s");
$user = $_SESSION["user"];
require_once 'db.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Database error. (1)");
exit();
//die($conn->connect_error);
}
$sql = "INSERT INTO links (id, ip, user, link, date, hour) VALUES ('".$id."', '".$ip."', '".$user."', '".$url."', '".$date."', '".$hour."')";
if ($conn->query($sql) === TRUE)
{
echo 'http://blabla.ru/'.$id;
}
else
{
echo 'Connection error. (2)';
//echo $conn->error;
}
$conn->close();
}