0

I've problem writing javascript in PHP code. Here my script :

echo ' <script>';
echo ' $(function(){';
echo ' x = parseInt($("#counter2").val());';
echo ' $("#add_row2").click(function(){';
echo ' $("#addr2"+x).html("<td align="right">\'+ (x+1) +\'</td><td class="col-xs-2"><input type="text" name="vprice[]" class="required form-control text-right"></td><td class="col-xs-2"><input type=text" name="vminorder[]" class="required form-control text-right"></td>");';
echo ' $("#tab_logic2").append("<tr id="addr2\'+ x+1 +\'"></tr>");';
echo ' x++;';
echo ' });';
echo ' $("#delete_row2").click(function(){';
echo ' if(x> 1){';
echo ' $("#addr2"+(x-1)).html("");';
echo ' x--;';
echo ' }';
echo ' });';
echo ' });';
echo ' </script>';

When running it is produce an error Uncaught SyntaxError: missing ) after argument list in line

echo ' $("#addr2"+x).html("<td align="right">\'+ (x+1) +\'</td><td class="col-xs-2"><input type="text" name="vprice[]" class="required form-control text-right"></td><td class="col-xs-2"><input type=text" name="vminorder[]" class="required form-control text-right"></td>");';
echo ' $("#tab_logic2").append("<tr id="addr2\'+ x+1 +\'"></tr>");';

Because in this line have variable I use quotes and wrap it with \ tag. It still error.

Any sugesstion?

5
  • 3
    you dont need one echo per line Commented Jun 22, 2016 at 2:03
  • 2
    you can use HEREDOC php syntax for doing that Commented Jun 22, 2016 at 2:07
  • 3
    and btw you should not definitely print js code using php Commented Jun 22, 2016 at 2:08
  • When running Where is your html file? Commented Jun 22, 2016 at 2:13
  • I'm using codeigniter framework when using heredoc it showing more error. Commented Jun 22, 2016 at 2:51

2 Answers 2

1

The problem is obviously due to the crazy mixture of syntax and the confusion it causes.

First, use HEREDOC like @Diego suggested in the comment:

// A single `echo`!
echo <<<EOD
<script>
    \$(function(){
        x = parseInt(\$("#counter2").val());
        \$("#add_row2").click(function(){
            \$("#addr2"+x).html("<td align='right'>" + (x+1) + "</td>" + 
                                "<td class='col-xs-2'><input type='text' name='vprice[]' class='required form-control text-right'></td>" +
                                "<td class='col-xs-2'><input type='text' name='vminorder[]' class='required form-control text-right'></td>");
            \$("#tab_logic2").append("<tr id='addr2" + (x+1) + "'></tr>");
            x++;
        });
        \$("#delete_row2").click(function(){
        if(x> 1){
            \$("#addr2"+(x-1)).html("");
            x--;
        }
    });
});
</script>
EOD;

In doing so, it gets rid of the outer set of quotes, which makes it somewhat easier. After that, I got rid of the double-quotes from within the HTML, which were causing problems with your jQuery html method.

Just the organization of code will help you prevent this in the future.

Sign up to request clarification or add additional context in comments.

2 Comments

Fwiw, you cannot have line feeds as is in JS strings.
thanks @wes-foster but i'm using codeigniter framework, and it showing more error :(
0

You have unbalanced quotes in your output, e.g.:

"<td align="right">'

"<tr id="addr2'

Consider using a templating engine.

And it is a good idea to separate server-side code from what it generates: if you have a client-side-related issue, first investigate the resulting code instead of server-side code.

Comments

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.