来自不同 html 页面的 PHP 连接

Posted

技术标签:

【中文标题】来自不同 html 页面的 PHP 连接【英文标题】:Php connection from different html page 【发布时间】:2018-10-19 04:05:51 【问题描述】:

Gud 下午的伙计们。非常感谢您在我之前的问题中的回答。它真的对我有帮助。今天我有另一个问题要从不同的 html 页面访问我的变量。我希望我的表单页面与富文本框页面连接,并从那里将内容应用于表单页面。我尝试通过在表单页面中插入“操作”来做到这一点,但富文本框页面显示未定义变量。我希望任何人都可以帮助我说出我的错。提前致谢。

表单页面:

<?php
session_start();
$_SESSION['message'] = '';
$mysqli=new MySQLi('127.0.0.1','root','','accounts');
if($_SERVER["REQUEST_METHOD"] == "POST") 
$option1 = $mysqli->real_escape_string($_POST['option1']);
$option2 = $mysqli->real_escape_string($_POST['option2']);
$option3 = $mysqli->real_escape_string($_POST['option3']);
$option4 = $mysqli->real_escape_string($_POST['option4']);
$option5 = $mysqli->real_escape_string($_POST['option5']);
$option6 = $mysqli->real_escape_string($_POST['option6']);


                $sql ="INSERT INTO menubar(option1,option2,option3,option4,option5,option6)"
                ."VALUES ('$option1','$option4','$option3','$option4','$option5','$option6')";

if($mysqli->query($sql)=== true) 
                          $_SESSION['message'] =  'Registration successful!
                          Added $username to the database!';
                     header("location:confirmnormal.php");
                      
                      else 
                          $_SESSION['message'] = "User could not be added to the database!";



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Normal</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" autocomplete="off">
<table align="center" >
<tr>
<td>

 Enter Menu Bar:
</td>
 &nbsp;<td> 
<input type="text" placeholder="Personal Information" name="option1" />
<tr><td></td><td><input type="text" placeholder="Career Aspirations" name="option2" /></td></tr>
 <tr><td></td><td> <input type="text" placeholder="Educational Background" name="option3" required /></td></tr>
 <tr><td></td> <td> <input type="text" placeholder="Skills" name="option4" required /></td></tr>
  <tr><td></td> <td><input type="text" placeholder="Language Proficiency" name="option5" required /></td></tr>
<tr><td></td><td><input type="text" placeholder="Job Preference" name="option6" required /></td>
</td></tr>
</tr>
</tr>
</form>
</tr>
<div>
<?= $_SESSION['message']?>
<form method="POST"  action="richtext.php">
<tr>
<td> 
Content
</td>
</tr>
<td>
Content 1 :
</td>
<td>
<a href="richtextbox.php">
<input type="text" placeholder="Resume" name="content1"<?=$_SESSION['content1']?> />
</a>
</td>
<tr>
<td>
Content 2 :
<td>
<a href="richtextbox.php"><? $_SESSION['content2']?>
<input type="text" placeholder="Personal" name="content2" />
</a>
</td>
</td>
</tr>
<tr>
<td>
Content 3 :
<td>
<a href="richtextbox.php"><? $_SESSION['content3']?>
<input type="text" placeholder="Resume" name="content3" />
</a>
</td>
</td>
</tr>
<tr>
<td>
Content 4 :
<td>
<a href="richtextbox.php"><? $_SESSION['content4']?>
<input type="text" placeholder="Personal" name="content4" />
</a>
</td>
</td>
</tr>
<tr>
<td>
Content 5 :
<td>
<a href="richtextbox.php">
<input type="text" placeholder="Resume" name="content5" <?=$_SESSION['content5']?>/>
</a>
</td>
</td>
</tr>
<tr>
<td>
Content 6 :
<td>
<a href="richtextbox.php">
<input type="text" placeholder="Personal" name="content6" <?=$_SESSION['content6']?>/>
</a>
</td>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" name="login" value="login" class="btn-login"/>
</td>
</tr>
</tr>

