Reputation: 284
I am struggling with getting AJAX to work with a form, passing data through to some PHP/MySQL.
Here is the code - HTML:
<form id="<?=$applicationKey?>" name="<?=$applicationKey?>" action="./post.<?=$appNo?>.<?=$applicationKey?>.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="uID" value="<?=$uID?>">
<input type="hidden" name="uaID" value="<?=$uaID?>">
<input type="hidden" name="appID" value="<?=$appID?>">
<input type="text" id="input_1_1" name="input_1_1" placeholder="Name of the Applicant's Organisation" value="<?=$input_1_1?>" required>
<input type="text" id="input_1_2" name="input_1_2" placeholder="First Name" value="<?=$input_1_2?>" required>
<input type="text" id="input_1_3" name="input_1_3" placeholder="Last Name" value="<?=$input_1_3?>" required>
Etc (I'm sure you don't need the entire form listed out here). I have this JS as a separate .js file, loaded in the header of the HTML:
function doSend_1_1() {
$.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_1', $('#input_1_1').serialize());
}
function doSend_1_2() {
$.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_2', $('#input_1_2').serialize());
}
function doSend_1_3() {
$.post('./post.4.ConSupAp.php?appID=' + (appID) + '&ident=input_1_3', $('#input_1_3').serialize());
}
$("document").ready(function() {
$("#input_1_1").blur(doSend_1_1);
$("#input_1_2").blur(doSend_1_2);
$("#input_1_3").blur(doSend_1_3);
})
And this sort of works well. When a user leaves either of the three fields, it does try to throw the data through to the PHP and make it do what I want it to do. The PHP looks like this:
<?php
include './conf/Funcs.php'; // Grab our functions
include './conf/DBconfig.php'; // Useful to have access to the database
$appID = $_GET['appID']; // identifies what entry we are talking to in the DB
$ident = $_GET['ident']; // defines what field is being posted
if(($ident) == "input_1_1") {
$userInput = $_POST['input_1_1'];
try {
$stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `organisation` = :userinput AND `lastModified` = :time WHERE `appID` = :appid");
$stmt->bindParam(':userinput', $userInput, PDO::PARAM_STR, 64);
$stmt->bindParam(':time', time(), PDO::PARAM_INT, 11);
$stmt->bindParam(':appid', $appID, PDO::PARAM_INT, 11);
$stmt->execute();
} catch(PDOException $e) { catchMySQLerror($e->getMessage()); }
}
?>
As I have not successfully got it working for the first input field I've not added sections in the php for using the other fields as yet. So yes they are currently missing.
Firebug reports there are no problems, and the information in the organisation field within the database does change from it's previously preset entry to "0" - not what I type into the input field. Also the time() stamp does not update even when the weird 0 value is input into the database. With no errors and Firebug seemingly happy, I'm at a loss as to why my data is not being inserted correctly. Firebug does report that whatever is input into the text field is passed correctly to the php. Any ideas exactly what it is that is going wrong?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
This is being utilised, loaded in the originating page head. The database is MySQL. All php communication with the DB is done using PDO.
Upvotes: 2
Views: 166
Reputation: 9157
The problem seems to be in that line:
$stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET `organisation` = :userinput AND `lastModified` = :time WHERE `appID` = :appid");
AND
operator shouldn't be in that place. Repalace it with comma.
You could also set the time()
directly in your query, as it doesn't have to be prepared
$stmt = $conn->prepare("UPDATE $database.app_ConSupAp SET organisation = :userinput , lastModified = '".time()."' WHERE appID = :appid");
Upvotes: 2