我在WordPress的页面上遇到了一个表单问题。我已经在functions.php文件中定义了我的表单,然后在页面上调用表单,但是它提交了两次数据。我怎么解决这个问题?
代码如下:
这是我在实际页面上使用的do_action函数
<?php do_action('map_location_form'); ?>
...这是我在function.php文件中定义的表单:
add_action('map_location_form', 'map_location_report_form');
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
wp_redirect(WP_HOME.'/news.php');
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};
如何解决问题,以便表单只将一行数据汇总到我的数据库?
我已经看到另一个解决方案,我没有得到双重提交到我的数据库,但然后表单显示在我的标题区域,它不应该。 代码如下:
/*add_action('init', 'map_location_report_form');*/
add_action('map_location_form', 'map_location_report_form');
function map_location_report_form()
{
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL )
{
echo '<form method="post" action="' . $this_page .'">
<div class="formfield-report" id="formfield-report-firstname">
<label for="first_name" id="first_name">Navn: </label>
<input type="text" name="first_name" id="first_name" />
</div>
<div class="formfield-report" id="formfield-report-lastname">
<label for="last_name" id="last_name">Efternavn: </label>
<input type="text" name="last_name" id="last_name" />
</div>
<div class="formfield-report" id="formfield-report-locationtype">
<label for="location_type" id="location_type">Rapport type: </label>
<select name="location_type" />
<option value="sigtmelding" selected>Sigtmelding</option>
<option value="fangstrapport">Fangstrapport</option>
<option value="jagtomraade">Jagtområde</option>
</select>
</div>
<div class="formfield-report" id="formfield-report-latitude">
<label for="location_latitude" id="location_latitude">Breddegrad: </label>
<input type="text" name="location_latitude" id="location_latitude" />
</div>
<div class="formfield-report" id="formfield-report-longitude">
<label for="location_longitude" id="location_longitude">Længdegrad: </label>
<input type="text" name="location_longitude" id="location_longitude" />
</div>
<input type="hidden" value="0" name="page" />
<div id="formfield-report-button">
<input class="btn btn-default submit-form-button" type="Submit" />
</div>
</form>';
wp_redirect(WP_HOME.'/news.php');
} //End Page 1 of Form
// Start Page 2 of Form
elseif ( $page == 0 )
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$location_type = $_POST['location_type'];
$location_latitude = $_POST['location_latitude'];
$location_longitude = $_POST['location_longitude'];
$page = $_POST['page'];
$page_one_table = 'maplocationreports';
$page_one_inputs = array
(
'first_name' => $first_name,
'last_name' => $last_name,
'location_type' => $location_type,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'page' => $page
);
$insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs);
echo '<h3>Mange tak for dit bidrag!</h3>';
echo '<p>Der er sat stor pris på at du har taget dig tid til at registrere et punkt på kortet!</p>';
} // End Page 2 of Form
};
有人知道如何解决这个问题吗?
请具体详细说明:)