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.

Jordyn
  • 1,133
  • 2
  • 13
  • 28

2 Answers2

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>\');'
Community
  • 1
  • 1
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

You should use Heredoc Syntax to avoid quote issues.

Aurélien
  • 400
  • 4
  • 11