Jump to content

Recommended Posts

I am taking a form that has been created and now trying to incorporate it into WP. The form is filled out multiple times per session. The form when filled out will generate an ID that is used to track the form submissions until completed. The problem that I'm running into is that the code is using a URL structure that breaks on the install.


I'm not sure what to tell the code to look for as the URL.


I hope I'm making sense. This is my first dive into WP dev. Thanks for the help.



<?php
/*
Template Name: Form
*/
?>
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* @package WordPress
* @subpackage Twenty_Thirteen
* @since Twenty Thirteen 1.0
*/

get_header(); ?>

<div id="main-content" class="main-content">

<div id="headertitle">
<h1>Steam Trap Form Input</h1>
</div>
<div id="formtable">
<form class="form-horizontal" role="form" action="/test/FormHandler.php" method="post">
<fieldset>
<div class="form-group">
<label for="dateSurvey" class="col-sm-2 control-label">Survey Date</label>
<div class="col-sm-6">
<input type="text"<? echo (isset($sd['date']) ? ' value="'.format_date($sd['date']).'"' : '') ?> class="form-control col-sm-6 input-append date" id="dp1" name="dateSurvey" data-date="02-12-2014" data-date-format="mm-dd-yyyy">
</div>
</div>

<div class="form-group">
<label for="plantName" class="col-sm-2 control-label">Plant Name</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="plantName"<? echo (isset($sd['plant_name']) ? ' value="'.$sd['plant_name'].'"' : '') ?>>
</div>
</div>

<div class="form-group">
<label for="plantLoc" class="col-sm-2 control-label">Plant Location</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="plantLoc"<? echo (isset($sd['plant_location']) ? ' value="'.$sd['plant_location'].'"' : '') ?>>
</div>
</div>

<div class="form-group">
<label for="plantContact" class="col-sm-2 control-label">Plant Contact Name</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="plantContact"<? echo (isset($sd['plant_contact_name']) ? ' value="'.$sd['plant_contact_name'].'"' : '') ?>>
</div>
</div>
<div class="form-group">
<label for="pph" class="col-sm-2 control-label">Pound Per Hour</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="pph" placeholder="XX.XX"<? echo (isset($sd['pph']) ? ' value="'.$sd['pph'].'"' : '') ?>>
</div>
</div>
<div class="form-group">
<label for="area" class="col-sm-2 control-label">Area</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="area" placeholder="Boiler Room"<? echo (isset($sd['area']) ? ' value="'.$sd['area'].'"' : '') ?>>
</div>
</div>
<hr>

<div class="form-group">
<label for="testDate" class="col-sm-2 control-label">Tested Date</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6 input-append date" id="dp2" name="testDate" data-date="02-12-2014" data-date-format="mm-dd-yyyy">
</div>
</div>

<!-- <div class="form-group">
<label for="direction" class="col-sm-2 control-label">Direction</label>
<div class="col-sm-6">
<select class="form-control col-sm-6" name="direction">
<option value="N">N</option>
<option value="NE">NE</option>
<option value="NW">NW</option>
<option value="S">S</option>
<option value="SE">SE</option>
<option value="SW">SW</option>
<option value="E">E</option>
<option value="W">W</option>
</select>
</div>
</div> -->

<div class="form-group">
<label for="location" class="col-sm-2 control-label">Location</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="location" placeholder=" 'Detailed as possible'">
</div>
</div>

<div class="form-group">
<label for="flrLevel" class="col-sm-2 control-label">Floor Level</label>
<div class="col-sm-6">
<select class="form-control col-sm-6" name="flrLevel">
<option value="GROUND">Ground</option>
<option value="1st">1st</option>
<option value="2nd">2nd</option>
<option value="3rd">3rd</option>
<option value="4th">4th</option>
<option value="5th">5th</option>
<option value="6th">6th</option>
<option value="7th">7th</option>
<option value="8th">8th</option>
<option value="9th">9th</option>
<option value="10th">10th</option>
<option value="11th">11th</option>
<option value="12th">12th</option>
<option value="13th">13th</option>
<option value="14th">14th</option>
<option value="15th">15th</option>
</select>
</div>
</div>

