Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Just seperate the form and the JS, and use Jquery to attach the handlers –  KyleK Jun 5 '13 at 20:08

4 Answers 4

up vote 0 down vote accepted

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

share|improve this answer

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();
share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.