0

i want to show 'error' and styling the input form errors.
this my code so far :

this index.html :

 <div class="error" style="display:none"><ol></ol></div>
    <input name="name">
    <input name="number">

and this one process.php:

<?php



$a   =$_POST['name'];
$b   =$_POST['number'];

if(empty($a)) {
  echo
   "<script>
   $(document).ready(function (){
   $('.error').slideDown();
   $('.error ol').append('<li>Name Cannot Empty</li>');
   $('input[name=\'name\']').css({"border":"1px solid red"});
   });
   </script>";
}

    ?>

its not working on my code, i dont have idea why.

1
  • Well the problem is the unescapped double quotes, as the SO syntax highlighter makes obvious. More important though is why you would want to do this Commented Sep 9, 2014 at 10:59

3 Answers 3

2

Your PHP string is delimited by " characters.

Your JavaScript includes " characters.

You need to escape them: \".


Alternatively, stop trying to write JavaScript embedded in HTML embedded in PHP. It's hard to read and causes problems like you are experiencing:

if(empty($a)) {
    ?>
        <script>
            $(document).ready(function (){
                $('.error').slideDown();
                $('.error ol').append('<li>Name Cannot Empty</li>');
                $('input[name="name"]').css({"border":"1px solid red"});
            });
        </script>";
    <?php
}
    ?>
Sign up to request clarification or add additional context in comments.

Comments

1

You have to escape double-quotes " using backslash \ => \":

if(empty($a)) {
  echo
   "<script>
   $(document).ready(function (){
   $('.error').slideDown();
   $('.error ol').append('<li>Name Cannot Empty</li>');
   $('input[name=\'name\']').css({\"border\":\"1px solid red\"});
   });
   </script>";
}

1 Comment

this working form me, thanks guys for feedback. [CASE CLOSED]
0

$('input[name=\'name\']').css({"border":"1px solid red"});

this line causing isse.. here double quotes around border and its value terminate the string here.. use the single quotes here..

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.