1

I'm getting an error and I don't know how to resolve it..

The error is:

Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$' in C:\xampp\htdocs\records\delete-confirm.php on line 7

Line 7 is:

echo "<script>$(document).ready(function(){$('.modal-" . $row->id . "').hide();$('#delete-" . $row->id . "').click(function(){$('.modal-" . $row->id . "').show();});$('#cancel-" . $row->id . "').click(function(){$('.modal-" . $row->id . "').hide();});});</script>";

I have checked the whole line but there seems no errors in there? What is going wrong here?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Mia
  • 817
  • 2
  • 8
  • 13
  • 1
    Variables are parsed in double quoted strings: http://stackoverflow.com/q/3446216/3933332 (Also see, more general: http://stackoverflow.com/q/18050071/3933332) – Rizier123 Feb 19 '16 at 14:08
  • Replace double-quotes with single-quotes. (and single with double) – fusion3k Feb 19 '16 at 14:10

2 Answers2

5

By using double quotes " in an echo statement, PHP reads the value of the content inside the quotes. Single quotes ' assign a value to what's between the quotes.

$variable = 'Mia';  // assigns the value Mia to the $variable
echo '$variable';   // output is $variable;
echo "$variable";   // output is Mia;

In your example, wrap literal output in single quotes instead of double quotes and it will solve your problem.

echo '<div class="example" id="' . $variable . '">';
serraosays
  • 7,163
  • 3
  • 35
  • 60
mitkosoft
  • 5,262
  • 1
  • 13
  • 31
1

Actually it's (technically) enough to change every {$ to { $ (i.e. separated by a space) as in

echo "<script>$(document).ready(function(){ $('.modal-" . $row->id . "').hide();$('#delete-" . $row->id . "').click(function(){ $('.modal-" . $row->id . "').show();});$('#cancel-" . $row->id . "').click(function(){ $('.modal-" . $row->id . "').hide();});});</script>";

(or change the double-quoted php string to a single-quoted one, as mentioned earlier)

PHP switches in a slightly different parsing mode when you use "....{$var}..." instead of "... $var ..." and in your case it ticks the parser off because $( makes no sense to it.

But do those few whitespaces, necessary to make the code human readable, really hurt?

<?php
echo '<script>
    $(document).ready(function(){
        $(".modal-' . $row->id . '").hide();
        $("#delete-' . $row->id . '").click(function(){
            $(".modal-' . $row->id . '").show();
        });
        $("#cancel-' . $row->id . '").click(function(){
            $(".modal-' . $row->id . '").hide();
        });
    });
</script>';
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Thanks, the problem has been solved with help from your first example. About to make the code readable for humans, I have multiple `` and inside those tags there's the jquery script. If I make the code human readable, these `` tags will become a huge part of the source code. Thanks for helping and have a nice weekend! – Mia Feb 19 '16 at 16:23
  • Oh, you're putting that code "into" multiple (read: many, many) tags? Have you considered using e.g. a [data- attribute](https://developer.mozilla.org/en/docs/Web/Guide/HTML/Using_data_attributes) just for the element id and then just let the (single instance) code figure it out? – VolkerK Feb 19 '16 at 16:27
  • I have not considered that because I have yet to learn the data attribute part. I know how to use it in some ways, but for now I'll stick to the old school way. :] – Mia Feb 19 '16 at 16:39