Ajax not filling input fields
-
Hello I’m new to wrdpress and php.
I wan to make a simple admin page for managing bands.the page looks like
<div class=”wrap”>
<form name=”newbandform” action=”” method=”post” id=”newbandform” role=”form”>
<input type=”text” name=”bandname” id=”bandname” placeholder=”Vul de bandnaam in”>
<input type=”submit” value=”Bewaar”>
</form>
</div>i have a php file inject-js.php:
function insidejazz_action_javascript(){
?>
<script type=”text/javascript”>
jQuery(document).ready(function ($)
{
var band = “”;
$(‘#newbandform’).submit(function ()
{
var data = ({ ‘action’: ‘add_or_update_band’,
‘bandname’: $(‘#bandname’).val()
});
$.post(ajaxurl, data, function (response)
{
band = JSON.parse(response);
loadband(band);
});
});function loadband(aband)
{
$(‘#bandname’).val(aband.bandname);
}</script>
<?php
}
?>and a bandrepo.php file :
function add_or_update_band_callback(){
// Get data
$bandname = $_POST[‘bandname’];// Send data back
$band= array(‘bandname’ => $bandname);
echo json_encode($band);
die();
}in the main plugin function file i do:
include(‘inc/inject-js.php’);
include(‘inc/bandrepo.php’);add_action(‘admin_footer’, ‘insidejazz_action_javascript’ );
add_action(‘wp_ajax_add_or_update_band’, ‘add_or_update_band_callback’ );
add_action(‘wp_ajax_nopriv_add_or_update_band’,’add_or_update_band_callback’);Know I can post the form correct but the #bandname is empty after that.
I know i have the bandname back because alert(aband.bandname) works in
the loadband function.The question is know:
how can i set value of #bandname so it isn’t empty anymore?The rest of the form en saving the band will folow if this works. 🙂
The topic ‘Ajax not filling input fields’ is closed to new replies.