javascript - 双表不会提交第二表

标签 javascript php jquery html forms

嗨,我有两个 HTML 表单,当第一个表单提交时,JavaScript 函数提交第二个表单,其中一个表单有效,但第二个表单不起作用,我不确定是表单还是帖子页面可以提供帮助。

第一个表单发送一封电子邮件,这是正确发送电子邮件,将正确的数据发送到正确的电子邮件地址。

第二种形式是上传一个文件,它似乎根本没有做任何事情,现在屏幕上显示错误我已经做了 try catch ,但没有显示任何内容我也查看了日志,但什么也没有显示我是

HTML

        <div id="loginborder">


        <form id ="upload" enctype="multipart/form-data" action="upload_logo.php" method="POST">
                        <input name="userfile" type="file" />
                        <input type="submit" onsubmit="alert()"  value="dont press" disabled>
        </form>

            <div id="login">
                <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
                    <input type="hidden" name="subject" value="Can you create me a Contributors account">
                    <input type="text" name="first_name" id="first_name" placeholder="Name">
                    <input type="text" name="company" id="company" placeholder="Company Name">
                    <input type="checkbox" id="tc" onclick= "checkbox()">
                    <input type="submit" id="submit" onsubmit="alert()" name="submit" value="Register" disabled>
                </form>



    </div>
        </div>

        <?php 
            }
            else
            // the user has submitted the form
            {
            // Check if the "subject" input field is filled out
                if (isset($_POST["subject"]))
                {
                    sleep(5);
                    $subject = $_POST["subject"];
                    $first = $_POST["first_name"];
                    $company = $_POST["company"];
                    $therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";          
                }
                    echo "$therest <br>";
                    $first = wordwrap($first, 70);
                    mail("careersintheclassroom01@gmail.com",$subject,$name,$therest,"subject: $subject\n");
                    echo "Thank you for sending us feedback";
                    header( "refresh:5;url=index.php" );
            }
        ?>
    </body>
</html>

JavaScript

<script type="text/javascript">

                function alert() 
                {
                document.getElementById("upload").submit();
                }

                function checkbox(){
                    if (document.getElementById("tc").checked == true)
                        document.getElementById("submit").disabled = false;
                    else
                        document.getElementById("submit").disabled = true;
                }

                $('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
            </script>

Upload_Logo.php

<html>
    <head>
    </head>
</html> 

<?php
    $uploaddir = "./images/"; 

    echo $uploaddir;
    mkdir($uploaddir, true);
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo "<br />";
    echo " <b>Your media has been uploaded</b><br /><br /> ";
?>

<?php echo $_SERVER["PHP_SELF"];?>在第二种形式上,调用页面底部的 php,这是正在工作的,它是 upload_logo.php,目前尚未工作,任何帮助将不胜感激

最佳答案

您正尝试一次提交 2 个表单。这是行不通的,因为您的浏览器一次只能定向到 1 个页面,因此您使用 JavaScript 提交上传表单的尝试会被提交的联系表单取消。我建议您将文件输入移动到与联系人字段相同的表单中,并在“用户已提交表单”部分中处理它们。

像这样的事情应该可以解决问题:

<?php
if (!isset($_POST["submit"]))
{
    ?>
    <div id="loginborder">
        <div id="login">
            <form enctype="multipart/form-data" method="POST">
                <input name="userfile" type="file" />
                <input type="hidden" name="subject" value="Can you create me a Contributors account">
                <input type="text" name="first_name" id="first_name" placeholder="Name">
                <input type="text" name="company" id="company" placeholder="Company Name">
                <input type="checkbox" id="tc" onclick="checkbox()">
                <input type="submit" id="submit" name="submit" value="Register" disabled>
            </form>
        </div>
    </div>

    <?php 
        }
        else
        // the user has submitted the form
        {
        // Check if the "subject" input field is filled out
            if (!empty($_POST["subject"]))
            {
                sleep(5);
                $subject = $_POST["subject"];
                $first = $_POST["first_name"];
                $company = $_POST["company"];
                $therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";          
                echo "$therest <br>";
                $first = wordwrap($first, 70);
                mail("careersintheclassroom01@gmail.com",$subject,$name,$therest,"subject: $subject\n");
                echo "Thank you for sending us feedback";
                header( "refresh:5;url=index.php" );
            }

            if (isset($_FILES['userfile']['name'])) {
                $uploaddir = "./images/"; 
                if (!file_exists($uploaddir)) {
                    mkdir($uploaddir, true);
                }
                $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
                move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
                echo "<br />";
                echo " <b>Your media has been uploaded</b><br /><br /> ";
            }
        }
    ?>
</body>

关于javascript - 双表不会提交第二表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23337705/

相关文章:

php - MySQL SUM 函数

javascript - Twitter Bootstrap 在下拉菜单打开时停止传播

javascript - 为什么history.push在react js中不起作用?

javascript - 无法通过 jQuery 正确定位两个不同的下拉菜单

php - 使用 jquery 发布和返回数据会损坏 å ä ö

javascript - 推手 : No callbacks on testChannel for App\Events\Event

javascript - 仅重置表单中的字段集并保留所有其他值

jquery - 大图像背景的断断续续的动画

javascript - Javascript 中的原型(prototype)概念,我做对了吗?

javascript - 如何停止我的 JavaScript 倒计时?