2

I want to add a jQuery code to a PHP string

Here is the PHP code

    $my_code ='
    <script src="js/jquery.form.js"></script>
    <script>
    $(document).ready(function()
    {
        $("#my_form").on("submit", function(e)
        {
            e.preventDefault();
            $("#myButton").attr("disabled", ""); 
            $("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);
            $(this).ajaxSubmit({
            target: "# my-output ",
            success:  afterSuccess
            });
        });
    });

function afterSuccess()
{   

    $("#myButton").removeAttr("disabled");

}
</script>
    ‘;

Issue I’m having in this code line

$("#my-output").html(‘<div class="alert alert-info" role="alert">My message</div>’);

I even try change the single quote to double quotes and when I do code stopped working. It works fine if I don’t have a div inside above code line. Ex:

$("#my-output").html(“My message”);

Appreciate your answers.

1
  • Why not create a separate .js file with that code ? Commented Nov 16, 2016 at 19:06

2 Answers 2

2

You can use single quotes inside the string, however you will need to \' escape them:

'$("#my-output").html(\'<div class="alert alert-info" role="alert">My message</div>\');'
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. Works like a charm. Thank you.
0

You should use Heredoc Syntax to avoid quote issues.

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.