1

I'm trying to insert javascript into a div. All of this is within a php document.

I am trying to get a div to fade into view when it's child div is loaded. Parent div had id of 'subemail' (this is hidden) Child div with id 'error' is shown then it should fade in the above div.

Following is the script I'm trying to load but get error unexpected T_STRING, expecting ',' or ';'.

<script type="text/javascript">
  $(function(){$('div#subemail').fadeIn('slow');});
</script>

The PHP document:

    if (empty($_POST['sub_name'])) {
        $errors[] = 'Please type in your name';
    } else {
        $sub_name = strip_tags($_POST['sub_name']);
    }

    if (filter_input(INPUT_POST, 'sub_email', FILTER_VALIDATE_EMAIL)) {
        $sub_email = $_POST['sub_email'];
    } else {
        $errors[] = 'Please type in a valid email'; 
    }

    if ($errors) {
        echo '<div class="errors">';
                    echo '<script type="text/javascript">';
                    echo '$(function(){$('div#subemail').fadeIn('slow');});';
                    echo '</script>';


        foreach ($errors as $error) {
            echo '<p class="error">- ' . $error . '</p>';
        }
        echo '</div>';

    }
0

3 Answers 3

10

You need to escape your quotes with backslashes:

echo '$(function(){$(\'div#subemail\').fadeIn(\'slow\');});';

The problem is you're opening a string with the first ', and closing it at 'div#.... rather than closing it at the end of the line. Most languages use a backslash to denote a quote which is part of the string, rather than terminating the string.

You could also switch contexts, which I think cleans things up drastically; just be sure to separate your view/controller logic by moving the below to a template file of some kind:

<? if ($errors) { ?>
  <div class="errors">
    <script type="text/javascript">
      $(function() { $('div#subemail').fadeIn('slow'); });
    </script>
    <? foreach ($errors as $error) { ?>
      <p class="error"><?= $error ?></p>
    <? } ?>
  </div>
<? } ?>
Sign up to request clarification or add additional context in comments.

Comments

1

alternatively, you can write

echo "$(function(){$('div#subemail').fadeIn('slow');});";

Comments

1
echo "$(function(){$('div#subemail').fadeIn('slow');});";

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.