up vote 0 down vote favorite
share [g+] share [fb]

I would like to write in an input field with a PHP variable. This is the current code I have:

JS:

<script type="text/javascript">
function reply(form, inp){
    form.texta.value = "@" + inp;
}
</script>

HTML:

 <a href="#" onClick="reply(form1, <?php echo $poster; ?>);">Reply ^</a>

 <form id="form1" name="form1" method="post" action="index.php"><input type="texta" name="texta" /></form>

This code however does not put the text of the variable in the input field. Sorry, I am a bit new to JavaScript.

Can anyone help me here?

link|improve this question

Is there any error you faced? – Click Mar 29 '11 at 9:48
2  
You had to enclose the $poster variable inside quotes, in order to be treated as text! – nuc Mar 29 '11 at 9:51
feedback

5 Answers

<a href="#" onClick="reply(form1, '<?php echo $poster; ?>');">Reply ^</a>

EDIT : You should Give Id to text box.

link|improve this answer
still does not work for me, the text does not get into the field. – gdscei Mar 29 '11 at 9:53
document.getElementById('texta').value = "@" + inp; in javascript – Gaurav Mar 29 '11 at 9:54
and give id to text box. – Gaurav Mar 29 '11 at 9:55
feedback

if you want it direct

<input type="texta" name="texta" value="<?=$poster?>" />

Or

use document.form.texta.value = value

or like this

document.forms["form"]["texta"].value = value

put any of these inside

function reply(form, inp){

}

<a href="#" onClick="reply(form1,"<?=$poster?>");">Reply ^</a>
link|improve this answer
not possible, the variable is inside a while loop after the input field is created. – gdscei Mar 29 '11 at 9:50
feedback
<input type="text" name="texta" value="<?php echo $poster; ?>" />
link|improve this answer
not possible, the variable is inside a while loop after the input field is created. – gdscei Mar 29 '11 at 9:49
feedback

Using my code I gave in the question and due to the answers/comments i have put single quotes around the php, and now it works perfectly. Thanks!

link|improve this answer
feedback

If $poster is a string, you must enclose it inside single quotes like this:

<a href="#" onClick="reply(form1, '<?php echo $poster; ?>');">Reply ^</a>

You could have used double quotes but this would break the double quotes that you use to wrap the onClick attribute. Anyway, this is not sufficient. Consider this:

<a href="#" onClick="alert('<?php echo "Frankie 'The Bat' Niagara"; ?>');">Reply ^</a>

This will output:

<a href="#" onClick="alert('Frankie 'The Bat' Niagara');">Reply ^</a>

Not acceptable; the single quotes inside the string will terminate the string prematurely, resulting in invalid JavaScript. You can try the json_encode() function, like this:

<a href="#" onClick='alert(<?php echo json_encode("Frankie 'The Bat' Niagara", JSON_HEX_QUOT|JSON_HEX_APOS); ?>);'>Reply ^</a>

Output:

<a href="#" onClick='alert("Frankie \u0027The Bat\u0027 Niagara");'>Reply ^</a>
link|improve this answer
not necessary, it is not possible for the variable to have any special characters. – gdscei Mar 29 '11 at 10:35
Is that case just wrap the PHP code inside single quotes (javascript string delimiter), which themself should be wrapped in double quotes (attribute value delimiter). – Salman A Mar 29 '11 at 10:40
see my answer ;) – gdscei Mar 29 '11 at 13:40
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.