I need help with this, when I press the submit button I want my echo to show in the page where i have my span, not in a new page like (../addIPSE.php)...
my 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>
my 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...";
}
?>
my addIPSE.js
$("#submitButton").click( function() {
var inputIPSE = $("#addIPSE").val();
$.post( $("#addIPSEform").attr("action"), inputIPSE, function(info){ $("#result").html(info); });
});
$("#addIPSEform").submit( function() {
return false;
});
Also when I press the submit button I first get to an all white page, then I have to refresh the page to see my echo, but the only echo I get is the "Already in DB..:", and it does not show in the span on my index page...
Your form is redirecting to your php file. You need to remove that action AND tell the submitButton click even to preventDefault():
$("#submitButton").click( function(e) {
e.preventDefault()
var inputIPSE = $("#addIPSE").val();
$.post( $("#addIPSEform").attr("action"), inputIPSE, function(info){ $("#result").html(info); });
});
Also, mysql is deprecated, you need to switch to PDO or MySQLi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With