0

所以我有一个使用 jquery $.ajax 调用 php 脚本的表单。我的问题是我还没有找到任何方法来发送文本输入和文件输入或图像。我可以发送文本,只是不能发送图像。

这是我得到的:

Javascript:

别的 {

        $.ajax({
            type: 'POST',
            url: 'test1.php',
            cache: false,
            data: $(".contact_form").serializeArray(),
            success: function (data) {
                if (data == "error") {
                    $('.success_box').hide();
                    $('.error_box').show();
                }
                else {
                    $('#sname').val('');
                    $('#email').val('');
                    $('#title').val('');
                    $('#message').val('');
                    $('#photo1').val('');
                    $('#photo2').val('');
                    $('.error_box').hide();
                    $('.success_box').show();
                }
            }
        });
    }

PHP:

    <?php
$sname=$_POST['sname'];
$email=$_POST['email'];
$title=$_POST['title'];
$description=$_POST['message'];
$photo1=$_FILES['photo1']['tmp_name'];
$photo2=$_FILES['photo2']['tmp_name'];

$link = mysql_connect("localhost","root","root");

if (!$link) {
    die(mysql_error());
    echo('link stop');  
}
$db = mysql_select_db("test");

if (!$db) {
    die(mysql_error()); 
    echo('braaahh');
}   
    $imge1 = addslashes(file_get_contents($_FILES['photo1']['tmp_name']));
    $imge2 = addslashes(file_get_contents($_FILES['photo2']['tmp_name']));

if (!mysql_query("INSERT INTO forSale VALUES ('', '$sname', '$email', '$title',         '$description', '$imge1', '$imge2')")) {
    die(mysql_error());
    echo("help");
}   

mysql_close();
    ?>

谢谢

4

1 回答 1

0

我个人为此使用了jquery 表单插件。另外,请查看这篇文章:使用 AJAX 上传文件

于 2012-11-12T07:07:19.873 回答