</td>
</tr>
</form>
</tr>
</table>
</body>
</html>
RichTextbox page
<?php
session_start();
$_SESSION['message'] = '';
$mysqli=new MySQLi('127.0.0.1','root','','accounts');
if($_SERVER["REQUEST_METHOD"] == "POST") 
$content2=$_POST['content2'];
$content3=$_POST['content3'];
$content4=$_POST['content4'];
$content5=$_POST['content5'];
$content6=$_POST['content6'];

$sql ="INSERT INTO menubar(content1,content2,content3,content4,content5,content6)"
                ."VALUES ('$content1','$content2','$content3','$content4','$content5','$content6')";

if($mysqli->query($sql)=== true) 
                          $_SESSION['content1'] =  $content1;
                          $_SESSION['content2'] =  $content1;
                          $_SESSION['content3'] =  $content1;
                          $_SESSION['content4'] =  $content1;
                          $_SESSION['content5'] =  $content1;
                          $_SESSION['content6'] =  $content1;
                     header("location:confirmnormal.php");
                      
                      else 
                          $_SESSION['message'] = "User could not be added to the database!";
            



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Rich text editor in js</title>
<script src="https://use.fontawesome.com/cfc0a97614.js"></script>
</head>

<body onload="enableEditMode();">
<div>
<?= $_SESSION['message']?>
<form class="form" action="" method="POST">
<button onclick="execCmd('bold');"><strong>B</strong></button>
<button onclick="execCmd('italic');"><em>I</em></button>
<button onclick="execCmd('underline');"><i class="fa fa-underline"></i></button>
<button onclick="execCmd('strikeThrough');"><i class="fa fa-strikethrough"></i></button>
<button onclick="execCmd('justifyLeft');"><i class="fa fa-align-left"></i></button>
<button onclick="execCmd('justifyCenter');"><i class="fa fa-align-center"></i></button>
<button onclick="execCmd('justifyRight');"><i class="fa fa-align-right"></i></button>
<button onclick="execCmd('justifyFull');"><i class="fa fa-align-justify"></i></button>
<button onclick="execCmd('copy');"><i class="fa fa-copy"></i></button>
<button onclick="execCmd('indent');"><i class="fa fa-indent"></i></button>
<button onclick="execCmd('outdent');"><i class="fa fa-dedent"></i></button>
<button onclick="execCmd('superscript');"><i class="fa fa-superscript"></i></button>
<button onclick="execCmd('undo');"><i class="fa fa-undo"></i></button>
<button onclick="execCmd('redo');"><i class="fa fa-repeat"></i></button>
<button onclick="execCmd('insertUnorderedList');"><i class="fa fa-list-ul"></i></button>
<button onclick="execCmd('insertOrderedList');"><i class="fa fa-list-ol"></i></button>
<br />
<select onchange = "execCommandWithArg('formatBlock',this.value);">
<option value="H1">H1</option>
<option value="H2">H2</option>
<option value="H3">H3</option>
<option value="H4">H4</option>
<option value="H5">H5</option>
<option value="H6">H6</option>

</select>
<button onclick="execCmd('insertHorizontalRule');">HR</button>
<button onclick="execCommandWithArg('createLink',prompt('Enter a URL','http://'));"><i class="fa fa-link"></i></button>
<button onclick="execCmd('unlink');"><i class="fa fa-unlink"></i></button>
<button onclick="toggleSource();"><i class="fa fa-code"></i></button>
<button onclick="toggleEdit();">Toggle Edit</button>
<select onchange = "execCommandWithArg('fontName',this.value);">
<option value="Arial">Arial</option>
<option value="Comic Sans MS">Comic Sans MS</option>
<option value="Courier">Courier</option>
<option value="Georgia">Georgia</option>
<option value="Tahoma">Tahoma</option>
<option value="Times New Roman">Times New Roman</option>
<option value="Verdana">Verdana</option>
</select>
<select onchange = "execCommandWithArg('fontSize',this.value);">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br />
Fore Color:<input type="color" onchange = "execCommandWithArg('forecolor',this.value);"> 
Background: <input type="color" onchange = "execCommandWithArg('hiliteColor',this.value);">

