如何得到一个书签弹出一个表单,提交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'-'.,@?^=%&:'/~'+#]*['w'-'@?^=%&'/~'+#])?/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);
这不是一个完整的代码,只是一个可以帮助您完成工作的原则示例。
相关文章:
- 创建一个不断变化的谷歌涂鸦风格的标题
- ESDoc将我所有的模块方法放在一个标题下,而不是放在每个文件下,我该如何改变这种行为
- 我该如何为video.js制作一个插件,在HTML标题中添加和删除三角形
- 如何使收音机显示为标题?另外,我如何让收音机决定下一个盒子
- 在另一个选项卡中打开同一域时,窗口标题会发生更改
- 使用jQuery链接到一个页面,然后跳转到一个特定的标题
- 为什么.attr('title')正在抓取前一个元素'的标题
- “index.swf”嵌入在“index.html”中,在一个页面中显示同一网站两次.HTML 标题标签/属性也显示值“
- Javascript 打开一个新窗口并更改标题文本
- 修复了标题导航和滚动到()下一个/上一个元素
- 钛应用器打开新窗口和上一个标题的动态后退按钮
- Twitter 引导程序:在开放手风琴标题中添加一个类
- 一个标题用于 HTML 中的多个页面
- 将一个标题替换为固定位置,将另一个标题位置替换为固定
- 给窗口打开弹出一个标题
- 如何设置<一个标题=“">用于erb
- Jquery Accordion幻灯片删除上一项时的下一个标题
- 添加一个标题到甜甜圈图
- 添加一个标题以弹出带有此代码的窗口---
- 在IE9的每个打印页面上都保留一个标题