简体   繁体   中英

Double form will not submit second form

Hi I have two HTML forms when the one is submit a JavaScript function submits the second the one form works but the second doesn't I'm not sure if it is the forms or the post pages can any one help.

The one form sends an email this is sending the email correctly sending the correct data to the correct email address.

The second form is meant to upload a file it doesn't seem to be doing anything at all there are now errors displayed to the screen I have done a try catch and nothing is displayed i have also looked into the logs and nothing is displayed I'm

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 /> ";
?>

the <?php echo $_SERVER["PHP_SELF"];?> on the second form calls the php at the bottom of the page this is the one that is working it is the upload_logo.php that is not currently working any help would be much appreciated

You're trying to submit 2 forms at once. That can't work, as your browser can only be directed to 1 page at a time, so your attempt to submit the upload form with JavaScript is cancelled by the contact form being submitted. I'd suggest that you move the file input into the same form as the contact fields, and handle them both in your "the user has submitted the form" section.

Something like this should do the trick:

<?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>

$uploadfile = ...之后尝试此操作

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

when you submit 2nd form e-mail would be generated becausue it triggers if(isset($_POST["subject"])) condition and the code will follow the next commands.

But when you submit 1st form, it will call onsubmit="alert(); function and that function again submits the same form because of these.

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

so you are just triggering a never ending loop.

My solution is

<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>

I'am not 100% sure about your requirement. hope you can get the point what i'am making. gl!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM