Javascript 双表不提交第二表

Javascript 双表不提交第二表,javascript,php,jquery,html,forms,Javascript,Php,Jquery,Html,Forms,您好,我有两个HTML表单,一个是提交,一个是JavaScript函数提交,第二个是一个表单工作,但第二个不工作,我不确定是表单还是帖子页面可以提供任何帮助 一个表单发送电子邮件这是正确发送电子邮件,将正确的数据发送到正确的电子邮件地址 第二个表单是用来上传一个文件的,它似乎什么都没有做,现在屏幕上显示了错误,我做了一次尝试,没有显示任何内容,我也查看了日志,没有显示任何内容 HTML <div id="loginborder"> <form

您好,我有两个HTML表单,一个是提交,一个是JavaScript函数提交,第二个是一个表单工作,但第二个不工作,我不确定是表单还是帖子页面可以提供任何帮助

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

第二个表单是用来上传一个文件的,它似乎什么都没有做,现在屏幕上显示了错误,我做了一次尝试,没有显示任何内容,我也查看了日志,没有显示任何内容

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>


$uploadfile=…

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);

您试图同时提交两份表格。这是行不通的,因为你的浏览器一次只能指向一个页面,所以你试图用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>


当您提交第二份表单时,将生成电子邮件,因为它会触发
if(isset($\u POST[“subject”])
条件,并且代码将遵循下一个命令

但是,当您提交第一个表单时,它将调用
onsubmit=“alert();
函数,并且由于这些原因,该函数再次提交相同的表单

function alert() 
                {
                document.getElementById("upload").submit();
                }
所以你只是在触发一个永无止境的循环

我的解决办法是

<script type="text/javascript">
function alert() 
                {


                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>

函数警报()
{
函数复选框(){
if(document.getElementById(“tc”).checked==true)
document.getElementById(“提交”).disabled=false;
其他的
document.getElementById(“提交”).disabled=true;
}
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder])。placeholder();

我不能100%确定您的要求。希望您能理解我的意思。gl!

请更具体地说明“当前不工作”的含义。您是否收到任何错误消息(显示在网站或日志中)?我已经更新了问题@John,但是没有,屏幕上或日志中都没有显示错误。我尝试在函数周围添加一个try catch,但是它没有显示任何想法??我不确定它是否真的进入了php页面,因为它似乎没有尝试上传文件。我想问题是你的“警报”函数(
onsubmit=“alert()”
)。alert由webbrowser(标准!)定义,并提供一个模式对话框。@adrianpruss在顶部我指定了一个名为alert的函数,因此它不应该调用该函数,通常该框将位于类似于
alert(“hello”)的函数中
当然可以,但在其他浏览器中可能会出现问题。在您的浏览器中,这似乎是可行的,…重命名方法/函数,您就处于安全状态。
警报(“hello”);
警报(“hello”)
对许多浏览器来说都是一样的。好吧,知道我该怎么做吗?我不知道我该如何将两者结合起来而不发生冲突@NiallOk,我已经移动了你的一些代码,为你形成了一个解决方案,它以相同的形式处理两组输入。在尝试你的示例时,我得到一个500内部服务器错误,但我在日志中找不到任何东西@我的NIALLY片段是从你的文章中改编的,但是我在开始时添加了“IF”,这是你中间的“其他”所暗示的——如果你复制并粘贴我的,请确保你没有2个开头“如果”。"现在是。如果你不给HTML表单一个操作,默认情况下它会提交到当前页面,所以你不需要指定它。这不起作用。我开始认为Niall的想法可能是个问题。任何想法我都知道如何将这两个表单放在一起这是一个很好的解决方案,但从外观上看,我需要一个解决方案,比如将两个表单放在一起Niall提出了一些关于我如何做到这一点的想法??
function alert() 
                {
                document.getElementById("upload").submit();
                }
<script type="text/javascript">
function alert() 
                {


                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>