简体   繁体   English

想要将php变量作为参数传递给javascript函数吗?

[英]Want to pass php variable as argument in javascript function?


i want to pass my php variable in one javascript function. 我想在一个JavaScript函数中传递我的php变量。
i know it seems simple but i don't know where am i missing something? 我知道这似乎很简单,但我不知道我在哪里缺少什么?

    <?php
        while($gg=mysql_fetch_array($lg))
        {
    ?>
        <td id="add_td">
        <?php
        $id = $gg['p_id'];
        echo "<a onclick=cnf('Are you sure you want to delete that?',$id)>"; ?>Delete</a>
        </td>
<?php
        }
?>

and in my javascript function 在我的javascript函数中

function cnf(msg,id)
{

     cnf = confirm(msg);
     if(cnf) { 
            parent.location.href = 'p_update.php?d=' + id;          
     }
}

so i need to know on which id that user had clicked so that i will only delete that id from database. 所以我需要知道用户单击了哪个ID,以便我只会从数据库中删除该ID。
if i try this thing then it showing error on "cnf" function and its saying like "unterminated string literal"? 如果我尝试这个东西,那么它在“ cnf”函数上显示错误,并且像“未终止的字符串文字”这样说?

if $id is not numeric you should write 

<?php
        while($gg=mysql_fetch_array($lg))
        {
    ?>
        <td id="add_td">
        <?php
        $id = $gg['p_id'];
        echo "<a onclick=cnf('Are you sure you want to delete that?','".$id."')>"; ?>Delete</a>
        </td>
<?php
        }
?>

Check your syntax 检查语法

<?php
    while($gg=mysql_fetch_array($lg))
    {
?>
        <td id="add_td">
        <?php
        $id = $gg['p_id'];
        ?>
        <a onclick="cnf('Are you sure you want to delete that?',<?=$id?>);">Delete</a>
        </td>
<?php
     }
?>

I would do something like this instead. 我会做这样的事情。 HREF is mandatory if you want the "hand" pointer A unique ID is also mandatory on tags and you need to quote the ID if you pass it in the function instead of what I suggest and give the link the id 如果需要“手”指针,则必须使用HREF。标记上还必须具有唯一ID,并且如果在函数中传递它(而不是我建议的值)并给链接提供ID,则必须引用ID。

function cnf(link,id) {
  if (confirm("Are you sure you want to delete "+id) {
    link.href = "p_update.php?d=" + id;          
    return true;
  }
  return false;
}

<?php
  while($gg=mysql_fetch_array($lg)) { 
    $id = $gg['p_id'];
?>
  <td id="add_td<?php echo $id; ?>"><a target="_parent" href="#" id="<?php echo $id; ?>" onclick="return cnf(this)">Delete</a></td>
<?php } ?>

Use quotes around php variable '$id'. 在php变量'$ id'周围使用引号。

echo "<a onclick=cnf('Are you sure you want to delete that?','$id')>";

Another example, $p is some php variable, 另一个例子, $p是一些php变量,

echo "<input type=button value='update' onclick=myfunc('$p')>"
foreach($mas as $k=>$v) {
  //echo $k.' = '.$v.'<br>';
  echo("

    <input type=\"button\" id=\"delete_id".$k."\" value=\"Blablabla\" onclick=\"alert('".$v."');\"/>

  ");
}

If I put there $k (index) it works but if string (value is string), I get an error 如果我把$ k(索引)放在那里,但是如果字符串(值是字符串),我会报错

unterminated string literal 未终止的字符串文字

In HTML tags this works but not as the function argument! 在HTML标记中,此方法有效,但不能用作function参数!

如果$ id是字符串文字,则在将其作为参数传递给<a ...>标记中的cnf()函数时,应将其放在引号中:

echo "<a onclick=cnf('Are you sure you want to delete that?', '$id')>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM