I am trying to redirect the data using POST method to a file in Wordpress called functions.php. It is able to send the data detected from the textbox to that file and gets the return value by using wp_send_json($_POST). After getting the returned value, the value of the paragraph was able to change to its new value. But it only allows me to update it once unless the page is being refreshed or reloaded. What have i done wrong or have i missed? Below are my codes for this process.
functions.php
function test_load(){
if(isset($_POST) && $_POST['element'] == 'header')
{
$name = $_POST['name'];
$hidden = $_POST['hidden'];
if( $hidden == 'tagline')
{
update_option( 'wpbst_tagline', $name );
}
else if( $hidden == 'footer')
{
update_option( 'wpbst_footer', $name );
}
wp_send_json($_POST);
}
}
add_action('init', 'test_load');
script
<script>
$(document).ready(function(){
function custom_update_box( id ) {
$(id).click(function () {
if(id == '#tagline')
{
var dynamicDialog = $('<div>\ <label>\ Tagline :\ </label>\ <input type="hidden" value="tagline" id="hidden">\ <input id="update" class="form-control" type="textbox" value=\'' + $(this).html().trim() + '\'/>\ </div>');
title = "Update Tagline";
}
else if(id == '#logo')
{
var dynamicDialog = $('<div>\ <label>\ Logo :\ </label>\ <input type="hidden" value="logo" id="hidden">\ <input id="update" class="form-control" type="textbox" value=\'' + $(this).html().trim() + '\'/>\ </div>');
title = "Update Logo";
}
else if(id == '#footer')
{
var dynamicDialog = $('<div>\ <label>\ Logo :\ </label>\ <input type="hidden" value="footer" id="hidden">\ <input id="update" class="form-control" type="textbox" value=\'' + $(this).html().trim() + '\'/>\ </div>');
title = "Update Footer";
}
dynamicDialog.dialog(
{
title: title,
modal: true,
buttons: {
'Submit': function(e) {
console.log(e);
$.ajax({
type: "POST",
data: { name: $("#update").val() , hidden: $("#hidden").val() , element: "header"}
})
.done(function(msg) {
console.log($("#update").val());
if(id == '#tagline')
$(id).html(msg.name);
else if(id == '#logo')
$(id).html(msg.name);
else if(id == '#footer')
$(id).html(msg.name);
});
$(this).dialog('close');
},
'Cancel': function() {
$(this).dialog('close');
}
}
});
});
}
custom_update_box("#tagline");
custom_update_box("#logo");
custom_update_box("#footer");
});
</script>
HTML
<center>
<span id="logo" class="bs-docs-booticon bs-docs-booticon-lg bs-docs-booticon-outline">
<?php echo $logo_decode->wpbst_logo_initial; ?>
</span>
<td>
<p id="tagline" name="blogdescription" class="lead"><?php echo $result_tagline;?></p>
<input id="tagline" type="hidden" name='blogdescription' value=<?php echo '"'.$result_tagline.'"';?> />
</td>
<p class="lead">
<a id="button" class="btn btn-outline-inverse btn-lg" href=""><?php echo $button_decode->wpbst_button_name;?></a>
</p>
</center>