<button onclick="execCommandWithArg('InsertImage',prompt('Enter the image URL',''));"><i class="fa fa-file-image-o"></i></button>
<button onclick="execCmd('selectAll');">Select All</button>
</div>
<iframe name="richTextField" style="width:500px; height: 500px;"></iframe>
<script type="text/javascript">
var showinSourceCode = false;
var isInEditMode = true;
function enableEditMode () 
    richTextField.document.designMode = 'On';

function execCmd (command) 
    richTextField.document.execCommand(command, false, null);

function execCommandWithArg(command,arg)
richTextField.document.execCommand(command, false, arg);
   
function toggleSource () 
if(showingSourceCode) 
    richTextField.document.getElementsByTagName('body')[0].innerHTML = richTextField.document.getElementsByTagName('body')[0].textContent;
    showingSourceCode = false;
else 
    richTextField.document.getElementsByTagName('body')[0].textContent = richTextField.document.getElementsByTagName('body')[0].innerHTML;
    showingSourceCode = true;



function toggleEdit() 
    if(isInEditMode)
        richTextField.document.designMode = 'Off';
        isInEditMode = false;
     else 
    richTextField.document.designMode = 'On';
        isInEditMode = true;
    



</script>
<br />
<a href="nORMAL.php"> <input type="submit" name="login" value="login" class="btn-login" /></a>
</body>
</html>

【问题讨论】:

【参考方案1】:

