dragon87836215 2018-07-28 23:09
浏览 61

Ajax忽略SQL

I have other functions just like this that work, however when I click, the Ajax gets posted but my page would originally refresh, until I added event.preventDefault. I have been stumped for hours and I cant see the problem..

Here is my AJAX:

 $(function(){
$(document).on('click','.addnewproject',function(){
    event.preventDefault();
    var curaid= $(this).attr('id');
    var $ele = $(this);
    $.ajax({
        type:'POST',
        url:'components/sql/insertproject.php',
        data:$("#addnewprojecttable").serialize(),
        success: 
        function(data){ 
        $(".image-list" + curaid).append(
                '<li id="projectfolder' + curaid + '">
\
           <img width="35px" onclick="openproject('+ curaid +')" style="cursor: pointer;" src="img/folder.png" /> 
\
            <p>Project ' + curaid + '</p> 
\
            ');
        }

        });
    });

});

My HTML:

<form  id="addnewprojecttable" method="post" >
<table>
<tr> 
<td> 
<input style="display:none;" name="username" id="username" value="<?php  echo "" . $_SESSION['username']; ?>" type="text"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="projectname" id="projectname" type="text" placeholder="projectname"/>
<input style="float:left; width:100%; height:20px; margin-bottom: 20px;" name="contactname" id="contactname" type="text" placeholder="contactname"/>
<input style="float:left; width:100%; height:20px;  margin-bottom: 20px;" name="contactemail" id="contactemail" type="text" placeholder="contactemail"/>
<input style="float:left; width:100%; height:20px;  margin-bottom: 20px;" name="contactphone" id="contactphone" type="text" placeholder="contactphone"/>

<input style="float:left; width:100%; height:20px;" name="description" id="description" type="text" placeholder="description"/>
<input style="float:left; width:100%; height:20px;" name="notes" id="notes" type="text" placeholder="notes"/>



<button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
<button  onclick="closenewprojectwindow()">Cancel</button>
</td> 
</tr>

</table>
</form>

My SQL:

<?php
include 'config.php';

$sqlupdateincome = "INSERT INTO projects (username, projectname)
VALUES ('". $_POST['username'] ."', 'Ochrom Test Project')";

if ($conn->query($sqlupdateincome) === TRUE) {
    echo "New record created successfully";
} else {
     echo  "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Here is my database just in case it might be that. click for image



I do appreciate all help and responses, thank you!

  • 写回答

1条回答 默认 最新

  • dongyouzhui1969 2018-07-28 23:47
    关注

    Lets break this down. You have a form tag, which means you want to submit a form. Without an action attribute, you will be at the will of the browser, but pretty much all will submit the form the same page.

    <form  id="addnewprojecttable" method="post">
    

    Further down, still within the form, you have a button. The default action of a button is to submit the form unless you have modified the function of the button with Javascript, which is what you kind of have.

    <button class="addnewproject" id="<?php echo $row["accid"]; ?>" >Add</button>
    

    I do NOT use JQuery at all, but I can read it enough to understand what you are doing. Your modification code for the button is saying. Do NOT submit the form with this line:

    event.preventDefault();
    

    But then continue with the rest of the code in the function.

    Without this line, the rest of the function will still be actioned and the form will be submit to itself as well (default action of the HTML). It may also be possible that the browser does not finish actioning the Javascript function before the form submission (inconsistent results - not a good thing).

    Depending on your layout, sometimes you may need the following, but that is really another discussion.

    event.stopPropagation();
    event.preventDefault();
    

    It is good practice to allow your HTML to work without Javascript enabled as well. For this reason, including an action in your form would also be something I would recommend.

    Take note: Your SQL is open to SQL Injection as well.

    Edit:

    What is the full path to config.php and components/sql/insertproject.php?

    On insertproject.php, add the following to the top:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    And then happens when you go directly to the URL /components/sql/insertproject.php? Is there an error displayed?

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题