weixin_33717298 2015-02-08 13:13 采纳率: 0%
浏览 2

AJAX与PHP / MySQL对话

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.

  • 写回答

1条回答 默认 最新

  • Didn"t forge 2015-02-08 13:57
    关注

    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");
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效