<div class="form-group">
<label for="elevation" class="col-sm-2 control-label">Elevation</label>
<div class="col-sm-6">
<input type="number" class="form-control col-sm-6" name="elevation" placeholder="in feet">
</div>
</div>

<div class="form-group">
<label for="tagNum" class="col-sm-2 control-label">Tag Number</label>
<div class="col-sm-6">
<input type="text" class="form-control col-sm-6" name="tagNum">
</div>
</div>

<div class="form-group">
<label for="manufacturer" class="col-sm-2 control-label">Manufacturer</label>
<? /* <input type="text" name="mmn" size="30"> */ ?>
<div class="col-sm-6">
<select name="manufacturer" class="depend col-sm-6 form-control" id="manufacturer">
<option value="" selected="selected" disabled="disabled">Select Manufacturer</option>
</select>
</div>
<div id="status"></div>
</div>

<div class="form-group">
<label for="model" class="col-sm-2 control-label">Model Number</label>
<div class="col-sm-6">
<select name="model" class="depend col-sm-6 form-control" id="model">
<option value="" selected="selected">Select value</option>
</select>
</div>
</div>

<div class="form-group">
<label for="size" class="col-sm-2 control-label">Size</label>
<div class="col-sm-6">
<select name="size" class="depend col-sm-6 form-control" id="size">
<option value="" selected="selected">Select value</option>
</select>
</div>
</div>

<div class="form-group">
<label for="pressure" class="col-sm-2 control-label">Pressure</label>
<div class="col-sm-6">
<select name="pressure" id="pressure" class="col-sm-6 form-control">
<option value="" selected="selected">Select value</option>
</select>
</div>
</div>

<div class="form-group">
<label for="service" class="col-sm-2 control-label">Service</label>
<div class="col-sm-6">
<select class="form-control col-sm-6" name="service" name="service">
<option value="TRACER">TRACER</option>
<option value="HEATER">HEATER</option>
<option value="PROCESS">PROCESS</option>
<option value="DRIP">DRIP</option>
</select>
</div>
</div>

<div class="form-group">
<label for="trapCond" class="col-sm-2 control-label">Trap Conditions</label>
<div class="col-sm-6">
<select class="form-control col-sm-6" name="trapCond">
<option value="OK">OK</option>
<option value="BT">BT</option>
<option value="CP">CP</option>
<option value="VO">VO</option>
<option value="RCL">RCL</option>
</select>
</div>
</div>

<p></p>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<? if(isset($surveyID)) { echo '<input type="hidden" name="surveyID" value="'.intval($_GET['survey']).'">'; } ?>
<!-- <? if(isset($areaID)) { echo '<input type="hidden" name="areaID" value="'.intval($_GET['area']).'">'; } ?>
-->
<button class="btn btn-default btn-lg" type="submit" name="submit" value="add">Add Trap</button>

<button class="btn btn-primary btn-lg" type="submit" name="submit" value="add_area">Submit Area</button>

<button class="btn btn-danger btn-lg" type="submit" name="submit" value="submit">Submit Survey</button>
</div>
</div>
</fieldset>
</form>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- <script src="../script/js/script.js"></script> -->
<script src="../script/js/live.js"></script>
<script src="../script/js/bootstrap-datepicker.js"></script>
<script type="text/javascript">

jQuery(document).ready(function() {
jQuery('#dp1').datepicker({
dateFormat : 'dd-mm-yy'
});
});

jQuery(document).ready(function() {
jQuery('#dp2').datepicker({
dateFormat : 'dd-mm-yy'
});
});

</script>
</body>
</html>
<!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_sidebar();
get_footer();
?>


<?php require("../test/base.php");

