I am using a form to submit and update my database sitting on an external server. When I place all the files on the same server (PHP and html), I am able to update without any issues. But when I separate it out, leaving only the php file on the server and operating the html files from my computer, I am no longer able to update.
The first alert("submit")
in the JavaScript doesn't even fire off when I click submit for the form. I can't keep all the files on the server as the html part is to be converted to a HTML5 app. Is there anyway I can resolve this being abel to keep the files separate.
HTML Code
<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<form method="post" id="infoForm">
<input type="text" name="first_name" id="first_name" value="" placeholder="First Name" />
<input type="text" name="last_name" id="last_name" value="" placeholder="Last Name" />
<input type="text" name="email" id="email" value="" placeholder="Email" />
<button type="submit">Submit</button>
</form>
<script>
$('#infoForm').submit(function() {
alert("submit");
var postTo = 'http://www.examplesite.com/add.php';
$.post(postTo,({first_name: $('[name=first_name]').val(), last_name: $('[name=last_name]').val(), email: $('[name=email]').val()}),
function(data) {
alert("data is " + data);
if(data != "") {
// do something
} else {
// couldn't connect
}
},'json');
return false;
});
</script>
</body>
</html>
PHP Code:
<?php
$server = "localhost";
$username = "user";
$password = "pass";
$database = "db";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$firstname = mysql_real_escape_string($_POST["first_name"]);
$lastname = mysql_real_escape_string($_POST["last_name"]);
$email = mysql_real_escape_string($_POST["email"]);
$sql = "INSERT INTO personnel (first_name, last_name, email) ";
$sql .= "VALUES ('$firstname', '$lastname', '$email')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
echo "Comment added";
}
mysql_close($con);