如何得到一个书签弹出一个表单,提交URL和标题的DB加上评论


How to get a bookmarklet to pop up a form that submits URL and Title to DB plus comments

我有一个bookmarklet实现,当点击发送URL和标题到数据库。我想一个表单首先弹出有一个字段来添加评论,一个字段用于编辑页面标题,以及一个下拉字段,用于选择从DB表填充的类别。

我还在学习php,几乎没有任何JS技能。我不知道如何让bookmarklet调用将提交到数据库或传递页面URL和标题到表单的弹出表单。

到目前为止我使用的代码来自这里-> http://tutorialzine.com/2010/04/simple-bookmarking-app-php-javascript-mysql/

书签代码:

(function () {
  var jsScript = document.createElement('script');
  jsScript.setAttribute('type', 'text/javascript');
  jsScript.setAttribute('src', '/bookmark.php?url=' + encodeURIComponent(location.href)
  + '&title=' + encodeURIComponent(document.title));
document.getElementsByTagName('head')[0].appendChild(jsScript);
})();

Bookmark.php代码:

// Error reporting:
error_reporting(E_ALL^E_NOTICE);
require "connect.php";
require "functions.php";
// Setting the content-type header to javascript:
header('Content-type: application/javascript');
// Validating the input data
if(empty($_GET['url']) || empty($_GET['title']) || !validateURL($_GET['url'])) die();
// Sanitizing the variables
$_GET['url'] = sanitize($_GET['url']);
$_GET['title'] = sanitize($_GET['title']);
// Inserting, notice the use of the hash field and the md5 function:
mysql_query("   INSERT INTO bookmark_app (hash,url,title)
                VALUES (
                    '".md5($_GET['url'])."',
                    '".$_GET['url']."',
                    '".$_GET['title']."'
                )");
$message = '';
if(mysql_affected_rows($link)!=1)
{
    $message = 'This URL already exists in the database!';
}
else
$message = 'The URL was shared!';

?>
/* JavaScript Code */
function displayMessage(str)
{
    // Using pure JavaScript to create and style a div element
    var d = document.createElement('div');
    with(d.style)
    {
        // Applying styles:
        position='fixed';
        width = '350px';
        height = '20px';
        top = '50%';
        left = '50%';
        margin = '-30px 0 0 -195px';
        backgroundColor = '#f7f7f7';
        border = '1px solid #ccc';
        color = '#777';
        padding = '20px';
        fontSize = '18px';
        fontFamily = '"Myriad Pro",Arial,Helvetica,sans-serif';
        textAlign = 'center';
        zIndex = 100000;
        textShadow = '1px 1px 0 white';
        MozBorderRadius = "12px";
        webkitBorderRadius = "12px";
        borderRadius = "12px";
        MozBoxShadow = '0 0 6px #ccc';
        webkitBoxShadow = '0 0 6px #ccc';
        boxShadow = '0 0 6px #ccc';
    }
    d.setAttribute('onclick','document.body.removeChild(this)');
    // Adding the message passed to the function as text:
    d.appendChild(document.createTextNode(str));
    // Appending the div to document
    document.body.appendChild(d);
    // The message will auto-hide in 3 seconds:
    setTimeout(function(){
        try{
            document.body.removeChild(d);
        }   catch(error){}
    },3000);
}
<?php 
// Adding a line that will call the JavaScript function:
echo 'displayMessage("'.$message.'");';
?>

显然也
<?php
/* Helper functions */
function validateURL($str)
{
    return preg_match('/(http|ftp|https):'/'/['w'-_]+('.['w'-_]+)+(['w'-'.,@?^=%&amp;:'/~'+#]*['w'-'@?^=%&amp;'/~'+#])?/i',$str);
}
function sanitize($str)
{
    if(ini_get('magic_quotes_gpc'))
        $str = stripslashes($str);
    $str = strip_tags($str);
    $str = trim($str);
    $str = htmlspecialchars($str);
    $str = mysql_real_escape_string($str);
    return $str;
}

function relativeTime($dt,$precision=2)
{
    if(is_string($dt)) $dt = strtotime($dt);
    $times=array(   365*24*60*60    => "year",
                    30*24*60*60     => "month",
                    7*24*60*60      => "week",
                    24*60*60        => "day",
                    60*60           => "hour",
                    60              => "minute",
                    1               => "second");
    $passed=time()-$dt;
    if($passed<5)
    {
        $output='less than 5 seconds ago';
    }
    else
    {
        $output=array();
        $exit=0;
        foreach($times as $period=>$name)
        {
            if($exit>=$precision || ($exit>0 && $period<60)) break;
            $result = floor($passed/$period);
            if($result>0)
            {
                $output[]=$result.' '.$name.($result==1?'':'s');
                $passed-=$result*$period;
                $exit++;
            }
            else if($exit>0) $exit++;
        }
        $output=implode(' and ',$output).' ago';
    }
    return $output;
}
// Defining fallback functions for mb_substr and 
// mb_strlen if the mb extension is not installed:
if(!function_exists('mb_substr'))
{
    function mb_substr($str,$start,$length,$encoding)
    {
        return substr($str,$start,$length);
    }
}
if(!function_exists('mb_strlen'))
{
    function mb_strlen($str,$encoding)
    {
        return strlen($str);
    }
}
?>

我想要的最终结果是像Bitly bookmarklet, https://bitly.com/a/tools这样的东西,但我只需要帮助将bookmarklet链接到提交给数据库的弹出表单。

我试着四处看看其他人的问题,但我找不到任何适用于使用弹出式表单提交到数据库。

你可以把HTML表单附加到书签中(javascript)。

var HTML = '<form action=""><input type="text" name="title" /></form>'; document.appendChild(HTML);

这不是一个完整的代码,只是一个可以帮助您完成工作的原则示例。