doutang7414
2010-10-29 08:30
浏览 33
已采纳

想在javascript函数中传递php变量作为参数吗?


i want to pass my php variable in one javascript function.
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

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.
if i try this thing then it showing error on "cnf" function and its saying like "unterminated string literal"?

图片转代码服务由CSDN问答提供 功能建议


i想在一个javascript函数中传递我的php变量。
i知道它似乎 很简单,但我不知道我在哪里遗漏了什么?

 &lt;?php 
 while($ gg = mysql_fetch_array($ lg))
 {
?  &gt; 
&lt; td id =“add_td”&gt; 
&lt;?php 
 $ id = $ gg ['p_id']; 
 echo“&lt; a onclick = cnf('你确定要吗? 删除那个?',$ id)&gt;“;  ?&gt;删除&lt; / a&gt; 
&lt; / td&gt; 
&lt;?php 
} 
?&gt; 
   
 
 

以及我的javascript函数

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

所以我需要知道用户点击了哪个ID,这样我才会从数据库中删除该ID。
\ 如果我尝试这个东西然后它显示错误“cnf”函数和它的说法像“未终止的字符串文字”?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dongshan4878 2010-10-29 08:45
    已采纳
    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
            }
    ?>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsfhe34889789708 2010-10-29 08:34

    If $id is a string literal, you should put it into quotes when you are passing it as a parameter to cnf() function in <a ... > tag:

    echo "<a onclick=cnf('Are you sure you want to delete that?', '$id')>";
    
    评论
    解决 无用
    打赏 举报
  • doubinchou4219 2010-10-29 08:34

    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
         }
    ?>
    
    评论
    解决 无用
    打赏 举报
  • dongxiao9583 2010-10-29 08:46

    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

    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 } ?>
    
    评论
    解决 无用
    打赏 举报
  • donglong1465 2011-10-05 14:54
    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

    unterminated string literal

    In HTML tags this works but not as the function argument!

    评论
    解决 无用
    打赏 举报
  • dongyuan9292 2018-06-17 02:55

    Use quotes around php variable '$id'.

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

    Another example, $p is some php variable,

    echo "<input type=button value='update' onclick=myfunc('$p')>"
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题