0

嗨,我有这个简单的代码:

var datastring="123";
$.ajax({ 
        url: 'actualizarimagen.php',
        type: 'post',
        dataType: 'text',
        data: datastring,
        cache: false,
        success: function(response){
            $('.msg1').html(response);

         },
        error: function(response){
            $('.msg1').html(response);
         }

    });

在actualizariimagen.php 中:

$desc_larga = print('<pre>') & print_R($_POST) & print('</pre>');
$insertSQL = sprintf("INSERT INTO prueba (texto) VALUES ($desc_larga)");

我收到成功消息,但在数据库中始终保存 1。我尝试更改所有内容,dataType,成功,错误,完整功能,但它不起作用。我正在搜索,但任何答案都无济于事。

谢谢。

编辑:添加响应

4

7 回答 7

5

datastring应该包含编码为的数据application/x-www-form-urlencoded

例如:var datastring="foo=123";

最好不要将字符串传递给 jQuery。传递一个对象,让它为你处理编码。

例如:data: { "foo": "123" }

于 2012-05-17T16:39:45.507 回答
1

要发送数据,有要遵循的格式。像

var datastring="var1=123&var2=abcd";

或者

var datastring=[{name:'var1',value:123},{name:'var2',value:'abcd'}];

第二种格式(对象名称值数组)类似于<input type="text" name="var1" value="123">html 输入元素具有要发布的名称和值的位置。

然后,您可以通过以下方式获取值:

$_POST['var1']  

或者

$_POST['var2']  
于 2012-05-17T16:37:58.270 回答
1

数据 对象,字符串

要发送到服务器的数据。如果还不是字符串,则将其转换为查询字符串。它附加到 GET 请求的 url。请参阅 processData 选项以防止此自动处理。对象必须是键/值对。如果 value 是一个 Array,jQuery 会根据传统设置的 value 序列化多个具有相同 key 的值(如下所述)。

您只是将 123 发送到服务器。

它应该是这样的

var datastring="myField=123";

或者

var datastring = {"myField" : 123 };

并且使用 PHP 你会阅读它

$_POST["myField"]  
于 2012-05-17T16:41:39.257 回答
1

轻松实现此目的的示例可能是:

JS:

var datastring="123";

$.post('actualizarimagen.php', { datastring:datastring }, function(data){
     if(data != 0){
        $('.msg1').html('correcto');
     } else {
        $('.msg1').html('error');
     } 
});

在您的 actualizariimagen.php 中:

if($_POST() && isset($_POST['datastring'])){

/* Connect to DB */
$link = mysql_connect('server', 'user', 'pwd');
if (!$link) {
    // No connection
    print(0);
    exit();
}

$db = mysql_select_db('db', $link);
if (!$db) {
    // DB selection error
    print(0);
    exit();
}

/* Sanitize the value */
$datastring = mysql_real_escape_string($_POST['datastring']);

// I don't understand here what you tried to do with $dec_larga but this is what I thought
$desc_larga = "<pre>".$datastring."</pre>";

/* Insert to DB */
$sql = "INSERT INTO prueba (texto) VALUES ('$desc_larga')";

if(mysql_query($sql,$link)){
    // Everything is Ok at this point
    print(1);
} else {
    // Error happened in your SQL query
    print(0);
}

}
于 2012-05-17T16:57:03.507 回答
0

在 ajax 调用中:

data: my_var : datastring,

在php中:

$desc_larga = '<pre>'.$_POST['my_var'].'</pre>';
于 2012-05-17T16:40:16.397 回答
0

尝试更换

type: "post",

type: "POST",

你的数据串应该是这样的:

single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1

如此处所述:

http://api.jquery.com/serialize/

于 2012-05-17T16:42:10.230 回答
0
var datastring = "123";
$.ajax({ 
   url: 'actualizarimagen.php',
   type: 'post',
   dataType: 'text',
   data: {data : datastring},
   cache: false
}).always(function(response) {
   $('.msg1').html(response);
});

在actualizariimagen.php 中:

$desc_larga = '<pre>'.$_POST['data'].'</pre>';
$query =  '"INSERT INTO prueba (texto) VALUES ('.$desc_larga.')"';
于 2012-05-17T16:49:45.723 回答