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

1
  • 1
    I can see errors which 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?") Commented Oct 29, 2010 at 8:47

6 Answers 6

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
        }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

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>";
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')>"

Comments

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

4 Comments

Yeah, it should be '<?php echo $id; ?>' it is php not asp
<?=$id?> is also valid php markup. codeigniter.com/user_guide/general/alternative_php.html for alternative echo markup in php
Short tags shouldn't be used because support is not guaranteed on shared servers and it's being removed completely next major version: stackoverflow.com/questions/200640/…
Too bad, I definitely prefer <?= id ?> to the <?php....;?> one - never knew it existed
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 } ?>

Comments

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!

2 Comments

Offcourse I can make an invisible <div> 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??
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.
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')>";

2 Comments

because in output it place string $id, it wont parse variable in sigle quotes
@eldar the echo statement is in double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.