fanboime Posted April 24, 2014 Share Posted April 24, 2014 (edited) Here's my script $( "#button" ).button(); $( "#button" ).click(function( event ) { <?PHP ... ?> }); And here's the input tag <input id="button" type="submit" value="Add" name = "subBtn" /> Whenever i click the button it triggers postback how can i avoid this? Edited April 24, 2014 by fanboime Quote Link to comment https://forums.phpfreaks.com/topic/287991-how-to-avoid-postback-on-jquery-button-click-in-php/ Share on other sites More sharing options...
gristoi Posted April 24, 2014 Share Posted April 24, 2014 $( "#button" ).click(function( event ) { event.preventDefault();}); Quote Link to comment https://forums.phpfreaks.com/topic/287991-how-to-avoid-postback-on-jquery-button-click-in-php/#findComment-1477169 Share on other sites More sharing options...
fanboime Posted April 25, 2014 Author Share Posted April 25, 2014 $( "#button" ).click(function( event ) { event.preventDefault(); }); I've tried this and whenever i click my button nothing is happening... Quote Link to comment https://forums.phpfreaks.com/topic/287991-how-to-avoid-postback-on-jquery-button-click-in-php/#findComment-1477226 Share on other sites More sharing options...
Solution DavidAM Posted April 25, 2014 Solution Share Posted April 25, 2014 $( "#button" ).click(function( event ) { <?PHP ... ?> PHP is executed on the SERVER before the page gets to the user. It is not possible to execute PHP code directly from JavaScript. You would have to use AJAX to send a request to the server to execute some PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/287991-how-to-avoid-postback-on-jquery-button-click-in-php/#findComment-1477233 Share on other sites More sharing options...
fanboime Posted April 25, 2014 Author Share Posted April 25, 2014 PHP is executed on the SERVER before the page gets to the user. It is not possible to execute PHP code directly from JavaScript. You would have to use AJAX to send a request to the server to execute some PHP code. Can you please give a tutorial? Quote Link to comment https://forums.phpfreaks.com/topic/287991-how-to-avoid-postback-on-jquery-button-click-in-php/#findComment-1477238 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.