douqian1296 2016-04-06 01:13
浏览 287

使用POST提交按钮而不是使用HTML表单

I have a form inside a form and this makes the top form unresponsive. When I take off the second form (which is inside the first form), the first form works. This is the second form I have:

<form action="imgupload.php" method="post" enctype="multipart/form-data">
            <h3>Upload a new image:</h3>
            <input type="file" name="fileToUpload" id="fileToUpload">
            <br>
            <input type="hidden" value="<?php echo $row['Gallery_Id']; ?>" name="gid">
            <input type="hidden" value="User" name="user">
            <input type="submit" value="Upload Image" name="imgup">
</form>

Since this makes the first form not work, I was wondering if I can take off the form fields and then the submit button can send the form data to the imgupload.php like this.

<input type="file" name="fileToUpload" id="fileToUpload">
<input type="hidden" value="<?php echo $row['Gallery_Id']; ?>" name="gid">
<input type="hidden" value="User" name="user">
<input type="submit" value="Upload Image" name="imgup" action="imgupload.php" method="post" enctype="multipart/form-data">

This does not work now. Is there a way I can get this working? If not, what's an alternative way to send this data to the other php?

  • 写回答

1条回答 默认 最新

  • dongpo5264 2016-04-06 01:19
    关注

    Since you are uploading files, have a look at Ravi Kusuma's Hayageek jQuery File Upload plugin. It's simple, it's a Swiss Army Knife, and it works.

    Study the examples.

    http://hayageek.com/docs/jquery-upload-file.php

    Ravi breaks down the process into three simple steps, that basically look like this:

    <head>
        <link href="http://hayageek.github.io/jQuery-Upload-File/uploadfile.min.css" rel="stylesheet">  // (1)
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="http://hayageek.github.io/jQuery-Upload-File/jquery.uploadfile.min.js"></script>   // (1)
    </head>
    <body>
        <div id="fileuploader">Upload</div>  // (2)
        <script>
            $(document).ready(function(){
                $("#fileuploader").uploadFile({  // (3)
                    url:"my_php_processor.php",
                    fileName:"myfile"
                });
            });
        </script>
    </body>
    

    The final step is to have the PHP file specified in the jQuery code (in this case my_php_processor.php) to receive and process the file:

    my_php_processor.php:

    <?php
        $output_dir = "uploads/";
        $theFile = $_FILES["myfile"]["name"];
        move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
    

    Note the relationship between myfile in the PHP ($_FILES["myfile"]), and the filename specified in the jQuery code block.

    Don't forget to check out the server-side code from the Server Side tab -- you need both parts (js and php).


    Looking at your question again, you will probably want to use this functionality as well:

    dynamicFormData: function()
    {
        var data ={ location:"INDIA"}
        return data;
    }
    

    or

    dynamicFormData: function(){
        return {
            newID: $("#newNID").val(),
            newSubj: $("#newSubj").val(),
            newBody: $("#newBody").val(),
            formRole: $('#formRole').val()
        };
    

    These will appear on the PHP side, thus:

    $newID = $_POST['newID'];
    $subj = $_POST['newSubj'];
    etc
    

    As with any plugin, resist the temptation to just plop it into your code. Do a couple of quick-and-dirty tests with it first. Kick its tires. Fifteen minutes will save you two hours.

    And don't forget to verify what was uploaded. You never know when a developing country black hat might be trying to get a new account.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)