duanjiu1003 2019-04-11 16:06
浏览 54
已采纳

我在哪里添加我的PHP表单处理程序文件在WordPress?

I am working with an 2 HTML forms. The first being a "Contact Form" which holds 3 fields: Contact Name, Business Name, and Email. When the submit button is clicked on the "Contact Form" an email is sent to me with the input and the "Quote Form" is loaded which includes the 3 fields from the "Contact Form" plus some additional fields for the user to fill out. When the submit button is clicked on the "Quote Form", an second email is sent to me all of the input and a thank you email is sent to the user. This gives me 3 files to work with: contactform.html, contactformhandler.php, and quoteformhandler.php.

Here's the problem:

I added the “contactform.html” to a text widget on a new page in WordPress that held the web page template. This worked, so I was able to see the Contact Form on the WordPress site, but when I hit submit I was taken to an error page.

I need to figure out where to add my “contactformhandler.php” and “quoteformhandler.php” files, so when I hit submit the input is validated.

I tried creating another page on the WordPress site and adding the php code to a text widget like I did for the .html file, but this did not work. When the submit button was clicked, both “form1” and “form2” were displayed on the page simultaneously.

I then tried handling things on the FTP side. I dropped my .php form handlers into cPanel. This didn’t solve the problem either. The form and form handler were communicating with each other, but instead of loading the Quote Form inside a WordPress template, the .php file was loaded by itself.

<?php
// define variables and set to empty values
$errorCount = "";

$bizNameErr = $bizTypeErr = $address1Err = $address2Err = $cityErr = $stateErr
= $zipErr = $sqftErr = $cNameErr = $emailErr = "";

$bizName = $bizType = $address1 = $address2 = $city = $state
= $zip = $sqft = $cName = $email = "";

// Checks to make sure the required fields from the Contact Form are not empty
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["bizName"])) {
    $bizNameErr = "Business name is required";
    ++$errorCount;
  } else {
    $bizName = test_input($_POST["bizName"]);
  } 
  if (empty($_POST["cName"])) {
    $cNameErr = "Contact Name is required";
    ++$errorCount;
  } else {
    $cName = test_input($_POST["cName"]);
  }
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
    ++$errorCount;
  } else {
    $email = test_input($_POST["email"]);
  }
}

