Ajax将数据插入数据库

时间:2015-10-17 12:52:28

标签: php jquery html ajax

我想使用Ajax插入数据。当我单击按钮(接受)时,应插入值1,并将按钮更改为已接受。我试过以下代码

<script>
    $(function(){
        $('#submit').on('click', function() {
            var name = $('#app').val();
            if (name.length > 0) {
                $.ajax({
                    url: 'approve.php',
                    method: 'POST'
                    data: { app: name },
                    success: function() {
                        alert("success");
                    }
                });
            }
        });
    });
</script>

HTML代码

<form action="" method="post">
    <input type="hidden" id="app" value="1"/>
  <input id='submit' type='submit' value='Accept'> 
</form>

approve.php是

$u_id  = $_SESSION['UserID'];
 $appinfo = $_POST['app'];
$sql = "UPDATE tbl_bides SET selected='$appinfo' WHERE bidder_id = '".$u_id."'" or die(mysql_error());
$Result = mysql_query($sql,$con) or die(mysql_error());

当我跑步时,没有任何事情发生。请帮忙!!

3 个答案:

答案 0 :(得分:1)

试试这个

HTML

<input type="hidden" value="1" id="app">
<input type="submit" value="SUBMIT" id="submit">

JS

$('#submit').click(function() {
var name = $('#app').val();

$.ajax({
    url: 'approve.php',
    data:"app="+ name,
    success:function(){
           alert("success");
    }
  });
});

approve.php(使用PDO)

include('connection.php');


$u_id  = $_SESSION['UserID'];

if(isset($_POST['app'])) {
    $appinfo  = $_POST['app'];

    $queryUpdate = $YourConnectionName->prepare("UPDATE tbl_bides SET selected=:appinfo WHERE bidder_id=:u_id");
    $queryUpdate->execute(array(':u_id' => $u_id, ':appinfo'=> $appinfo));
}

connection.php

$username = 'YourUsernameOfDatabase';
$password = 'YourPasswordOfDatabase';

try {
    $YourConnectionName = new PDO('mysql:host=Yourhost; dbname=YourDatabaseName', $username, $password);
    $YourConnectionName->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $YourConnectionName->exec("SET CHARACTER SET utf8");
} catch (PDOException $e) {
    echo 'You are not connected';
    $e->getMessage() . "<br/>";
    die();
}

答案 1 :(得分:1)

你的javascript错了。首先,您正在尝试设置“提交”#39;隐藏表单字段上的事件处理程序。只有<form>会自然触发submit事件。其次,您没有设置method参数,因此$ .ajax通过GET提交您的数据,这意味着您的PHP代码不会看到任何值。

最好在提交按钮上设置点击事件:

 <script>
    $(function(){
        $('#submit').on('click', function() {
            var name = $('#app').val();

            if (name.length > 0) {
                $.ajax({
                    url: 'approve.php',
                    method: 'POST'
                    data: { app: name },
                    success: function() {
                        alert("success");
                    }
               });
           }
        });
    });
</script>

你批准了.php:

$u_id  = $_SESSION['UserID'];
$appinfo = $_POST['app'];
$sql = "UPDATE tbl_bides SET selected='$appinfo' WHERE bidder_id = '$u_id'";
$Result = mysql_query($sql, $con) or die(mysql_error());

答案 2 :(得分:0)

 Add One more parameter in ajax for submitting post data 

    $.ajax({
                url: 'approve.php',
                type: 'POST'
                data:"app="+ name,
                success:function(){
                    alert("success");
                }
           }));