【问题标题】:sending variables from <a> to javascript, from javascript to <form value>将变量从 <a> 发送到 javascript,从 javascript 发送到 <form value>
【发布时间】:2011-03-03 19:56:26
【问题描述】:

我有以下代码:

代码1:

<div name="content">
    <a id="various3" href="picture.php" title="<?php echo $info; ?> " idno="5" >
        <img class="last" src="./images/page11.png" />
    </a>
</div>

代码2:

<script language="javascript" type="text/javascript">
        $(document).ready(function() {
        var imagelinks = $('div[name=content] a');
        idno = imagelinks.attr('idno');
        document.getElementById("pid").value = idno;
</script>

代码3:

<form id="target" method="get" action="picture.php?=">
    <input id="pid" name="pid" value="" />

代码4:

<?php

$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'thepillar';
$phpVar = $_GET["pid"];

mysql_connect($host,$user,$pw); 
mysql_select_db($db); 
$sql = "select pics, ext from infopics where id='$phpVar'"; 
$result = mysql_query($sql) or die('Bad query at 12!'.mysql_error()); 
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
    $db_img = $row['pics'];
    $type = $row['ext'];
} 
$img = base64_decode($db_img); //print_r($db_img );
$img = imagecreatefromstring($img);
header("Content-Type: image/jpeg");
imagejpeg($img);
imagedestroy($img);

?>

.code1、code2、code3在同一个页面index.php,code4在另一个页面picture.php。

我想要的流程是,当 index.php 加载 code2 时,将内部的每个标记分配为 imagelinks,然后声明一个 javascript 变量 idno,然后将 code1 中我的标记中的 idno 属性设置为其值。

idno = imagelinks.attr('idno');

接下来

document.getElementById("pid").value = idno;

此代码将 javascript 变量 idno 设置为我的 code3 中输入 id="pid" 的值。结果,我的 index.php 上出现了一个文本框,其中包含我的 code3 中的值。

。我要做的是将pid值传递给我的picture.php中的code4并将其存储为$phpVar的值

$phpVar = $_GET["pid"];

当我点击 code1 中的标签而不是使用 .

【问题讨论】:

  • 这篇文章是stackoverflow.com/questions/5160247/… 的延续吗?为什么不编辑第一个,因为它没有解决?
  • 另外,向 html 元素添加不存在的属性 (idno) 在很多浏览器中都不起作用,您不应该这样做。
  • 你有一个 SQL 注入漏洞。切勿在 SQL 查询中使用用户可以更改的变量(例如 $_GET)。请改用 PDO,或者如果您不能使用它,请至少在您的 $sql 上使用 mysql_real_escape_string(),然后再使用 mysql_query()
  • @VirtualBlackFox - 是的。我很抱歉。我现在很困惑,因为这个项目是为了我的论文。 T_T
  • .@itchy - 我使用了 mysql_real_escape_string() 但仍然没有任何变化。

标签: php javascript jquery html


【解决方案1】:

首先,$(document).ready 内部的 jQuery 可以更简单地写成这样:

$('#pid').val($('div[name=content] a').attr('idno'));

如果你想把它发送到 php,你可以添加如下内容:

$('#various3 img').click(function(){
    $('#target').submit();
});

点击图片时会提交表单。

我还赞同评论者关于 SQL 清理和不使用任意 HTML 属性的建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多