doutan2456 2013-09-04 05:00
浏览 30
已采纳

javascript,ajax,PHP insert onSubmit not working ... POST未设置

I'm trying to insert into my MySQL database with an onSubmit() function... For the main part, my function works. However, it keeps inserting a 1 into my DB where the logbook_id field is (the logbook_id should NOT be a 1 every time). Here's my (relevant) html:

echo '<div class="logbook_box" id="logbook'.$id.'" 
      style="display: block; overflow: hidden;">';
echo '<form action="cgi-bin/read_log.php" method="post"
      onSubmit="return ajaxSubmit(this, ' . $id . ');">' . 

'<input type="hidden" value="'.$id.'" id="id" name="id" />'.
'<input style="float: right; width: 20%" type="submit" value="Mark as Read" />' .
'</form>';

Here's the onSubmit function:

<script type="text/javascript" src="cgi-bin/jquery-2.0.3.js"></script>
<script>
var ajaxSubmit = function(form, id) {
    div = document.getElementById("logbook" + id);
    div.style.display = "none";
        // fetch where we want to submit the form to
        var url = $(form).attr('action');

        var id = $('#id').val();

    // setup the ajax request
    $.ajax({
    url: url,
    data: { id : id },
    dataType: "json",
    success: function() {
        if(rsp.success) {
            alert('Marked as read, thanks!');
            }
    }
    });

     // return false so the form does not actually
     // submit to the page
     return false;
}
</script>

And here's my PHP file:

<?php

include('/var/www/olin2/includes/functions.php');
$con = connect_db();
session_start();

$id = isset($_POST['id']);
$username = $_SESSION['username'];

$query = 'insert into read_logbook(logbook_id, username) 
          values("'.$id.'",     "'.$username.'")';
mysqli_query($con, $query) or die(mysqli_error($con));


?>

So, the <div> disappears like it should when Submit is clicked, which shows that the id is getting passed correctly. The PHP script is called, but it either doesn't input anything into the database, or (even more strangely) it will submit my username and the value 1 into the database if I change the PHP from $_POST to $_GET without changing the method in the form.

I'm new at ajax/javascript, so I'm sure it has something to do with the function, but I'm confused and at a complete loss... Any ideas?

  • 写回答

2条回答 默认 最新

  • dongyuruan2957 2013-09-04 05:04
    关注

    add type option as post to your ajax since you are retriving the value as post...default is get..

     $.ajax({
      url: url,
      data: { id : id },
      type: "POST", //<---here
      ....
    

    and make sure the id of logbook_box is different and not 1 all the time

    and change this

    $id = isset($_POST['id']);   //this return either true or false. so your $id is either 0 or 1
    

    try this

    $id = isset($_POST['id'])?$_POST['id']:""; //or $_POST['id']; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题