存在一些语法问题。请检查下面的代码。

  <?php
    session_start();
    $_SESSION['message'] = '';
    $mysqli=new MySQLi('127.0.0.1','root','','accounts');
    if($_SERVER["REQUEST_METHOD"] == "POST") 
        $option1 = $mysqli->real_escape_string($_POST['option1']);
        $option2 = $mysqli->real_escape_string($_POST['option2']);
        $option3 = $mysqli->real_escape_string($_POST['option3']);
        $option4 = $mysqli->real_escape_string($_POST['option4']);
        $option5 = $mysqli->real_escape_string($_POST['option5']);
        $option6 = $mysqli->real_escape_string($_POST['option6']);


        $sql = "INSERT INTO menubar(option1,option2,option3,option4,option5,option6)"
            . "VALUES ('$option1','$option4','$option3','$option4','$option5','$option6')";

        if ($mysqli->query($sql) === true) 
            $_SESSION['message'] = 'Registration successful!
                              Added $username to the database!';
            header("location:confirmnormal.php");
         else 
            $_SESSION['message'] = "User could not be added to the database!";

        

    
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Normal</title>
    </head>
    <body>
    <form method="post" enctype="multipart/form-data" autocomplete="off">
        <table align="center" >
            <tr>
                <td>

                    Enter Menu Bar:
                </td>
                &nbsp;<td>
                    <input type="text" placeholder="Personal Information" name="option1" />
            <tr><td></td><td><input type="text" placeholder="Career Aspirations" name="option2" /></td></tr>
            <tr><td></td><td> <input type="text" placeholder="Educational Background" name="option3" required /></td></tr>
            <tr><td></td> <td> <input type="text" placeholder="Skills" name="option4" required /></td></tr>
            <tr><td></td> <td><input type="text" placeholder="Language Proficiency" name="option5" required /></td></tr>
            <tr><td></td><td><input type="text" placeholder="Job Preference" name="option6" required /></td>
                </td></tr>
            </tr>
            </tr>
        </table>
    </form>
        <?= $_SESSION['message']?>
        <form method="POST"  action="richtext.php">
            <tr>
                <td>
                    Content
                </td>
            </tr>
            <td>
                Content 1 :
            </td>
            <td>
                <a href="richtextbox.php">
                    <input type="text" placeholder="Resume" name="content1"<?=$_SESSION['content1']?> />
                </a>
            </td>
            <tr>
                <td>
                    Content 2 :
                <td>
                    <a href="richtextbox.php"><? $_SESSION['content2']?>
                        <input type="text" placeholder="Personal" name="content2" />
                    </a>
                </td>
                </td>
            </tr>
            <tr>
                <td>
                    Content 3 :
                <td>
                    <a href="richtextbox.php"><? $_SESSION['content3']?>
                        <input type="text" placeholder="Resume" name="content3" />
                    </a>
                </td>
                </td>
            </tr>
            <tr>
                <td>
                    Content 4 :
                <td>
                    <a href="richtextbox.php"><? $_SESSION['content4']?>
                        <input type="text" placeholder="Personal" name="content4" />
                    </a>
                </td>
                </td>
            </tr>
            <tr>
                <td>
                    Content 5 :
                <td>
                    <a href="richtextbox.php">
                        <input type="text" placeholder="Resume" name="content5" <?=$_SESSION['content5']?>/>
                    </a>
                </td>
                </td>
            </tr>
            <tr>
                <td>
                    Content 6 :
                <td>
                    <a href="richtextbox.php">
                        <input type="text" placeholder="Personal" name="content6" <?=$_SESSION['content6']?>/>
                    </a>
                </td>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <input type="submit" name="login" value="login" class="btn-login"/>
                </td>
            </tr>
            </tr>

            </td>
            </tr>
        </form>
        </tr>
        </table>
    </body>
    </html>
    RichTextbox page
    <?php
    session_start();
    $_SESSION['message'] = '';
    $mysqli=new MySQLi('127.0.0.1','root','','accounts');
    if($_SERVER["REQUEST_METHOD"] == "POST") 
        $content2=$_POST['content2'];
        $content3=$_POST['content3'];
        $content4=$_POST['content4'];
        $content5=$_POST['content5'];
        $content6=$_POST['content6'];

        $sql ="INSERT INTO menubar(content1,content2,content3,content4,content5,content6)"
            ."VALUES ('$content1','$content2','$content3','$content4','$content5','$content6')";

        if($mysqli->query($sql)=== true) 
            $_SESSION['content1'] =  $content1;
            $_SESSION['content2'] =  $content1;
            $_SESSION['content3'] =  $content1;
            $_SESSION['content4'] =  $content1;
            $_SESSION['content5'] =  $content1;
            $_SESSION['content6'] =  $content1;
            header("location:confirmnormal.php");
        
        else 
            $_SESSION['message'] = "User could not be added to the database!";
        

    

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Rich text editor in js</title>
        <script src="https://use.fontawesome.com/cfc0a97614.js"></script>
    </head>

    <body onload="enableEditMode();">
    <div>
        <?= $_SESSION['message']?>
        <form class="form" action="" method="POST">
            <button onclick="execCmd('bold');"><strong>B</strong></button>
            <button onclick="execCmd('italic');"><em>I</em></button>
            <button onclick="execCmd('underline');"><i class="fa fa-underline"></i></button>
            <button onclick="execCmd('strikeThrough');"><i class="fa fa-strikethrough"></i></button>
            <button onclick="execCmd('justifyLeft');"><i class="fa fa-align-left"></i></button>
            <button onclick="execCmd('justifyCenter');"><i class="fa fa-align-center"></i></button>
            <button onclick="execCmd('justifyRight');"><i class="fa fa-align-right"></i></button>
            <button onclick="execCmd('justifyFull');"><i class="fa fa-align-justify"></i></button>
            <button onclick="execCmd('copy');"><i class="fa fa-copy"></i></button>
            <button onclick="execCmd('indent');"><i class="fa fa-indent"></i></button>
            <button onclick="execCmd('outdent');"><i class="fa fa-dedent"></i></button>
            <button onclick="execCmd('superscript');"><i class="fa fa-superscript"></i></button>
            <button onclick="execCmd('undo');"><i class="fa fa-undo"></i></button>
            <button onclick="execCmd('redo');"><i class="fa fa-repeat"></i></button>
            <button onclick="execCmd('insertUnorderedList');"><i class="fa fa-list-ul"></i></button>
            <button onclick="execCmd('insertOrderedList');"><i class="fa fa-list-ol"></i></button>
            <br />
            <select onchange = "execCommandWithArg('formatBlock',this.value);">
                <option value="H1">H1</option>
                <option value="H2">H2</option>
                <option value="H3">H3</option>
                <option value="H4">H4</option>
                <option value="H5">H5</option>
                <option value="H6">H6</option>

            </select>
            <button onclick="execCmd('insertHorizontalRule');">HR</button>
            <button onclick="execCommandWithArg('createLink',prompt('Enter a URL','http://'));"><i class="fa fa-link"></i></button>
            <button onclick="execCmd('unlink');"><i class="fa fa-unlink"></i></button>
            <button onclick="toggleSource();"><i class="fa fa-code"></i></button>
            <button onclick="toggleEdit();">Toggle Edit</button>
            <select onchange = "execCommandWithArg('fontName',this.value);">
                <option value="Arial">Arial</option>
                <option value="Comic Sans MS">Comic Sans MS</option>
                <option value="Courier">Courier</option>
                <option value="Georgia">Georgia</option>
                <option value="Tahoma">Tahoma</option>
                <option value="Times New Roman">Times New Roman</option>
                <option value="Verdana">Verdana</option>
            </select>
            <select onchange = "execCommandWithArg('fontSize',this.value);">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
            </select>
            <br />
            Fore Color:<input type="color" onchange = "execCommandWithArg('forecolor',this.value);">
            Background: <input type="color" onchange = "execCommandWithArg('hiliteColor',this.value);">

            <button onclick="execCommandWithArg('InsertImage',prompt('Enter the image URL',''));"><i class="fa fa-file-image-o"></i></button>
            <button onclick="execCmd('selectAll');">Select All</button>
    </div>
    <iframe name="richTextField" style="width:500px; height: 500px;"></iframe>
    <script type="text/javascript">
        var showinSourceCode = false;
        var isInEditMode = true;
        function enableEditMode () 
            richTextField.document.designMode = 'On';
        
        function execCmd (command) 
            richTextField.document.execCommand(command, false, null);
        
        function execCommandWithArg(command,arg)
            richTextField.document.execCommand(command, false, arg);
        
        function toggleSource () 
            if(showingSourceCode) 
                richTextField.document.getElementsByTagName('body')[0].innerHTML = richTextField.document.getElementsByTagName('body')[0].textContent;
                showingSourceCode = false;
            else 
                richTextField.document.getElementsByTagName('body')[0].textContent = richTextField.document.getElementsByTagName('body')[0].innerHTML;
                showingSourceCode = true;


            
            function toggleEdit() 
                if(isInEditMode)
                    richTextField.document.designMode = 'Off';
                    isInEditMode = false;
                 else 
                    richTextField.document.designMode = 'On';
                    isInEditMode = true;
                
            

        
    </script>
    <br />
    <a href="nORMAL.php"> <input type="submit" name="login" value="login" class="btn-login" /></a>
    </body>
    </html>

【讨论】:

对不起先生..你能告诉我哪里是语法错误 1.缺少第 25 行 - 您必须关闭 if($_SERVER["REQUEST_METHOD"] == "POST") 块 \n 2. 第 35 行 &lt;table&gt; 标记应正确关闭,然后表单标记应关闭

以上是关于来自不同 html 页面的 PHP 连接的主要内容,如果未能解决你的问题,请参考以下文章

如何从另一个页面加载 .php 页面并加载动态内容

如何在没有 iFrame 的情况下在 html 上显示来自另一台服务器的 PHP 页面?

如何用php程序主动向html页面发送信息?

页面静态化常用方法

PHP - 简单获取页面内容

将来自两个不同 wordpress 的帖子合并到一个按日期排序的帖子页面