带有javascript按钮的PHP表单-不起作用

PHP form with javascript button - not working

本文关键字:表单 不起作用 PHP javascript 按钮 带有      更新时间:2024-06-26

我在使用PHP表单时遇到了一些问题,该表单应该将用户输入的值通过电子邮件发送到特定的电子邮件地址。我的网站上有一个非常相似的表单,它使用相同的JS和PHP,运行良好。我已经复制了JS/PHP/HTML,但就是不能让它唱歌跳舞。我没有收到任何错误消息,所以我甚至不确定这个过程在哪里失败了。

我已经多次使用和滥用Stackoverflow,但我就是无法理解这一点。

形式:

    <h2 class="module-title">Application Form</h2>
      <div class="content"><p>*Denotes required fields.</p></div>
<form action="/wp-content/themes/default/php/form.apply.php" method="post" name="application" id="application">
          <div class="module-field-list">
            <ul>
              <li class="field-375">
                <div class="field">
                  <label for="apply-surname" class="field-label">Surname</label>
                  <input name="apply-surname" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-firstname" class="field-label">First Name</label>
                  <input name="apply-firstname" type="text" placeholder="">
                  </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-previousnames" class="field-label">Previous Names</label>
                  <input name="apply-previousnames" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-agesept1st" class="field-label">Age at 1st September</label>
                  <input name="apply-agesept1st" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-dob" class="field-label">Date of Birth</label>
                  <input name="apply-dob" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-address" class="field-label">Address</label>
                  <input name="apply-address" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-postcode" class="field-label">Postcode</label>
                  <input name="apply-postcode" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-hometel" class="field-label">Home Telephone</label>
                  <input name="apply-hometel" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-mobile" class="field-label">Mobile Telephone</label>
                  <input name="apply-mobile" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-email" class="field-label">Email Address</label>
                  <input name="apply-email" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-gender" class="field-label">Gender</label>
                  <input name="apply-gender" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-nationality" class="field-label">Nationality</label>
                  <input name="apply-nationality" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-outsideeea" class="field-label">Have you ever lived outside the EEA?</label>
                  <input name="apply-outsideeea" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-outsideeeadates" class="field-label">Dates lived outside the EEA</label>
                  <input name="apply-outsideeeadates" type="text" placeholder="">
                </div>
              </li>
              <li class="field-750">
                <div class="field">
                  <label for="apply-medical" class="field-label">XYZ is committed to supporting all of our students. If you have a disability, medical condition or learning difficulty (such as dyslexia, Asperger's, etc) please state</label>
                  <input name="apply-medical" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-sen" class="field-label">Do you have a SEN statement or S139a?</label>
                  <input name="apply-sen" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-ethnic" class="field-label">What is your Ethnic background?</label>
                  <input name="apply-ethnic" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-predictedgrades" class="field-label">What are your predicted grades?</label>
                  <input name="apply-predictedgrades" type="text" placeholder="">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-coursechoice" class="field-label">At this stage, are you fairly sure which course you wish to take at XYZ?</label>
                  <input name="apply-coursechoice" type="text" placeholder="">
                </div>
              </li>
              <li class="field-750">
                <div class="field">
                  <label for="apply-asa2" class="field-label">AS/A2 levels</label>
                  <input name="apply-asa2" type="text" placeholder="In order of preference, please list a minimum of four subjects you are considering taking">
                </div>
              </li>
              <li class="field-750">
                <div class="field">
                  <label for="apply-voc" class="field-label">Vocational</label>
                  <input name="apply-voc" type="text" placeholder="Please state the exact title as it appears in the prospectus, for example Travel and Tourism BTEC Level 1 Diploma">
                </div>
              </li>
              <li class="field-375">
                <div class="field">
                  <label for="apply-nocourse" class="field-label">If you have answered no, please list the courses below you may be considering</label>
                  <input name="apply-nocourse" type="text" placeholder="">
                </div>
              </li>
              <li class="field-750">
                <div class="field">
                  <label for="apply-career" class="field-label">Do you have any career ideas? If so, what are they?</label>
                  <input name="apply-career" type="text" placeholder="">
                </div>
              </li>
              <li class="field-750">
                <div class="field">
                  <label for="apply-other" class="field-label">Other relevant information that you feel may assist us with helping you make the right course decision</label>
                  <input name="apply-other" type="text" placeholder="i.e work experience, part-time employment, voluntary work">
                </div>
              </li>


            </ul>
          </div>

       <div class="loader"></div>
       <div class="module-messages">
          <ul id="contact-messages">
          </ul>
        </div>

<div class="module-button"><a id="apply-submit">Apply</a></div>
       </form>
      </div>

PHP:

<?php
// Receiving variables
@$surname = htmlspecialchars($_POST['apply-surname']);
@$firstname = htmlspecialchars($_POST['apply-firstname']);
@$previousnames = htmlspecialchars($_POST['apply-previousnames']);
@$agesept1st = htmlspecialchars($_POST['apply-agesept1st']);
@$dob = htmlspecialchars($_POST['apply-dob']);
@$homeaddress = htmlspecialchars($_POST['apply-address']);
@$postcode = htmlspecialchars($_POST['apply-postcode']);
@$hometel = htmlspecialchars($_POST['apply-hometel']);
@$mobile = htmlspecialchars($_POST['apply-mobile']);
@$email = htmlspecialchars($_POST['apply-email']);
@$gender = htmlspecialchars($_POST['apply-gender']);
@$uln = htmlspecialchars($_POST['apply-uln']);
@$nationality = htmlspecialchars($_POST['apply-nationality']);
@$oustideeea = htmlspecialchars($_POST['apply-outsideeea']);
@$outsideeeadates = htmlspecialchars($_POST['apply-outsideeeadates']);
@$secondaryschools = htmlspecialchars($_POST['apply-secondaryschools']);
@$medical = htmlspecialchars($_POST['apply-medical']);
@$sen = htmlspecialchars($_POST['apply-sen']);
@$ethnic = htmlspecialchars($_POST['apply-ethnic']);
@$predictedgrades = htmlspecialchars($_POST['apply-predictedgrades']);
@$coursechoice = htmlspecialchars($_POST['apply-coursechoice']);
@$asa2 = htmlspecialchars($_POST['apply-asa2']);
@$voc = htmlspecialchars($_POST['apply-voc']);
@$nocourse = htmlspecialchars($_POST['apply-nocourse']);
@$career = htmlspecialchars($_POST['apply-career']);
@$other = htmlspecialchars($_POST['apply-other']);

//Sending Email to form owner
# Email to Owner 
$pfw_header = "From: $email";
$pfw_subject = "Application Form";
$pfw_email_to = "XXX@example.com";
$pfw_message = "Surname: $surname'n"
. "First Name: $firstname'n"
. "Previous Name: $previousnames'n"
. "Age at 1st Sept: $age1stsept'n"
. "Date of Birth: $dob'n"
. "Home Address: $address'n"
. "Postcode: $postcode'n"
. "Home Telephone: $hometel'n"
. "Mobile no: $mobile'n"
. "Email address: $email'n"
. "Gender: $gender'n"
. "ULN: $uln'n"
. "Nationality: $nationality'n"
. "Lived outside EEA: $outsideeea'n"
. "Dates lived outside EEA: $outsideeeadates'n"
. "Secondary school: $secondaryschools'n"
. "Medical issues: $medical'n"
. "SEN: $SEN'n"
. "Ethnic: $ethnic'n"
. "Predicted Grades: $predictedgrades'n"
. "Course choice: $coursechoice'n"
. "AS/A2 choice: $asa2'n"
. "Vocational choice: $voc'n"
. "No course choice: $postcode'n"
. "Career: $career'n"
. "Other info: $other'n"
;
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;
echo('<li class="message-success">Your Application form has been received</li>');   
?>

JavaScript:

// Application form submit
$("#apply-submit").click(function () {
    $('form[name=application]').submit();
    $("#application").find('.loader').fadeIn();
});
$('form[name=application]').submit(function () {
    wcFormAction = $("#application").attr('action');
    $.post(wcFormAction, $('form[name=application]').serialize(), function (data) {
        $("#application").find('.loader').fadeOut();
    });
    return false;
});

编辑:我没有创建原始代码,我只是尝试将此代码作为表单模板。我以前从未考虑过提交表单,很抱歉,如果我的问题有点偏离主题,如果我能提供更多信息,请询问!

PHP代码即使有那些不必要的@也似乎可以。您正在使用的Javascript代码应该工作得很好,在jsfiddle上已经尝试过了。

你必须解决问题的原因,为此,写下你正在使用的每一个步骤和工具,并检查它们,即使是最基本的步骤和工具。喜欢

你确定吗,

  1. 您是否正确地包含了jQuery
  2. 您的ajax请求处理得当吗?(检查您是否收到任何回复)

等等…