【问题标题】:Trying to: Button calls function which replaces html by id, into <?php include("")?>尝试:按钮调用函数,将 html 替换为 id,进入 <?php include("")?>
【发布时间】:2016-11-24 16:54:26
【问题描述】:

我一直在尝试用两个按钮为我的网络服务器创建一个简单的 menu.php b1=登录,b2=注册。它们中的每一个都将调用其单独的函数,然后扫描 id='main' 块并将其内部 html 更改为类似于&lt;?php include('testLogIN.php') ?&gt;&lt;?php include('testRegister.php') ?&gt; 的内容。这些按钮会擦除 id='main' 的块的内部 html,但在我的服务器上,单击时按钮什么也不做。我被引导相信这可能是嵌套字符串如何工作的问题,因此echo "He said 'haha' and left." 不会正确打印He said 'haha' and left.。我知道我的描述可能到处都是,但请多多包涵。

@charset "UTF-8";

.right{
	margin:1%;
	position:relative;
	clear:right;
	float:right;
	right:15px;
}

.left{
	margin:1%;
	position:relative;
    	clear:left;
	float:left;
	left:10px;
}

.bleft{
	margin:1%;
	position:relative;
	float:left;
	width=15px;
}

.fleft{
	float:left;
}

.mainbox{
	margin:2%;
	width:450px;
	height:auto;
}

.box{
	clear:left;
	float:left;
	margin:1%;
	width:280px;
	height:33px;
}

img{ 
    	max-width: 100%;
	max-height: 100%;
    	width: auto; 
}

.stat{
	float:right;
	position:fixed;
	right:10px;
}

.bolded{
	border-width: 5px;
	size:5px;
}

h2{
	margin-left:2px;
}

.white{
	color:#DDD;
}

.black{
	background-color:#333;
}

hr{
	clear:left;
}
<!DOCTYPE html>
<html>
	<head>

		<title>Main</title>

		<link rel="stylesheet" type="text/css" href="mystyle.css">

		<style>
			.abs{
    				position: absolute;
    				top: 0;
    				left: 0;
			}
			.rel{
    				position: relative;
			}
			a:visited{
    				color: green;
			}
			body{
    				margin: 0;
    				padding: 0;
				border: 0;
    				background--image: url("images/dragon.jpg");
				background-size: cover;
			}
		</style>
		
	</head>
	<body>

		<div class="rel">
			<img src="images/dragon.jpg">
			
			<div id='main' class="mainbox black abs">
				<button onclick='tologin()'>Log in</button>
				<button onclick='toregister()'>Register</button>
			</div>
		</div>

	</body>
</html>

<script>
function tologin(){
    document.getElementById("main").innerHTML ="<?php include('unistuff/LogIN/login.php');?>";
}
function toregister(){
    document.getElementById("main").innerHTML ="<?php include('unistuff/LogIN/toregister.php');?>";
}
</script>

【问题讨论】:

  • PHP 代码只能在服务器上执行。更改 innerHTML 只会更改 HTML(客户端事件)。单击“登录”后,您可能需要向服务器发送请求,服务器将为您生成一个新页面。

标签: javascript php html include


【解决方案1】:

由于 php 忽略脚本(它将包含两个文件而不屈服于 javascript),我建议您查看 Loading file with javascript

或者你可以使用jQuery加载方法(不加载PHP文件):

<script>
    function tologin(){
        $("#main").load('login.html');
    }
    function toregister(){
        $("#main").load('register.html');
    }
</script>

如果必须是 PHP,那么你可以使用 Ajax

【讨论】:

  • 说实话,您没有阅读完整的答案吗?即使这样你也可以看到(使用 javascript 加载文件)为什么要重复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 2010-10-19
  • 2014-04-08
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 2013-01-30
相关资源
最近更新 更多