将PHP结果返回到原始html页面
Returning PHP results to original html page
我想将值$newsum
返回到从php计算到db结果的coldiv。我想保持html和php文件分开。然而,当这是我没有得到index.html页面上的结果。
我已经尝试了一些ajax解决方案,但没有工作,
index . html
<form id="search" action="index.php" method="post">
<input type='search' name='keywords' value='' style="width:99%;">
<a href='#col'>
<input type='hidden' value='Submit' id='submit' name='doSearch' />
</a>
</form>
<div id="col" style="width:100%; margin:0 auto;">
<script>
document.write("<?php echo $newsum.$message; ?>");
</script>
index . php
if($_POST['doSearch'] == 'Submit')
{
$value=$_POST['keywords'];
$newsum= round($total,1);
if($newsum >= 3.5)
{
$message=$newsum.'/5';
}
}
AJAX <script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.post("index.php", $("#search").serialize(), function(response) {
$('#col').html(response);
});
return false;
});
});
</script>
您错过了回显搜索结果:
编辑index.php
if($_POST['doSearch'] == 'Submit')
{
$value=$_POST['keywords'];
$newsum= round($total,1);
if($newsum >= 3.5)
{
$message=$newsum.'/5';
echo $message; // added
}
else
{
echo "Nope"; // this is only to make sure something at all is echoed.
}
}
然后修改JS:
// submit action instead of click is the way to use form data
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$('#search').submit(function(e){
e.preventDefault();
$.post("index.php", $("#search").serialize(), function(response) {
$('#col').html(response);
});
});
});
</script>
注意
将PHP代码添加到HTML文档中。这是行不通的…
<script>
document.write("<?php echo $newsum.$message; ?>");
</script>
即使是PHP文档,变量也不会存在,因为它们是在index.php中设置的。
相关文章:
- 如何使用JavaScriptLib将字符串(WYSIWYG)转换为原始html
- 获取原始HTML并将其重新呈现为HTML
- Webpack:如何将原始HTML加载为React JSX
- 表情符号显示为原始 HTML 代码
- 正在将DOM元素转换为原始HTML字符串
- 保存对“;contentEditable;部分返回到原始HTML文件
- 当我附加XSS过滤后的html时,它会打印原始html
- Electron:将原始html注入BrowserWindow
- 有没有一种方法可以用javascript将原始html添加为新的表行
- 如何在 MVC 中使用 javascript 作为原始 html 插入内容 ASP.NET
- 按 Esc 按钮后恢复为原始 html
- 确定元素是否由 JS 添加,还是原始 HTML 文档 *OR* 检测脚本何时通过 InnerHTML 更新节点
- 获取聚合物元素的原始 HTML 代码
- DOM、原始 html 以及页面上显示的内容是什么关系
- 原始HTML样式与jQuery生成的具有相同结构的HTML不同
- 使用 jquery 检索原始 html,而不是渲染的 html
- Angularjs:在双花括号内渲染原始html - 可能
- 如何将 html 元素添加到当前文本区域值,并将其输出为原始 html
- Angularjs routing 使用 $state 得到原始 html 标签
- 如何将节点内容从 xml 复制到硬编码的 html(动态到原始 html)