我有一个表单作为自定义插件的一部分,但它在提交时不保留数据。它保存在数据库中,如果我刷新页面它会显示信息,但我希望它第一次保留在表单中。
我知道它一定是非常愚蠢的东西,我似乎没有找到它......提前谢谢!
这是代码:
<?php
global $guarantor_details;
add_shortcode('guarantorForm', 'guarantor_form');
function guarantor_form()
{
$output = "";
global $current_user;
$current_user = wp_get_current_user();
$guarantor_details = getGuarantorData();
$message = (isset($_POST["guarantor_save"])) ? saveGuarantor() : false;
if ($message) $output .= '<div class="success">' . $message . '</div>';
$message = (isset($_GET["resend"]) == "true") ? generateGuarantorEmail($guarantor_details) : false;
if ($message) $output .= '<div class="success">' . $message . '</div>';
if($current_user->ID){
$output .= '
<h1>Guarantor Details</h1>
<form action="" method="POST" class="profileForm">
<div class="formField">
<label for="guarantor_title">Title</label>
<select name="guarantor_title">
<option value="Mr">Mr</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
</select>
</div>
<div class="formField">
<label for="guarantor_name">Guarantor Full Name</label>
<input name="guarantor_name" type="text" required
value="' . $guarantor_details->guarantor_name . '"/>
</div>
<div class="formField">
<label for="guarantor_relationship">Relationship to student</label>
<input name="guarantor_relationship" type="text" required
value="' . $guarantor_details->guarantor_relationship . '"/>
</div>
<div class="formField">
<label for="guarantor_address1">Address 1</label>
<input name="guarantor_address1" type="text" required
value="' . $guarantor_details->guarantor_address1 . '"/>
</div>
<div class="formField">
<label for="guarantor_address2">Address 2</label>
<input name="guarantor_address2" type="text" value="' . $guarantor_details->guarantor_address2 . '"/>
</div>
<div class="formField">
<label for="guarantor_city">City</label>
<input name="guarantor_city" type="text" required
value="' . $guarantor_details->guarantor_city . '"/>
</div>
<div class="formField">
<label for="guarantor_county">County</label>
<input name="guarantor_county" type="text" required
value="' . $guarantor_details->guarantor_county . '"/>
</div>
<div class="formField">
<label for="guarantor_postcode">Postcode</label>
<input name="guarantor_postcode" type="text" required
value="' . $guarantor_details->guarantor_postcode . '"/>
</div>
<div class="formField">
<label for="guarantor_country">Country</label>
<select name="guarantor_country">
' . countryList() . '
</div>
<div class="formField">
<label for="guarantor_mobile">Mobile</label>
<input name="guarantor_mobile" type="tel" required
value="' . $guarantor_details->guarantor_mobile . '"/>
</div>
<div class="formField">
<label for="guarantor_confirm_mobile">Confirm Mobile</label>
<input name="guarantor_confirm_mobile" id="confirm_mobile" type="tel" required value="' . $guarantor_details->guarantor_mobile . '"/>
</div>
<div class="formField">
<label for="guarantor_telephone">Telephone</label>
<input name="guarantor_telephone" type="tel" required
value="' . $guarantor_details->guarantor_telephone . '"/>
</div>
<div class="formField">
<label for="guarantor_email">Email</label>
<input name="guarantor_email" type="email" required
value="' . $guarantor_details->guarantor_email . '"/>
</div>
<div class="formField">
<input name="guarantor_save" type="submit" value="Save"/>
</div>
<div class="formField alignRight">
Lost the email with the link? <a href="?resend=true">Resend Email</a>
</div>
</form>
';}
else{
$output = 'You must <a href="'.site_url().'/login">login</a> to fill in guarantor details.';
}
return $output;
}
function getGuarantorData()
{
global $current_user;
global $wpdb;
$table = $wpdb->prefix . 'vebra_tenants';
$guarantor_details = $wpdb->get_row(
"
SELECT *
FROM $table
WHERE tenant_user_id = $current_user->ID
"
);
return isset($guarantor_details) ? $guarantor_details : false;
return $guarantor_details;
}
//save guarantor details
function saveGuarantor()
{
global $wpdb;
global $current_user;
$guarantor_details = getGuarantorData();
$details = $_POST;
unset($details["guarantor_save"]); //no submit
unset($details["guarantor_confirm_mobile"]); //no submit
if ($guarantor_details) {
$wpdb->update(
$wpdb->prefix . 'vebra_tenants',
$details,
array('tenant_user_id' => $current_user->ID));
} else {
$details['tenant_user_id'] = $current_user->ID;
$wpdb->insert($wpdb->prefix . 'vebra_tenants', $details);
$guarantor_details = getGuarantorData();
}
//let db know details updated
update_user_meta($current_user->ID, 'vebra_guarantor_details', true );
//send email if not already sent
if ($guarantor_details->guarantor_hash == "") {
generateGuarantorEmail($guarantor_details);
return "The guarantor details have been saved and an email has been sent to your guarantor to accept the agreement. They have seven days to respond.";
}
//set status percentage of that property
$pcode = get_user_meta($current_user->ID, 'vebra_pcode', true);
saveStatusProgress($pcode);
return "The guarantor details have been saved";
}
function generateGuarantorEmail($guarantor_details)
{
global $current_user;
global $wpdb;
$code = md5($guarantor_details->guarantor_name);
$wpdb->update($wpdb->prefix . 'vebra_tenants',
array('guarantor_hash' => $code,
'guarantor_sent' => date('Y-m-d h:m:s')),
array('tenant_user_id' => $current_user->ID,
));
$message = "Hello " . $guarantor_details->guarantor_name . "\n\n";
$message .= "Your name has been listed as a guarantor for a student let for " . $guarantor_details->tenant_firstname . " " . $guarantor_details->tenant_surname;
$message .= "\n\nIf you have agreed to being the guarantor, please click on the link below to go to a secure area of our website. There you will find a copy of the Assured Tenancy Agreement for the property, and the guarantor form. Please print off the guarantor form, sign and have it witnessed and then return it to us as soon as you can.";
$message .= "\n\n" . get_permalink(get_option('vebra_confirmPermalink')) . "?confirm=" . $guarantor_details->guarantor_hash;
$message .= "\n\n If you have not agreed to being a guarantor, would you please email us to let us know.";
$message .= "\n\n Thank you for your help.\n\n \n\n";
sendEmailToPublic($guarantor_details->guarantor_email, ' ', 'Guarantor Proposal', $message);
return "An email has been sent to the guarantor with a link for them to activate and accept to be a guarantor";
}
答案 0 :(得分:0)
您可能无法在$guarantor_details
对象中获取数据库存储值。
所以,请检查您是否收到了$guarantor_details->guarantor_name
中的内容
如果没有,那么试着解决它,
要么
如果您只是想显示提交的数据,那么您只需打印guarantor_mobile
<input name="guarantor_mobile" type="tel" required value="'. $_POST["guarantor_mobile"].'"/>
答案 1 :(得分:0)
感谢您的回答,但我以不同的方式解决了这个问题,必须在构建表单之前加载$guarantor_details = getGuarantorData();
以确保数据正确显示。