我需要帮助,当我按下提交按钮时,我希望我的回显显示在我有跨度的页面中,而不是像(../addIPSE.php)...。
我的index.html
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="js/jQueryMobile/jquery.mobile-1.4.5.min.css" />
<script type="text/javascript" src="js/jquery-2.2.0.min.js"></script>
<script type="text/javascript" src="js/jQueryMobile/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="js/addIPSE.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" position="fixed">
<h1>Page Title</h1>
</div><!-- /header -->
<div data-role="content">
<form id="addIPSEform" action="addIPSE.php" method="post">
<label for="addIPSE">IPSE:</label>
<input type="text" name="addIPSE" id="addIPSE" placeholder="IPSE..." value="" data-clear-btn="true">
<button id="submitButton" class="ui-btn ui-corner-all">Submit</button>
</form>
<span name="result" id="result"></span>
</div><!-- /content -->
<div data-role="footer" position="fixed">
<h4>Page Footer</h4>
</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>
我的addIPSE.php
<?php
include_once('DBconnection.php');
$IPSE = $_POST['addIPSE'];
$lookForIPSEinDB = mysql_query("SELECT IPSE FROM myDB WHERE IPSE='$IPSE'");
if (mysql_num_rows($lookForIPSEinDB) != 0) {
mysql_query("UPDATE myDB SET antal_login = antal_login + 1 WHERE IPSE='".$IPSE."'");
echo "Already in DB, adding +1 antal_login...";
}
else {
mysql_query("INSERT into myDB(IPSE) VALUES('".$IPSE."')");
mysql_query("UPDATE myDB SET antal_login = antal_login + 1 WHERE IPSE='".$IPSE."'");
echo "New IPSE added in DB...";
}
?>
我的addIPSE.js
$("#submitButton").click( function() {
var inputIPSE = $("#addIPSE").val();
$.post( $("#addIPSEform").attr("action"), inputIPSE, function(info){ $("#result").html(info); });
});
$("#addIPSEform").submit( function() {
return false;
});
当我按下提交按钮时,我首先进入全白页,然后我必须刷新页面以查看我的回声,但我得到的唯一回音是“已经在DB ..:”,它在我的索引页面的跨度中没有显示...
答案 0 :(得分:3)
您的表单正在重定向到您的php文件。您需要删除该操作并告诉submitButton单击甚至以防止默认():
$("#submitButton").click( function(e) {
e.preventDefault()
var inputIPSE = $("#addIPSE").val();
$.post( $("#addIPSEform").attr("action"), inputIPSE, function(info){ $("#result").html(info); });
});
此外,不推荐使用mysql,您需要切换到PDO或MySQLi