javascript - 404 error because of ajax code? -
i have form action=""
, many ajax codes validate or chain-select form elements below.
** current code (simplified) **
<?php if( isset( $_post['create_transaction'] ) ) { // } ?> <div> <!-- period information filled ajax --> <input type="text" name="period" class="form-control" id="period" readonly /> </div> <form id="form" method="post" action=""> <input name="r_date" type="date" value="<?php echo isset( $_post['r_date'] ) ? $_post['r_date'] : date( "y-m-d" ); ?>" class="transaction_date" /> <input type="submit" name="create_transaction" value="create" /> </form> <script> jquery(document).ready(function($) { // , that... /* period information if date filled on page load */ var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; var $transaction_date = $(".transaction_date").val(); if( $transaction_date ) { // call ajax period <-- problem child. jquery.ajax({ url: ajaxurl, data: 'action=get_period_by_date&transaction_date='+$transaction_date, type: 'get', success:function(results) { jquery("#period").val(results); } }); } }); </script>
here retrieving period name using input date. example, if date 2014-01-01, #period
div filled period-01. if date 2014-08-25, #period
div filled period-08. date field auto-filled today's date when page loads.
the problem if insert ajax code retrieve period information using date input, submitting form (clicking 'create' button) leads 404 error. if remove code, form works well.
there doesn't seem problem in get_period_by_date
function since correctly retrieves , shows period name.
what cause of problem?
404 error means ajax request leads unexisting path. thats first issue should solve :).
use console debug one:
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
you using var in jquery method. think literally interpreted. don't think it's possible use php syntax here.
i'm not sure if it's practice, quick fix make hidden field in html page tag. , value use of jquery or javascript
Comments
Post a Comment