我在这里面临一个问题。
以下代码在我的网站上运行,当我填写表单并检查我的数据库时,数据已经注册了两次。
请从我的functions.php文件中获取以下php代码。
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>';
} //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_shortcode('map_location_report','map_location_report_form');`
如何阻止此代码两次注册数据?
提前致谢!
答案 0 :(得分:1)
将add_shortcode()
方法包含在init
操作
add_action('init', 'my_function');
function my_function(){
add_shortcode('map_location_report','map_location_report_form');`
}
答案 1 :(得分:0)
检查下面的$ _POST
if (!empty($_POST)) {
//Insert the values to table
}
if (empty($_POST)) {
//display the form
}
答案 2 :(得分:0)
由于某种原因,表单在标题中运行时会注册一次数据,如下所示:
add_action('init', '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>';
} //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
};´
但是使用页面模板上的do_action函数在页面上运行时两次注册数据:
<?php do_action('map_location_form'); ?>
...和functions.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>';
} //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
};
是否有解决方案在页面上显示表单而没有两次注册数据?