从< a>发送变量到JavaScript,从JavaScript到<表单值> [英] sending variables from <a> to javascript, from javascript to <form value>
问题描述
我有以下代码:
code1:
< div name =content>
< a id =various3href =picture.phptitle =<?php echo $ info;?>idno =5>
< img class =lastsrc =./ images / page11.png/>
< / a>
< / div>
code2:
< script language =javascripttype =text / javascript>
$(document).ready(function(){
var imagelinks = $('div [name = content] a');
idno = imagelinks.attr('idno');
document.getElementById(pid)。value = idno;
< / script>
code3:
< form id =targetmethod =getaction =picture.php ?=>
< input id =pidname =pidvalue =/>
code4:
<?php
$ host =' localhost';
$ user ='root';
$ pw ='';
$ db ='thepillar';
$ phpVar = $ _GET [pid];
$ b $ mysql_connect($ host,$ user,$ pw);
mysql_select_db($ db);
$ sql =select pics,ext from infopics where id ='$ phpVar '';
$ result = mysql_query($ sql)或者死('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和然后在代码1中将idno属性设置为其值。
idno = imagelinks.attr('idno');
下一页
document.getElementById(pid)。value = idno;
这段代码将javascript变量idno设置为我的代码中输入id =pid的值3 。结果一个文本框出现在我的index.php包含我的code3的值。
。我想要做的是将pid值传递给我的图片中的code4 .php并将其作为$ phpVar的值存储
$ phpVar = $ _GET [pid];
当我点击代码1内的标签而不是使用。
$首先, $(document).ready
中的jQuery可以被写成更简单的样子,就像所以:
$('#pid')。val($('div [name = content] a')。attr ('证件号码'));
如果您想将它发送给php,您可以添加如下内容:
$($#$ b
$('#various3 img')。click(function(){
$('#target')。submit() ;
});
点击图片时将提交表单。
我还会介绍有关SQL消毒的评论者的建议,而不是使用任意HTML属性。
I have the following codes:
code1:
<div name="content">
<a id="various3" href="picture.php" title="<?php echo $info; ?> " idno="5" >
<img class="last" src="./images/page11.png" />
</a>
</div>
code2:
<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>
code3:
<form id="target" method="get" action="picture.php?=">
<input id="pid" name="pid" value="" />
code4:
<?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, and code3 are on the same page index.php, while code4 is on another page picture.php.
the flow that i want to have is that when index.php loads code2 assigns every tag inside as imagelinks, and then declare a javascript variable idno and then sets the idno attribute in my tag in code1 as its value.
idno = imagelinks.attr('idno');
next up
document.getElementById("pid").value = idno;
this code sets the javascript variable idno as the value for the input id="pid" in my code3. as a result a textbox appears on my index.php containing the value from my code3.
.what i want to do is to pass the pid value to code4 in my picture.php and store it as the value of $phpVar
$phpVar = $_GET["pid"];
and it is performed when i click the tag inside code1 instead of using a .
Firstly, the jQuery inside $(document).ready
could be written more simply like so:
$('#pid').val($('div[name=content] a').attr('idno'));
If you then want to send that to the php, you could add something like:
$('#various3 img').click(function(){
$('#target').submit();
});
Which will submit the form when the image is clicked.
I would also second the commenter's advice regarding SQL sanitizing and not using arbitrary HTML attributes.
这篇关于从< a>发送变量到JavaScript,从JavaScript到<表单值>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!