Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have some question about concatenation php string to javascript string ...

for example:

php variable

$man = "Jamse";

and have javascript function

<script>
    if (document.getElementById("fname").value == "") {
        q = false;
        msg = <?php echo 'Please fill first name'.$formErrors['fname'].'\n' ?>;
    }
</script>

i want to do something like this can anyone help me ?

share|improve this question

4 Answers 4

alert('my name is: <?php echo $man; ?>' );

share|improve this answer
    
what can i do if i need to exactly concat it ? –  VostanAzatyan Mar 21 at 15:40
1  
This, of course, assumes $man has no apostrophes. –  Mike Christensen Mar 21 at 15:40
    
When your page is rendeded, <?php echo $man; ?> will be replaced with whatever is in $man; in this case the rendered output will be alert('my name is: Jamse' ); –  ElGavilan Mar 21 at 15:41
    
ok .... let me explain more clearly what i need –  VostanAzatyan Mar 21 at 15:42
    
@MikeChristensen That is a good point. If $man is populated via user input (via form, etc.) you will want to validate your input. –  ElGavilan Mar 21 at 15:42
alert('my name is: <?= $man; ?>');

Since PHP will insert $man on the server side, it's not a separate string that must be combined by JS. All the browser will see is

alert('my name is: Jamse');
share|improve this answer

Why not write it all in php?

<?php
$man = "Jamse";
echo "<script>
function alertMyName() {
    alert('my name is:" . $man . "');
}
</script>";
?>
share|improve this answer
    
You should avoid manually echoing HTML whenever possible. –  ElGavilan Mar 21 at 15:43
    
@ElGavilan can you elaborate? –  user3147515 Mar 21 at 15:45
    
It is better to leave the static parts of the HTML and javascript code in place and only use PHP to render the dynamic parts. Echoing the entire HTML is generally considered bad form as it makes your code harder to maintain and more prone to errors. –  ElGavilan Mar 21 at 15:49
up vote 0 down vote accepted

The other answers are true, I just forgot to write quotes and javascript did not understand it was a String and gave me an error. The correct code:

   if (document.getElementById("fname").value == "") {
        q = false;
        msg = "<?php echo 'Please fill first name'.$formErrors['fname'].'\n'; ?>";
    }
share|improve this answer
1  
Are you answering your own question? Or posting further info? It's hard to understand the sentence above the code (plus the sentence is part of the code block). If this is indeed an answer, please edit it to be readable. If it's additional info for your question, edit your question. –  David Makogon Mar 23 at 23:27
1  
@DavidMakogon I think he's just agreeing with the other answers, which told him to use quotes. –  dcastro Mar 24 at 8:28
    
thank you for editing –  VostanAzatyan Mar 24 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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