0

Forgive the title, this is the best way I can think to word my issue.

Basically, I have a html form inside some php code, but I need to include javascript functions (with parameters specified) inside the form. But I have an issue with the single and double quote marks escping the php code. See below:

echo'<form id="create_booking_form" onsubmit="return false;">
                    <div>Student Name: </div>
                    <input id="students_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('students_name')" maxlength="30">
                    <div>Parents Name: </div>
                    <input id="parents_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_name')" maxlength="30">
                    <div>Parents Email Address: </div>
                    <input id="parents_email" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_email')" maxlength="100">
                    <div>Booking Time: </div>
                    <input id="booking_time" type="text" onfocus="emptyElement('status')" onkeyup="restrict('booking_time')" maxlength="8">
                    <div>Comments / Questions: (max 255 characters)</div>
                    <input id="comments" type="text" onfocus="emptyElement('status')" onkeyup="restrict('comments')" maxlength="255">
                    <div>Password: </div>
                    <input id="password" type="password" maxlength="16">
                    <br /><br />
                    <button id="createbookingbtn" onclick="createbooking()">Submit your booking</button>
                    <span id="status"></span>
                </form>
            </div>
        </div>';?>

My issue is for example: onkeyup="restrict('students_name')"

If i use single quote to specify 'student_name' it escapes the echo command form the php code. If i use double quotes it escapes the restrict() function...

What can I do?

Much appreciated

Is anyone able to help me out here?

1
  • Just seperate the form and the JS, and use Jquery to attach the handlers Commented Jun 5, 2013 at 20:08

4 Answers 4

5

If your string is created with ''s, you can print a ' character by escaping it: \', and if it's created with "'s, then you can print it by escaping it to \".

In your situation, however, I wouldn't do either. I'd just end the PHP tag and start it again:

?>
<form id="create_booking_form" onsubmit="return false;">
            <div>Student Name: </div>
            <input id="students_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('students_name')" maxlength="30">
            <div>Parents Name: </div>
            <input id="parents_name" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_name')" maxlength="30">
            <div>Parents Email Address: </div>
            <input id="parents_email" type="text" onfocus="emptyElement('status')" onkeyup="restrict('parents_email')" maxlength="100">
            <div>Booking Time: </div>
            <input id="booking_time" type="text" onfocus="emptyElement('status')" onkeyup="restrict('booking_time')" maxlength="8">
            <div>Comments / Questions: (max 255 characters)</div>
            <input id="comments" type="text" onfocus="emptyElement('status')" onkeyup="restrict('comments')" maxlength="255">
            <div>Password: </div>
            <input id="password" type="password" maxlength="16">
            <br /><br />
            <button id="createbookingbtn" onclick="createbooking()">Submit your booking</button>
            <span id="status"></span>
        </form>
    </div>
</div>
<?php

But what when I want the HTML content to be in a variable?

Just use an output buffer.

ob_start();
?>
    <p>Some HTML</p>
<?php
$html = ob_get_clean();
Sign up to request clarification or add additional context in comments.

Comments

4

I would place the javascript in an external file, and have the php place the link to that file instead.

Comments

2

Put a \ in front of the quote to escape:

echo'<form id="create_booking_form" onsubmit="return false;">
                    <div>Student Name: </div>
                    <input id="students_name" type="text" onfocus="emptyElement(\'status\')" onkeyup="restrict(\'students_name\')" maxlength="30">

etc

Comments

0

You can escape your quotes by adding a single backslash before them..

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.