// Form validation
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
// This function, when called, redisplays the Contact Form
function redisplayForm($bizName, $cName, $email, $bizNameErr, $cNameErr, $emailErr ) { ?>
    <form name="Contact Form Handler" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <div class="form-embed">
        <span class="error"> <?php
// Determining that the user input was sent successfully and setting a resultMsg variable accordingly
            if (isset($result)) {
                $resultMsg = "Your infomation was sent successfully.";
            } else {
                $resultMsg = "There was a problem sending your information.";
            }
            echo $resultMsg; ?>
        </span><br><br>
        <span class="error">Please re-enter the information below. <br><br> * required field</span>
        <div class="bodyText"><p style="text-align: center;">
            <span style="font-family: 'times new roman', times; font-size: 18px; color: #00ccff;">
            <strong><span style="font-size: 24px;"><em>Looking For A Quote</em>
            </span>?&nbsp;</strong></span>
        </div>
        <br><br>    
        <label for="bizName" class="control-label">Business Name:
        <span class="error">* <?php echo $bizNameErr;?> </span></label>
        <input required maxlength="200" type="text" class="form-control" value="<?php echo $bizName; ?>" placeholder="Enter Business Name" name="bizName" id="bizName"  />
        <label for="cName" class="control-label">Contact Name:
        <span class="error">* <?php echo $cNameErr;?></span></label>
        <input required maxlength="200" type="text" class="form-control" value="<?php echo $cName; ?>" placeholder="Enter Contact Name" name="cName" id="cName" />
        <label for="email" class="control-label">Email Address: 
        <span class="error">* <?php echo $emailErr;?></span></label>
        <input required maxlength="200" type="email" class="form-control" value="<?php echo $email; ?>" placeholder="Enter Email Address" name="email" id="email"  />
        <br>
        <div class="buttons">
            <input type="submit" name="Submit" value="Send Form"  /> 
        </div>
    </div>
    </form>
// This function, when called, displays the Quote Form
function redisplayForm2($bizName, $bizType, $address1, $address2, $city, 
$state, $zip, $sqft, $cName, $email, $bizNameErr, $bizTypeErr,
$address1Err, $cityErr, $stateErr, $zipErr, $sqftErr, $cNameErr, $emailErr ) { ?>
    <form name="Quote Form" method="post" action="quoteformhandler.php">
    <div class="form-embed">
        <div class="bodyText"><p style="text-align: center;">
            <span style="font-family: 'times new roman', times; font-size: 18px; color: #00ccff;">
            <strong><span style="font-size: 24px;"><em>Looking For A Quote</em>
            </span>?&nbsp;</strong></span>
        </div><br><br>
        <label for="bizName" class="control-label">Business Name:</label>
        <input required maxlength="200" type="text" class="form-control" value="<?php echo $bizName ?>" name="bizName" id="bizName" />
        <label for="cName" class="control-label">Contact Name:</label>
        <input required maxlength="200" type="text" class="form-control" value="<?php echo $cName ?>" name="cName" id="cName"  />
        <label for="email" class="control-label">Email Address:</label>
        <input required maxlength="200" type="email" class="form-control" value="<?php echo $email ?>" name="email" id="email"  />
        <label for="bizType" class="control-label">Business Type:</label>   
        <select required name="bizType">
            <option value=""disabled selected hidden>Choose business type...</option>
            <option value="Restaurant">Restaurant</option>
            <option value="Bar or Pub">Bar or Pub</option>
            <option value="Brewery">Brewery</option>
            <option value="Wedding Venue">Wedding Venue</option>
            <option value="Salon or Spa">Salon or Spa</option>
            <option value="Event or Party Space">Event or Party Space</option>
            <option value="Funeral Home">Funeral Home</option>
            <option value="Retail">Retail</option>
            <option value="Museum or Gallery">Museum or Gallery</option>
            <option value="Industrial Facility">Industrial Facility</option>
            <option value="Medical Facility">Medical Facility</option>
            <option value="Commercial Property">Commercial Property</option>
            <option value="Fitness Facility / Gym">Fitness Facility / Gym</option>
            <option value="Sports or Recreational Facility ">Sports or Recreational Facility </option>
            <option value="Other">Other</option>
        </select>
        <label for="address1" class="control-label">Address Line 1:</label>
        <input required type="text" name="address1" id="address1"
        size="40" placeholder="Street and number, P.O. box, c/o">
        <label for="address2" class="control-label">Address Line 2:
        <input type="text" name="address2" id="address2"
        size="40" placeholder = "Apartment, suite, unit, building, floor, ect."> 
        <label for="city" class="control-label">City:</label>
        <input required type="text" class="form-control" placeholder="Enter City" name="city" id="city" />
        <label for="state" class="control-label">State:</label> 
        <select required name="state">
            <option value="" disabled selected hidden>Choose state...</option>
            <option value="AL">Alabama (AL)</option>
            <option value="AK">Alaska (AK)</option>
            <option value="AZ">Arizona (AZ)</option>
            <option value="AR">Arkansas (AR)</option>
            <option value="CA">California (CA)</option>
            <option value="CO">Colorado (CO)</option>
            <option value="CT">Connecticut (CT)</option>
            <option value="DE">Delaware (DE)</option>
            <option value="DC">District Of Columbia (DC)</option>
            <option value="FL">Florida (FL)</option>
            <option value="GA">Georgia (GA)</option>
            <option value="HI">Hawaii (HI)</option>
            <option value="ID">Idaho (ID)</option>
            <option value="IL">Illinois (IL)</option>
            <option value="IN">Indiana (IN)</option>
            <option value="IA">Iowa (IA)</option>
            <option value="KS">Kansas (KS)</option>
            <option value="KY">Kentucky (KY)</option>
            <option value="LA">Louisiana (LA)</option>
            <option value="ME">Maine (ME)</option>
            <option value="MD">Maryland (MD)</option>
            <option value="MA">Massachusetts (MA)</option>
            <option value="MI">Michigan (MI)</option>
            <option value="MN">Minnesota (MN)</option>
            <option value="MS">Mississippi (MS)</option>
            <option value="MO">Missouri (MO)</option>
            <option value="MT">Montana (MT)</option>
            <option value="NE">Nebraska (NE)</option>
            <option value="NV">Nevada (NV)</option>
            <option value="NH">New Hampshire (NH)</option>
            <option value="NJ">New Jersey (NJ)</option>
            <option value="NM">New Mexico (NM)</option>
            <option value="NY">New York (NY)</option>
            <option value="NC">North Carolina (NC)</option>
            <option value="ND">North Dakota (ND)</option>
            <option value="OH">Ohio (OH)</option>
            <option value="OK">Oklahoma (OK)</option>
            <option value="OR">Oregon (OR)</option>
            <option value="PA">Pennsylvania (PA)</option>
            <option value="RI">Rhode Island (RI)</option>
            <option value="SC">South Carolina (SC)</option>
            <option value="SD">South Dakota (SD)</option>
            <option value="TN">Tennessee (TN)</option>
            <option value="TX">Texas (TX)</option>
            <option value="UT">Utah (UT)</option>
            <option value="VT">Vermont (VT)</option>
            <option value="VA">Virginia (VA)</option>
            <option value="WA">Washington (WA)</option>
            <option value="WV">West Virginia (WV)</option>
            <option value="WI">Wisconsin (WI)</option>
            <option value="WY">Wyoming (WY)</option>
        </select>
        <label for="zip" class="control-label">Zip Code:</label>
        <input required type="text" class="form-control" 
        pattern="[0-9]{5}" maxlength="5" size="5" 
        placeholder="Enter Zip Code" name="zip" id="zip" />
        <label for="sqft" class="control-label">Estimated Square Feet:</label>
        <select required name="sqft" >
            <option value="" disabled selected hidden>Choose square footage...</option>
            <option value="4,000 or less">4,000 sqft or less</option>
            <option value="5,000 to 9,000">5,000 sqft to 9,000 sqft</option>
            <option value="10,000 to 14,000">10,000 sqft to 14,000 sqft</option>
            <option value="15,000 to 20,000">15,000 sqft to 20,000 sqft</option>
            <option value="20,000 or more">20,000 sqft or more</option>
        </select>
        <br>
        <div class="buttons">
            <input type="submit" name="Submit" value="Send Form"  /> 
        </div>
    </div>
    </form>
`````````````````````````````````````````````
`````````````````````````````````````````````
<?php
}
// Error checking. 
// If there are errors, call the redisplayForm function to redisplay the Contact Form.
if ($errorCount>0 || $errorCount<0) {
    redisplayForm($bizName, $cName, $email, $bizNameErr, $cNameErr, $emailErr);

    $bizNameErr = $cNameErr = $emailErr = "";

    $bizName = $cName = $email = "";
// If there are no errors, an email will be sent to Conversion Worx with the user's input
// and the redisplayForm2 function will be called to display the Quote Form.
} else {    
    $To = "myemail";
    $Subject = "Contact Form Results";
    $Message = "Business Name: " . $bizName . "
" 
    . "Contact Name: " . $cName . "
"
    . "Email Address: " . $email;
    $result = mail($To, $Subject, $Message);

    redisplayForm2($bizName, $bizType, $address1, $address2, $city, 
    $state, $zip, $sqft, $cName, $email, $bizNameErr, $bizTypeErr,
    $address1Err, $cityErr, $stateErr, $zipErr, $sqftErr, $cNameErr, $emailErr );

    $bizNameErr = $bizTypeErr = $address1Err = $address2Err = $cityErr = $stateErr
    = $zipErr = $sqftErr = $cNameErr = $emailErr = "";

    $bizType = $address1 = $address2 = $city = $state
    = $zip = $sqft = "";
} 
?>
  • 写回答

1条回答 默认 最新

  • dougang2749 2019-04-11 17:46
    关注

    There are many ways to do this with Wordpress. You could do it with a plugin. Contact Form 7 is excellent. You could use the plugin to create the form and then use the plugin's hook to process the data.

    Alternatively you could set the form to post to admin-ajax.php and then handle the processing with the wp_ajax_my_action hook.

    The easiest way based on your code would probably be to continue down this line:

    I then tried handling things on the FTP side. I dropped my .php form handlers into cPanel. This didn’t solve the problem either. The form and form handler were communicating with each other, but instead of loading the Quote Form inside a WordPress template, the .php file was loaded by itself.

    Specifically, upload the contactformhandler.php file, have your form post to this file, and then set a redirect at the end of the processing to the second page that you want to load.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!