Ajax调用php函数

时间:2013-08-07 08:01:51

标签: php jquery mysql ajax

我正在尝试使用jQuery和PHP发布到MySQL:

jQuery的:

var data = $("#prueba").text();
$.ajax({
  type: "POST",
  url: "insertar_mysql.php",
  data: {
    values: data
  },
  success: function(msg) {
    if (msg) {
      alert('success'); //testing purposes
    } else {
      alert('fail'); //testing purposes
    }
  }
});

然后在同一文件夹中我有文件insert_mysql.php,代码是

$nombre = $_POST['values'];

问题是AJAX功能不起作用,并不确切知道原因。代码永远不会进入insertar_mysql.php

更新:

我解决了问题,文件目录错了,但实际上我遇到了更多问题,chorme控制台说:

    POST http://uplaber.com/manager/insertar_mysql.php 403 (Forbidden) jquery-latest.js:8706
    send jquery-latest.js:8706
    x.extend.ajax jquery-latest.js:8136
(anonymous function) uplaber-manager:988
x.event.dispatch jquery-latest.js:5095
v.handle

任何建议,为什么被禁止?

4 个答案:

答案 0 :(得分:0)

用此函数中的以下替换你的ajax函数我添加了错误事件以确保你的ajax调用中有任何错误。

var data = $("#prueba").text();
$.ajax({
  type: "POST",
  url: "insertar_mysql.php",
  data: {
    values: data
  },
  success: function(msg) {
    if (msg) {
      alert('success'); //testing purposes
    } else {
      alert('fail'); //testing purposes
    }
  },
  error:function(e){
      alert("something wrong"+ e) // this will alert an error
  }
});

答案 1 :(得分:0)

var data = $("#prueba").text();
var dataString = "value="+data;
$.ajax({
  type: "POST",
  url: "insertar_mysql.php",
  data: dataString ,
  success: function(msg) {
      alert(msg); //testing purposes
  }
});

PHP

$nombre = $_POST['value'];
echo $nombre;

答案 2 :(得分:0)

如果地址中有insertar_mysql.php
[drive]:\wamp\www\insertar_mysql.php,您使用url: "/insertar_mysql.php"之类的[drive]:\wamp\www\new\insertar_mysql.php,使用url: "/new/insertar_mysql.php" 试试这段代码:

$.ajax({
    url:"insertar_mysql.php",
    data:{values: data},
    dataType:"html",
    cache:false,
    context:document.body,
    type:"POST",
    success: function(result) {
        if (msg) {
            alert('success'); //testing purposes
        } else {
            alert('fail'); //testing purposes
        }
    },Error: function(xhr,ajaxOptions,thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
}); 

答案 3 :(得分:0)

我在Chrome控制台中找到了这个

POST http://uplaber.com/index.php/es/insertar_mysql.php 404 (Not Found) jquery-  latest.js:8706
send jquery-latest.js:8706
x.extend.ajax jquery-latest.js:8136
(anonymous function) uplaber-manager:988
x.event.dispatch jquery-latest.js:5095
v.handle jquery-latest.js:4766

所以我认为我的问题是我的insertar_mysql-php文件的目录不正确,我会尝试找到确切的目录。