因此,我对PHP很陌生,我在使用我的一些表单时遇到了困难。我认为这可能是我的网站设置方式给我带来了问题。
我有index.php,其设置如下:
<div class="pageContent">
<div id="main" style="width:1000px; margin:0 auto;">
<!-- Create the tabs -->
<div id="tabs" >
<ul>
<li><a href="#tabs-0">Overview</a></li>
<li><a href="#tabs-1">Ranked</a></li>
<li><a href="#tabs-2">Arena</a></li>
<li><a href="#tabs-3">Decks</a></li>
<li><a href="#tabs-4">New Game</a></li>
<li><a href="#tabs-5">Admin</a></li>
</ul>
<div id="tabs-0"></div>
<div id="tabs-1"></div>
<div id="tabs-2"></div>
<div id="tabs-3"></div>
<div id="tabs-4"></div>
<div id="tabs-5"></div>
</div>
<!-- Load the pages into tabs -->
<script>
$("#tabs").tabs();
$("#tabs-0").load("tab0.php");
$("#tabs-1").load("tab1.php");
$("#tabs-2").load("tab2.php");
$("#tabs-3").load("tab3.php");
$("#tabs-4").load("newGame.php");
$("#tabs-5").load("admin.php");
</script>
</div><!-- / main -->
</div><!-- / pageContent -->这给了我一个很好的静态页面和6个.php文件选项卡来做一些很酷的事情。
在index.php中有几个表单,登录之类的,它们的功能都很好。但是,当我在选项卡中的页面上创建一个表单时,它不会。
下面是admin.php (#tabs-5)的一个例子
<?php
if(isset($_POST['delLog'])){
unlink('log.txt');
echo 'Success';
}
if(isset($_POST['delLog'])){
unlink('error_log');
echo 'Success';
}
?>
<html>
<head>
</head>
<body>
<table width="100%" border="1">
<tr>
<td width="40%" valign="top">
</td>
<td width="30%" valign="top">
<h1>error_log</h1>
<form action="" method="post">
<input type="submit" name="delErr" id="delErr" value="Delete" />
</form>
<hr />
<?php
$lines = explode("\n", file_get_contents('error_log'));
foreach ($lines as $line){
echo $line.'<br>';
}
?>
</td>
<td width="3'0%" valign="top">
<h1>log.txt</h1>
<form action="" method="post">
<input type="submit" name="delLog" id="delLog" value="Delete" />
</form>
<hr />
<?php
$lines = explode("\n", file_get_contents('log.txt'));
foreach ($lines as $line){
echo $line.'<br>';
}
?>
</td>
</tr>
</table>
</body>
</html>这是另一个更好的例子。我昨天为这个问题做的一次脱光测试
<?php
define('INCLUDE_CHECK',true);
include 'php/functions.php';
error_reporting(E_ALL);
ini_set('display_errors',1);
logThis('ready!');
if (isset($_POST['submit'])) {
logThis('success');
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="">
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</body>
</html>这两个例子都不起作用。当按下submit按钮时,站点会刷新,但不会发生PHP操作。error_log中没有错误消息。我觉得它根本没接到电话。
两个表单都不返回输出,其中一个添加到数据库中。另一个删除日志文件。
希望我已经提供了足够的细节。
发布于 2015-07-08 19:59:56
,无论你想做什么,都是不可能的。无法将PHP代码加载到前端,只能由服务器.处理。
语句$("#tabs-5").load("admin.php"); 只获取html代码(由服务器处理),而不是PHP脚本。
发布于 2015-07-08 19:49:22
<form action="" method="post">将发布到当前页面(即。不管你的地址栏里有什么)。
当您使用ajax将页面加载到index.php中时,这意味着表单将发布到index.php.我猜您希望在每个php-文件中获取表单提交。
因此,要修复它,要么必须使用ajax发布(并使用响应刷新选项卡),要么需要将表单上的action属性更改为每个php-文件.在这种情况下,提交表单时会丢失选项卡式布局。您可以这样做,如下所示:
在每个php文件中,您都会执行类似的操作(以newGame.php为例):
<?php
if (isset($_POST)) {
//Do your form-handling stuff first
// [...]
// ...and then redirect back to index.php
header("Location: index.php");
die();
} else {
//Print your form
echo '<form action="newGame.php" method="post">';
// [...]
echo '<input type="submit" value="Create game">';
echo '</form>';
}<html>、<head>、<body>或其他html --只是在选项卡中需要的裸露内容.现在,对于ajax-submit方法,您还应该删除所有的“包装html",仍然应该在每个表单上设置一个target="[...]",并且仍然应该在每个单独的文件中进行表单处理。但是,您不应该在表单处理之后进行重定向,而应该再次输出表单:
<?php
if (isset($_POST)) {
//Do your form-handling stuff first
// [...]
//Maybe print a little message to let the user know something happened
echo 'Form completed at ' . date('H:i:s');
}
//...then print your form no matter what
echo '<form action="newGame.php" method="post">';
// [...]
echo '<input type="submit" value="Create game">';
echo '</form>';然后将此脚本添加到index.php -它将使用ajax在选项卡内发布所有表单,而不是刷新页面:
<script>
my_cool_ajax_post = function(form_elm, tab_elm) {
//Actual ajax post
$.post(form_elm.target, $(form_elm).serialize(), function(data){
//Results are in, replace tab content
tab_elm.html(data);
});
};
$(document).on("submit", "#tabs form", function(){
//Store the form element
var form_elm = this;
//Find the parent tab element
var tab_elm = $(this).closest('#tabs > div');
//Really simple "loader"
tab_elm.html('<p>loading...</p>');
//Submit by ajax
my_cool_ajax_post(form_elm, tab_elm);
return false;
});
</script>免责声明:完全未经测试,所以请告诉我.
https://stackoverflow.com/questions/31302260
复制相似问题