tags:

views:

37

answers:

4

Hi friends,
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"?

A: 

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
same error on cnf called unterminated string literal......
Nitz
because in output it place string $id, it wont parse variable in sigle quotes
eldar
@eldar the echo statement is in double quotes.
Gordon
@Gordon you are right, i have misread
eldar
+1  A: 

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
error on <?=$id?> error called "invalid XML markup"...
Nitz
Yeah, it should be '<?php echo $id; ?>' it is php not asp
mplungjan
then try <?php echo $id; ?>
Chinmayee
<?=$id?> is also valid php markup. http://codeigniter.com/user_guide/general/alternative_php.html for alternative echo markup in php
Chinmayee
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
Thanks Coquevas, I wasn't aware of the update :)
Chinmayee
Too bad, I definitely prefer <?= id ?> to the <?php....;?> one - never knew it existed
mplungjan
A: 
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
in this also cnf was showing error called unterminated string literal but i just put one thing in it...... echo "<a onclick=\"cnf('Are you sure you want to delete that?','".$id."')\">Delete</a>";
Nitz
A: 

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