I'm trying to post to MySQL with jQuery and PHP:
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
}
}
});
Then in the same folder I have the file insert_mysql.php
, and the code is
$nombre = $_POST['values'];
The problem is that the AJAX function does not work, and don't know exactly why. The code never goes into insertar_mysql.php
.
UPDATE:
i solved the problem, the file directory was wrong, but actually i have on more problem, chorme console says:
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
any advice , why is forbidden??
Replace your ajax function with following in this function i have added error event to make sure that is there any error in your ajax call.
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
}
});
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;
If insertar_mysql.php
in the like address:
[drive]:\\wamp\\www\\insertar_mysql.php
,You use url: "/insertar_mysql.php"
else like [drive]:\\wamp\\www\\new\\insertar_mysql.php
,You use url: "/new/insertar_mysql.php"
Try this code:
$.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);
}
});
I found in chrome console this
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
so i think my problem is the directory of my insertar_mysql-php file is not correct, i will try to find the exact directory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.