I have been trying to get my html form to launch automatically when the page is loaded rather than when a button is clicked however have been unable to get the $.ajax
,$.get
and $.post
method to perform the submission identically to the submit button.
My first method was to used a regular button :
<input style="width:80%;margin-left:10%;display:none;" class="span2" name="latest" id="latest" type="text" value="">
<input style="" type="submit" value="Go" name="/latest">
This worked exactly as I expected, however i want the form to submit automatically when the page was loaded so :
<div id="cont2">
<script type="text/javascript">
var a=$('#target').serialize();
$.ajax({
type:'post',
url:'/latest',
data:a,
beforeSend:function(){
alert("click");
},
complete:function(){
alert("complete");
},
success:function(result){
alert(result);
$( '#cont2' ).html();
}
});
</script>
</div>
This did not perform as expected, instead of returning the value from the /latest route it instead just returned the same page.
This method did not work either :
<script type="text/javascript">
$.get( "/latest", function( data ) {
$( "#cont2" ).html( data );
alert( "Load was performed." );
});
</script>
As the route is a POST route it is expecting a post submission however this did not work either:
<script type="text/javascript">
$('form[name=target]').submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(json) {
alert(json);
}, 'json');
return false;
});
</script>
The route that is reached once the button is clicked that actually works is :
$f3->route('POST /', function($f3, $params) {
if(isset($_POST['/latest']))
{
echo "accept";
}
}
In short, when the button is clicked it all performs as expected. But when using jquery to submit the form (so i can be done automatically) I cannot get the form to return the same response. Any help solving this would be appreciated.