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


Post a value from form using javascript then postin with php

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

$(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。

在处理表单中使用POST而不是REQUEST,并将image_id值与JSON一起传递为soft/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'];

像这样尝试

'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
        });
    }