dougu5847 2019-06-05 07:40
浏览 66

无法在提交网络表单后发送电子邮件

I have a web form which a user fills out, and I'm aiming for a notification email to be sent out advising a ticket has been raised. I have 2 seperate php pages, one titled index.php which hosts the form & sendEmail.php which holds the email details.

If I include sendEmail.php within index.php it works, but I found every time the page was refreshed the email sent, so I've been trying to modify the code to only run upon the user clicking 'submit'

  <?php
  if(isset($_POST["submit"])){
         $to  = 'email1@domain.com';
         $subject = 'test subject';
         $message = 'test message';
         $headers  = 'Cc: email2@Sdomain.com';
         mail($to,$subject,$message,$headers);
       }
       else
       {
         echo 'Error';
       }
  ?>

Using the above code nothing happens, I don't get an error nor do I receive an email. My button has an input type and name both called 'submit'

EDIT: HTML Form below...

    <div id="NewTicket" class="tabcontent">
        <form id="newForm" action="">
            <p style='font-weight: bold'>Which area do you want to raise?
            <select id='businessDropdown'; name='businessDropdown'; onchange='businessChanged(this.value)'; style='background-color: #ffff99; margin-left: 15px';>
            <option value="">- Select business -</option>
    <?
    if (isset($business)) {
        if ($business=="PSM") {
            echo "<option value='PSM' selected>PSM</option>
";
            echo "<option value='CMS'>CMS</option>
";
        } else {
            echo "<option value='PSM'>PSM</option>
";
            echo "<option value='CMS' selected>CMS</option>
";

        }
    } else {
        echo "<option value='PSM'>PSM</option>
";
        echo "<option value='CMS'>CMS</option>
";
    }
    ?>
    </select>
    </p>

    <p style='font-weight: bold'>Filter by a particular model?
    <select style='min-width: 50%; max-width: 90%; background-color: #ffff99; margin-left: 50px' id='modelDropdown' name='modelDropdown' onchange='modelChanged(this.value)'>
    <option value="">- Select business first -</option>
    </select>
    </p>


    <p style='font-weight: bold'>What board do you want to look at?
    <select style='min-width: 50%; max-width: 90%; background-color: #ffff99' id='boardDropdown' name='boardDropdown' onchange='boardChanged(this.value)'>
    <option value="">- Select business first -</option>
    </select>
    </p>
    <p>
    <input id='showAllBoardsCheckbox' type='checkbox' onclick='toggleShowAllBoards()'><label for="showAllBoardsCheckbox">Show all boards</label>
    </p>
    <p style='font-weight: bold'>What side do you want to see?
    <select id='sideDropdown' name='sideDropdown' onchange='sideChanged(this.value)' style='background-color: #ffff99; margin-left: 35px';>
    <option value="">- Select board first -</option>
    </select>
    </p>

    <p style='font-weight: bold'>Which revision do you want to use?
    <select id='revisionDropdown' name='revisionDropdown' onchange='revisionChanged(this.value)' style='background-color: #ffff99';>
    <option value="">- Select board first -</option>
    </select>
    </p>

    <div id='lineDropdownContainer' name='lineDropdownContainer'>
    <p style='font-weight: bold'>Which line are they running down<br>or where have they been built?
    <select id='lineDropdown' name='lineDropdown'  onchange='lineChanged(this.value)' style='background-color: #ffff99; margin-left: 32px';>
    <option value="">- Select board first -</option>
    </select>
    </p>
    </div>


    <label for="Category"><b>Category:</b></label>
        <select id="categoryDropdown" name="categoryDropdown" style='background-color: #ffff99; margin-left: 175px';>
        </select><BR>
<script>
  function validateCategory() {
    var x = document.forms["newForm"]["categoryDropdown"].value;
    if (x == "") {
      alert("A Category must be provided");
      return false;
    }
  }
</script>

    <label for="Reason"><b>Reason for Change/Action:</b></label>
        <select id="reasonDropdown" name="reasonDropdown" style='background-color: #ffff99; margin-left: 57px';>
        </select><BR>
<script>
  function validateReason() {
    var x = document.forms["newForm"]["reasonDropdown"].value;
    if (x == "") {
      alert("A Reason must be provided");
      return false;
    }
  }
</script>

    <label for="problem"><b>Problem Description:</b></label><br>
    <textarea id="problemdesc" name="problemdescript" style='background-color: #ffff99'; cols="100" rows="4" style="resize:none" placeholder="Please provide as much information as possible..."></textarea><br>

<script>
  function validateDescription() {
    var x = document.forms["newForm"]["problemdescript"].value;
    if (x == "") {
      alert("A description of the problem must be provided");
      return false;
    }
  }
</script>

    <div>
        <label><strong>Add Attachment</strong>(optional)</label><br /> <input type="file"
            name="attachmentFile" id="attachmentFile"
            class="InputBox">
    </div><br>

    <label for="Status1"><b>Status:</b></label>
        <select id="statusDropdown1" name="statusDropdown" style='background-color: #ffff99';>
        </select>
<script>
  function validateStatus() {
    var x = document.forms["newForm"]["statusDropdown"].value;
    if (x == "") {
      alert("A status must be provided");
      return false;
    }
  }
</script>

    <label for="requiredaction"><b>Action Required?</b></label>
        <select id="requiredaction" name="requireaction" style='background-color: #ffff99';>
          <option value="Yes">Yes</option>
          <option value="No">No</option>
        </select>

    <label for="SubmittedBy"><b>Submitted By:</b></label>
    <input type="text" id="SubmittedBy" name="SubmittedBy"  style='background-color: #ffff99'; value = "<?php echo $_SESSION['smtRequestForAction']['user_name'];?>" readonly>

    <label for="DateSubmitted"><b>Date Submitted:</b></label>
    <input input type="date" id="DateSubmitted" name="DateSubmitted" style='background-color: #ffff99'; value="<?php echo date('Y-m-d');?>" readonly><br>

    <input type="submit" value="Submit" name="submit" onclick="validateCategory();validateReason();validateDescription();validateStatus();sendEmail()";>
  • 写回答

1条回答 默认 最新

  • doutangdan3588 2019-06-05 09:59
    关注

    Ok, so I managed to solve it via using a Javascript function, my sendEmail.php is modified to the following...

    <?php
           $to  = 'email1@domain.com';
           $subject = 'test subject';
           $message = 'test message';
           $headers  = 'Cc: email2@domain.com';
           mail($to,$subject,$message,$headers);
    ?>
    

    And within index.php i've used the following function...

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
    function sendMail() {
      $.get("sendEmail.php");
      return false;
    }
    </script>
    

    I added the sendMail() function to the onclick option and now the email doesn't send when I refresh, only when the user fills the form in and submit's it. Hope this helps someone.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染