Visual P Visual P - 1 year ago 135
jQuery Question

Ajax Navigation and form submissions

Currently my website dynamically loads content using an ajax script.
Here is the script

* Dynamic Ajax Content- (c) Dynamic Drive DHTML code library (
* Please keep this notice intact
* Visit Dynamic Drive at for full source code
var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
catch (e){
page_request = new ActiveXObject("Microsoft.XMLHTTP")
catch (e){}
return false
loadpage(page_request, containerid)
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()'GET', url+bustcacheparameter, true)

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))

function loadobjs(){
if (!document.getElementById)
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref.setAttribute("src", file);
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
if (fileref!=""){
loadedobjects+=file+" " //Remember this object as being already added to page

<p> Hello World! </p>

<a href="javascript:ajaxpage('example.html', 'content');">Form</a>
<div id="content"></div>

This part work's fine my content is loaded dynamically the problem arises when I attempt to submit a php form inside of the loaded content the whole page simply refreshes I've tried using javascript to stop the page from refreshing but still no luck. I used this form as a test it functions properly outside of the ajax content but when placed inside the page simply just refreshes! If anyone could give me some insight on how to solve this problem It'd be greatly appreciated

Answer Source

I think it's because your form is dynamically generated, therefore the event handler doesn't capture your submit event. It happened to me a lot of times and what I did was something like this. I had my selector to the static content. $("#staticContainer").on("submit","#formId",function(){ });

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download