-2

Im tring to pass a string which im getting from my database to a javascript function.

In my php I have:

<input type="image" name="edit_me" id="edit_me"  value="<?php echo $row['id']; ?>" onClick="edit_chirp(<?php echo $row['id']; ?>)"/> //THIS WORKS

When I pass the id im getting it works fine but when I pass a field where it has text it doesnt work.

<input style="float:right" type="image" name="edit_me" id="edit_me"  value="<?php echo $row['id']; ?>" onClick="edit_chirp(<?php echo $row['content']; ?>)" //THIS DOESNT WORK

My javascript function is

function edit_chirp(somethig)
    {
        alert(somethig);
    }
EMAD SEYED
  • 13
  • 4

2 Answers2

1

You have been missing quotes dude. Try this

<input style="float:right" type="image" name="edit_me" id="edit_me"  value="<?php echo $row['id']; ?>" onClick="edit_chirp('<?php echo mysql_real_escape_string($row['content']); ?>')" //THIS WILL WORK
Voonic
  • 4,667
  • 3
  • 27
  • 58
  • 1
    Well, it doesn't work "perfectly"---it'll break as soon as there are apostrophes in the content or if someone decides to inject malicious JavaScript there. – JJJ Oct 15 '13 at 14:03
  • @Juhana yep you are right. He can use mysql_real_escape_string for this – Voonic Oct 15 '13 at 14:10
  • @shadow That's not the right function for this case. At all. – JJJ Oct 15 '13 at 14:22
0

Try to add simple quotes in the function call for the parameter. Something like this :

<input style="float:right" type="image" name="edit_me" id="edit_me"  value="<?php echo $row['id']; ?>" onClick="edit_chirp('<?php echo $row['content']; ?>')"
guikk
  • 195
  • 5