使用javascript从表单发布值,然后使用php发布

时间:2012-08-20 13:39:59

标签: php javascript uploadify

我正在尝试将一些信息插入数据库,我的第一页是:

$(document).ready(function() {

    //alert('I am ready to use uploadify!');
    $("#file_upload").uploadify({
        'uploader': 'uploadifyit/uploadify.swf',
        'script': 'uploadifyit/uploadify.php',
        'cancelImg': 'uploadifyit/cancel.png',
        'folder': 'images',
        'auto': false, // use for auto upload
        'multi': true,
        'queueSizeLimit': 4,
        'onQueueFull': function(event, queueSizeLimit) {
            alert("Please don't put anymore files in me! You can upload " + queueSizeLimit + " files at once");
            return false;
        },
        'onComplete': function(event, ID, fileObj, response, data) {
            // you can use here jQuery AJAX method to send info at server-side.
            $.post("insert.php", { name: fileObj.name }, function(info) {
                alert(info); // alert UPLOADED FILE NAME
            });
        }
    });



});

</script>
</head>

<body>
<?php
$img_id=$_REQUEST['img_id'];
?>
<form id="form1" name="form1" action="">
<input type="file" name="file_upload" id="file_upload" /><br />
<input type="hidden" value="<? echo $img_id ;?>" name="image_id"  />

<a href="javascript:$('#file_upload').uploadifyUpload();">Upload File</a>
</form>

虽然处理表格是:

if(isset($_POST)) {

        //echo $_POST['name'];
        $fileName = $_POST['name'];
        $id = $_REQUEST['image_id'];


        mysql_query("INSERT INTO tbl_images(img_file,img_gal_id) VALUES('$fileName','$id')");
        $inserted_id = mysql_insert_id($dbc);

        if($inserted_id > 0) { // if success
            echo "uploaded file: " . $fileName;
        }

    }

我可以发布$ fileName但不能发布$ id。

2 个答案:

答案 0 :(得分:1)

在处理表单中使用POST代替REQUEST,并将image_id值与JSON一起传递为软/ MMK推荐值。您可以保存隐藏字段和变量声明,并将值注入$.post

$.post("insert.php", 
  { 
    name: fileObj.name, 
    // Add the image_id value to post to insert.php
    image_id: <?php echo $_REQUEST['img_id']; ?> 
  ...

然后处理:

$fileName = $_POST['name'];
// Grab the posted value.
$id = $_POST['image_id'];

答案 1 :(得分:0)

试试这个

'onComplete': function(event, ID, fileObj, response, data) {
        // you can use here jQuery AJAX method to send info at server-side.
        $.post("insert.php", { name: fileObj.name,image_id:ID}, function(info) {
            alert(info); // alert UPLOADED FILE NAME
        });
    }