Form not populating the first time – wordpress

I have a form as part of a custom plugin, but it is not retaining the data when submitted. It saves in the database, and if I refresh the page it shows the information, but I want it to stay in the form first time.

I know it must be something really stupid, I just don’t seem to find it… Thanks in advance!

Read More

This is the code:

<?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 . "nn";
$message .= "Your name has been listed as a guarantor for a student let for " . $guarantor_details->tenant_firstname . " " . $guarantor_details->tenant_surname;
$message .= "nnIf 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 .= "nn" . get_permalink(get_option('vebra_confirmPermalink')) . "?confirm=" . $guarantor_details->guarantor_hash;
$message .= "nn If you have not agreed to being a guarantor, would you please email us  to let us know.";
$message .= "nn Thank you for your help.nn nn";

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";
}

Related posts

2 comments

  1. You might not getting database stored values inside $guarantor_details object.

    so please check are you getting something in $guarantor_details->guarantor_name
    if not then try to resolve it,
    OR
    If you just want to display submitted data then, you can simply print the post data in your case for guarantor_mobile its

    <input name="guarantor_mobile" type="tel" required value="'. $_POST["guarantor_mobile"].'"/>
    
  2. Thanks for the answers, but I solved this in a different way, had to load $guarantor_details = getGuarantorData(); just before building the form to make sure the data was displayed correctly.

Comments are closed.