Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用html表单内的javascript函数_Php_Javascript_Html_Forms_Function - Fatal编程技术网

在php中使用html表单内的javascript函数

在php中使用html表单内的javascript函数,php,javascript,html,forms,function,Php,Javascript,Html,Forms,Function,请原谅这个标题,这是我能想到的最好的表达我的问题的方式 基本上,我在一些php代码中有一个html表单,但我需要在表单中包含javascript函数和指定的参数。但是我对php代码的单引号和双引号有一个问题。见下文: echo'<form id="create_booking_form" onsubmit="return false;"> <div>Student Name: </div>

请原谅这个标题,这是我能想到的最好的表达我的问题的方式

基本上,我在一些php代码中有一个html表单,但我需要在表单中包含javascript函数和指定的参数。但是我对php代码的单引号和双引号有一个问题。见下文:

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>';?>
我的问题是:onkeyup=restrict'students\u name'

如果我使用单引号指定'student_name',它将从php代码中转义echo命令。如果我使用双引号,它将逃避限制函数

我能做什么

非常感谢


有人能帮我吗?

我会将javascript放在一个外部文件中,让php将指向该文件的链接放在那里。

你可以通过在引号前面添加一个反斜杠来转义引号。

如果你的字符串是用s创建的,你可以通过转义来打印一个“字符:\”,如果它是用“s”创建的,然后您可以通过将其转义到\来打印它

然而,在你的情况下,我也不会这么做。我只需结束PHP标记并重新启动它:

?>
<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
但是,当我希望HTML内容位于变量中时,该怎么办

只需使用输出缓冲区

ob_start();
?>
    <p>Some HTML</p>
<?php
$html = ob_get_clean();

将\置于要转义的引号前:

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

只需将表单和JS分开,然后使用Jquery附加处理程序