Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何让bookmarklet弹出一个表单,向DB plus comments提交URL和标题_Php_Javascript_Forms_Bookmarklet - Fatal编程技术网

Php 如何让bookmarklet弹出一个表单,向DB plus comments提交URL和标题

Php 如何让bookmarklet弹出一个表单,向DB plus comments提交URL和标题,php,javascript,forms,bookmarklet,Php,Javascript,Forms,Bookmarklet,我实现了一个bookmarklet,当单击它时,它会将URL和标题发送到DB。我想首先弹出一个表单,它有一个用于添加注释的字段、一个用于编辑页面标题的字段和一个用于选择从DB表填充的类别的下拉字段 我仍然在学习php,几乎没有任何JS技能。我不知道如何让bookmarklet调用将提交到DB的pop表单,或者将页面URL和标题传递给表单 到目前为止,我使用的代码是从这里开始的-> Bookmarklet代码: (function () { var jsScript = document.cr

我实现了一个bookmarklet,当单击它时,它会将URL和标题发送到DB。我想首先弹出一个表单,它有一个用于添加注释的字段、一个用于编辑页面标题的字段和一个用于选择从DB表填充的类别的下拉字段

我仍然在学习php,几乎没有任何JS技能。我不知道如何让bookmarklet调用将提交到DB的pop表单,或者将页面URL和标题传递给表单

到目前为止,我使用的代码是从这里开始的->

Bookmarklet代码:

(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.'");';

?>
//错误报告:
错误报告(E_全部^E_通知);
需要“connect.php”;
需要“functions.php”;
//将内容类型标题设置为javascript:
标题('Content-type:application/javascript');
//验证输入数据
如果(空($_GET['url'])空($_GET['title'])空($_GET['url'])死();
//清理变量
$\u GET['url']=清理($\u GET['url']);
$\u GET['title']=清理($\u GET['title']);
//插入时,请注意哈希字段和md5函数的使用:
mysql_查询(“插入书签_应用程序(哈希、url、标题)
价值观(
“.md5($\u GET['url'])。”,
“$\u获取['url']”,
“$_获得['title']””
)");
$message='';
如果(mysql受影响的行($link)!=1)
{
$message='此URL已存在于数据库中!';
}
其他的
$message='该URL已共享!';
?>
/*JavaScript代码*/
函数显示消息(str)
{
//使用纯JavaScript创建和设置div元素的样式
var d=document.createElement('div');
(d.风格)
{
//应用样式:
位置='固定';
宽度='350px';
高度='20px';
top=‘50%’;
左='50%';
保证金='-30px0-195px';
背景颜色='#f7f7f7';
边框='1px实心#ccc';
颜色='#777';
填充='20px';
fontSize='18px';
fontFamily=““Myriad Pro”,Arial,Helvetica,无衬线字体”;
textAlign='中心';
zIndex=100000;
textShadow='1px 1px 0白色';
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)');
//添加作为文本传递给函数的消息:
d、 appendChild(document.createTextNode(str));
//将div追加到文档
文件.正文.附件(d);
//消息将在3秒内自动隐藏:
setTimeout(函数(){
试一试{
文件.body.removeChild(d);
}捕获(错误){}
},3000);
}
Functions.php

<?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的东西,但我只需要帮助将bookmarklet链接到提交到db的弹出表单


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

您只需将HTML表单附加到bookmarklet(javascript)中即可

var HTML='';
文件.appendChild(HTML);


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

请发布完整的bookmarklet代码。我明白了,您使用get作为反馈。起初我想让bookmarklet生成表单(发布数据而不是get),但这确实会破坏反馈。因此,您必须将bookmark.php更改为分两个阶段运行
if(哈希存在于db中){alert url已经共享;}else if(form submit){向db添加条目;alert url共享;}else{display form;}
对于表单提交,可以使用json,或者插入另一个带有geturl和submit变量的javascript。这就是你想要的吗?我明白你的意思,但我现在没有足够的JS来编写表单和“if”语句。上面的代码是我从网上的一个教程中获得的,因为它是教我想做什么的最接近的例子。我想我需要一个简短的“如果”语句,它只会带来一个字段形式,我可以扩展它。我讨厌学习一种语言,需要了解其他人才能完成我的工作,但这是学习过程的一部分。我很快就了解到,JS可能是我下一个真正需要学习的技能,而不是php。你不需要表单。只有两个输入字段、一个文本区域和一个选择框(从db填充)。但是javascript必须用db中的值填充selectbox。我已经有了一些关于这个问题的片段(但它们还不能构成一个完整的答案),但这个问题实际上相当复杂。您需要的工具有javascript、html、php、sql,可能还有一些json。而且,当前代码没有用于注释和“category”(我猜)的数据库字段。还有很多空白要填补。“这正是我要找的!你能做到吗?