简体   繁体   English

jQuery $ .ajax到php问题

[英]jquery $.ajax to php problem

i have two problems with jquery $.ajax. 我有jquery $ .ajax两个问题。 first problem is ihave a php file named action.php and here the code: 第一个问题是我有一个名为action.php的php文件,代码如下:

if($_GET['action']=='add'){
    //rest of the code here
}

And i use jquery $.Ajax function to call that when form fills: 我使用jQuery $ .Ajax函数在表单填充时调用它:

$.ajax({type:"POST", url:"action.php?action=add", data:$("#form").serialize(), cache:false, timeout:10000});

this works but i wanted to know is there anyway to send the action=add code with data and not the url? 这可行,但我想知道是否仍然有发送action = add代码和数据而不是URL?

and the second problem that i have is that i have a link: 第二个问题是我有一个链接:

<a href="#" onclick="delete(4);">delete row from mysql where id is 4</a>

and a jquery function: 和一个jQuery函数:

    function deleteUser(id){
    $.ajax({type:"POST", url:"action.php?action=delete", data:"id="+id, cache:false, timeout:10000});}

and of course the action.php code: 当然还有action.php代码:

if($_GET['action']=='deletestudent'){
    mysql_query("DELETE FROM `students` WHERE `student_id` = {$_POST['id']}");
}

but it doesn't work.what should i do? 但它不起作用。我该怎么办?

First part: Yes 第一部分:

var postData = $("#form").serialize();
postData.action = 'add';
$.ajax({
    type:"POST"
  , url: "action.php"
  , data: postData
  , cache: false
  , timeout:10000
});

For the 2nd part: that isn't working because your "action" values are not congruent: delete vs deletestudent . 对于第二部分:这是行不通的,因为您的“操作”值deletestudentdelete vs deletestudent Nor are your function names: delete() vs deleteUser() 您的函数名称也不是: delete() vs deleteUser()

Also, I'd recommend applying some SQL injection protection in that query as well. 另外,我建议在该查询中也应用一些SQL注入保护。

You have a function deleteUser() and you are using delete() even you're sending post action is delete while you're php script is looking for deletestudent 你有一个函数deleteUser()并且使用delete() ,即使你发送后动作delete ,而你的PHP脚本正在寻找deletestudent

make your onclick onclick="deleteUser(4);" 使您的onclick onclick="deleteUser(4);"

and change your action from 并从

$.ajax({url:"action.php?action=delete&id="+id, cache:false, timeout:10000});}

to

$.ajax({url:"action.php?action=deletestudent&id="+id, cache:false, timeout:10000});}

For the first problem: 对于第一个问题:

You can add to your form a hidden input with the name/value you need. 您可以在表单中添加具有所需名称/值的隐藏输入。 Example: 例:

<input type="hidden" name="action" value="add" />

For the second problem: 对于第二个问题:

According to your code, it seems that you are send "delete" but in the condition you are testing if equal to "deletestudent", maybe that's your problem. 根据您的代码,似乎您发送的是“删除”,但在测试条件是否等于“删除目标”的情况下,也许这就是您的问题。

chnage the type to GET o remove it, $.ajax default type is GET 将类型更改为GET或将其删除,$。ajax默认类型为GET

$.ajax({url:"action.php?action=delete&id="+id, cache:false, timeout:10000});}

in php change your 在PHP中更改您的

....WHERE `student_id` = {$_GET['id']}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM