无法在脚本函数中传递php变量
i am creating two text areas with id's-id1$i(when,$i=1) and id1$i(when,$i=2)..what i want is that as soon as user changes the text of any of these text areas it should display that text in an alert box.Here is my code:
<?php
$i=1;
while($i)
{
echo '<textarea id="id1$i" onchange=Display("id1$i")></textarea>';
echo "id1$i..<br>";
if($i==2){break;}
$i=2;
}
?>
<input name="Submit" type="submit" value="Submit"/>
<script>
function Display(id1$i)
{
alert(document.getElementById("id1$i").value);
}
</script>
Now when i try running this...the problem is when i enter "one" in the first text area box,it displays "one",that's ok.....but when i enter "two" in the second text area box,it still displays "one"...:mad::mad::mad:...
The only reason dat i can think of why it's hapning lyk dat is the function Display(id1$i) is not taking the value of $i=2...please help!!..
If i use proper names like "id11" and "id12"(i.e i dont use the variable $i anywhere in my code or say if i harcode everythng)..then the code works fine..it displays both "one" "two",respectively..
我正在创建ids-id1 $ i(when,$ i = 1)和id1 $ i的两个文本区域 (当,$ i = 2)..我想要的是,一旦用户更改任何这些文本区域的文本,它应该在警告框中显示该文本。这是我的代码: p> \ n
&lt;?php
$ i = 1;
while($ i)
{
echo'&lt; textarea id =“id1 $ i”onchange = Display(“id1 $ i” )&gt;&lt; / textarea&gt;';
echo“id1 $ i ..&lt; br&gt;”;
if($ i == 2){break;}
$ i = 2;
} \ n?&gt;
&lt;输入名称=“提交”类型=“提交”值=“提交”/&gt;
&lt; script&gt;
功能显示(id1 $ i)
{
alert(document.getElementById( “id1 $ i”)。value);
}
&lt; / script&gt;
code> pre>
h2>
现在当我 尝试运行这个...问题是当我在第一个文本区域框中输入“one”时,它显示“one”,这没关系.....但是当我在第二个文本区域框中输入“two”时,它 仍然显示“一个”...:mad :: mad :: mad:... p>
我能想到的唯一原因是wh 你正在解决lyk dat是函数Display(id1 $ i)没有取$ i = 2的值...请帮助!! .. p>
如果我使用正确的名字,如 “id11”和“id12”(即我不在我的代码中的任何地方使用变量$ i或者说如果我对每个东西进行编码)..然后代码工作正常..它分别显示“一个”“两个”..
div>
You can't embed PHP like that. You'll need to use the <?php ?>
tags, therefore you'll need to change:
function Display(id1$i)
// ...
alert(document.getElementById("id1$i").value);
to:
function Display(id1<?php echo $i; ?>)
// ...
alert(document.getElementById("id1<?php echo $i; ?>").value);
And to use variables inside strings directly you can only use "
therefore you can do one of the following:
'<textarea id="id1'. $i .'" onchange=Display("id1'. $i .'")></textarea>'
// or
"<textarea id=\"id1$i\" onchange=Display(\"id1$i\")></textarea>"