1


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"?

Nitz
  • 1,690
  • 11
  • 36
  • 56
  • 1
    I can see errors which http://validator.w3.org/ will pick up (and when you have code generated by other code which breaks, you'll find it much easier to get an answer if you look at the generated code and try to narrow the question down to "Why doesn't this JavaScript work?" or "Why doesn't this PHP output this JavaScript?" instead of "Why doesn't the JavaScript generated by this PHP work?") – Quentin Oct 29 '10 at 08:47

6 Answers6

4
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
        }
?>
Eldar
  • 862
  • 9
  • 22
  • in this also cnf was showing error called unterminated string literal but i just put one thing in it...... echo "Delete"; – Nitz Oct 29 '10 at 08:54
2

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')>"
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Lagrange
  • 95
  • 7
1

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
     }
?>
Chinmayee G
  • 7,947
  • 2
  • 31
  • 41
  • Yeah, it should be '' it is php not asp – mplungjan Oct 29 '10 at 08:48
  • =$id?> is also valid php markup. http://codeigniter.com/user_guide/general/alternative_php.html for alternative echo markup in php – Chinmayee G Oct 29 '10 at 08:52
  • 1
    Short tags shouldn't be used because support is not guaranteed on shared servers and it's being removed completely next major version: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Coquevas Oct 29 '10 at 08:52
  • Too bad, I definitely prefer = id ?> to the one - never knew it existed – mplungjan Oct 29 '10 at 08:55
1

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 } ?>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1
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!

Alex
  • 11
  • 1
  • Offcourse I can make an invisible
    tag with some id and put in there $v as the value, and get it using getElementById. But maybe there is some right syntax to put a php variable into JS function??
    – Alex Oct 05 '11 at 15:24
  • This is not an answer to the original question and might be worthwhile having as a separate question. Please have a look at [this page](http://stackoverflow.com/questions/how-to-answer). – Serge Belov Nov 10 '12 at 06:53
0

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')>";
Kel
  • 7,680
  • 3
  • 29
  • 39