if(isset($_POST['submit']))
{
$url = '../test/form'; // path to form.php

if(!isset($_POST['manufacturer'])) { $_POST['manufacturer'] = NULL; }
if(!isset($_POST['model_number'])) { $_POST['model_number'] = NULL; }
if(!isset($_POST['size'])) { $_POST['size'] = NULL; }
if(!isset($_POST['pressure'])) { $_POST['pressure'] = NULL; }
$data = prep($_POST);

if($data['submit'] == 'add')
{
if(!isset($data['surveyID'])) {
$surveyID = save_survey($data);
}
else { $surveyID = $data['surveyID']; }

save_survey_details($surveyID, $data);
if($surveyID) { header('Location: '.$url.'?survey='.$surveyID); }

}

if($data['submit'] == 'add_area')
{
if(!isset($data['areaID'])) {
$areaID = save_survey($data);
}
else { $areaID = $data['areaID']; }

save_survey_details($areaID, $data);
if($areaID) { header('Location: '.$url.'?survey='.$areaID); }

}
elseif($data['submit'] == 'submit')
{
if(isset($data['surveyID'])) {
save_survey_details($data['surveyID'], $data);
}
else {
$surveyID = save_survey($data);
save_survey_details($surveyID, $data);
}
header('Location: '.$url);
}
}

function prep($input) {
$data = array();
if(is_array($input)) {
foreach ($input as $key => $val) {
$data[$key] = mysql_real_escape_string($val);
}
}
return $data;
}

function format_date($date = '') {
if(isset($date) && $date) {
$tmp = explode('-', $date);
$d = $tmp[2].'-'.$tmp[0].'-'.$tmp[1];
}
else { $d = NULL; }
return $d;
}

function save_survey($data) {
$result = mysql_query("INSERT INTO surveys (date, plant_name, plant_location, plant_contact_name, pph, area)
VALUES('".format_date($data['dateSurvey'])."','".$data['plantName']."','".$data['plantLoc']."','".$data['plantContact']."','".$data['pph']."','".$data['area']."')") or die('Error: ' . mysql_error());
$result = mysql_query("INSERT INTO area (date, plant_name, plant_location, plant_contact_name, pph, area)
VALUES('".format_date($data['dateSurvey'])."','".$data['plantName']."','".$data['plantLoc']."','".$data['plantContact']."','".$data['pph']."','".$data['area']."')") or die('Error: ' . mysql_error());
return mysql_insert_id();
}

function save_survey_details($surveyID, $data) {
$result = mysql_query("INSERT INTO survey_details (survey_id, tested_date, direction, location, floor_level, elevation, tag_number, manufacturer, model_number, size, pressure, service, trap_conditions)
VALUES($surveyID, '".format_date($data['testDate'])."','".$data['direction']."','".$data['location']."','".$data['flrLevel']."','".$data['elevation']."','".$data['tagNum']."','".$data['manufacturer']."','".$data['model']."','".$data['size']."','".$data['pressure']."','".$data['service']."','".$data['trapCond']."')");
$result = mysql_query("INSERT INTO area_details (survey_id, tested_date, direction, location, floor_level, elevation, tag_number, manufacturer, model_number, size, pressure, service, trap_conditions)
VALUES($surveyID, '".format_date($data['testDate'])."','".$data['direction']."','".$data['location']."','".$data['flrLevel']."','".$data['elevation']."','".$data['tagNum']."','".$data['manufacturer']."','".$data['model']."','".$data['size']."','".$data['pressure']."','".$data['service']."','".$data['trapCond']."')");
//or die('Error: ' . mysql_error());
}

?>

  • 3 weeks later...

With WordPress, everything runs through index.php. So you'll need to find the right hook (maybe 'after_theme_setup'?) to bind to an action in your functions.php file to process the script.

 

I've not set up a custom form inside WordPress to the degree you're attempting, but I don't think you'll need the action parameter - you're already technically on the index page and you'll be posting back to the index page, so why bother, right? I could be wrong, so someone please correct me if I am.

 

Of course, you could always just bind the action to the wp_ajax_* hooks and run everything through ajax - you'll know exactly where and when things are happening and you can redirect the user based on the outcome of the form processing.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.