Jump to content

Recommended Posts

I'm using the latest version of jQuery along with the ajaxForm plugin for jQuery. The ajax form plugin documentation can be found here... http://malsup.com/jquery/form/

 

I have the following form and javascript:

	var loginOptions = {
	type: 'POST',
    target: '#contentmain',
	url: 'lib/login.php',
	clearForm: true,
        success: function(html) { 
            $('#contentmain').html(html);
        } 
};

$('#login').submit(function() { 
	$("#loginForm").ajaxSubmit(loginOptions); 
  //  return false;

	$.ajax({
		method: "GET", url: 'rightpanel.php',
		success: function(html){$("#right").html(html);}
	});
	$.ajax({
		method: "GET", url: 'leftpanel.php',
		success: function(html){$("#left").html(html);}
	});
    
}); 

<form id="loginForm" action="lib/login.php" method="post">

<tr>
<td>Username:</td>
<td>
<input type="text" name="username" />
</td>
</tr>

<tr>
<td>Password:</td>
<td>
<input type="password" name="password" />
</td>
</tr>

<tr>
<td colspan="2" style="text-align: center;">
<input type="submit" value="Login" name="login" id="login" />
</td>
</tr>

</form>

 

The problem I have is... the form submits the data to lib/login.php which is fine. But.... the form acts like a standard link and so goes to the page lib/base.php, rather then just staying on index.php and loading base.php within the #contentmain div I have.

 

Any ideas/help/suggestions are welcome.

 

Regards, Ace

 

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/
Share on other sites

try adding a "return false" to your submit event.

 

$('#login').submit(function() {
  $("#loginForm").ajaxSubmit(loginOptions);


  $.ajax({
    method: "GET", url: 'rightpanel.php',
    success: function(html){$("#right").html(html);}
  });
  $.ajax({
    method: "GET", url: 'leftpanel.php',
    success: function(html){$("#left").html(html);}
  });
  // return false at the end of the submit event
  return false;
});

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-912258
Share on other sites

still goes to lib/login.php

 

the form is in lib/home.php , but each pages content in the lib directory is loaded within index.php with jQuery AJAX.

 

So... My URL looks like this... http://localhost/newcf/

But when the form is submitted it changes to http://localhost/newcf/lib/login.php

when it should stay the same and just load login.php within my div. Instead of going to the file.

 

So can I remove the action="lib/login.php" from the form somehow? I've removed it and then nothing happens with the form. But maybe with the ajaxForm plugin I can send my form data to lib/login.php without it just changing to that file completely?

 

sorry if that doesn't make much sense, trying my best to explain the problem :(

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-912418
Share on other sites

So can I remove the action="lib/login.php" from the form somehow? I've removed it and then nothing happens with the form. But maybe with the ajaxForm plugin I can send my form data to lib/login.php without it just changing to that file completely?

That would not be the solultion if you remove the action then the form would still be submitted. The page loaded has to stay in the browser and not be reloaded.

 

I notice a ajaxSubmit() function, is that a jQuery plugin and do you have a link to how it is supose to work? I think the solution might be in that function.

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-912437
Share on other sites

Ah I see what the problems are

 

1. your selector should be "#loginForm" instead of "#login"

2. the submit event should have a return false

3. not sure if this matters but i see $(this) used instead of re-using the id

 

Your submit event should be something like:

$('#loginForm').submit(function() {
  $(this).ajaxSubmit(loginOptions);

  $.ajax({
    method: "GET", url: 'rightpanel.php',
    success: function(html){$("#right").html(html);}
  });
  $.ajax({
    method: "GET", url: 'leftpanel.php',
    success: function(html){$("#left").html(html);}
  });
  return false;

}); 

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-913063
Share on other sites

still no good :S

 

the form's submitting fine. Still goes to the page. Rather then loading the pages(base.php) content in #contentmain.

 

the form is in lib/home.php with the actual page loaded in the web browser being index.php. lib/home.php is loaded in #contentmain.

so the URL at this point is... http://localhost/newcf/#

 

form is submitted to lib/login.php, That is successful so lib/login.php should be loaded into #contentmain. But instead of loading it within #contentmain in index.php, it just loads the file so the url is... http://localhost/newcf/lib/login.php

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-913369
Share on other sites

sorry for double posting...

 

Would it be easier to submit the form without the ajaxSubmit(); plugin? and if so, how would I modify my current script?

 

I've commented out my code, and tried to rewrite it without ajaxSubmit(); but it all seems to be doing is refreshing the page upon submit.

var username = $("input#username").val();  
var password = $("input#password").val();  
var dataString = 'username='+ username + '&password=' + password;  
$('#login').submit(function() {
		$.ajax({  
			type: "POST",  
			url: "lib/login.php",  
			data: dataString,  
			success: function(html) {  
				$('#contentmain').html(html);
			}	  
	});
});

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-913396
Share on other sites

Your code worked fine for me. Here is what I think that went wrong. I thought you only pasted the relevant code but perhaps it was all your code. Did you wrap the events in a document ready event? In order to see the elements in the document the complete DOM should be loaded. That or the javascript events should be underneath the corresponding elements.

 

Here is the complete code I used:

// wait till the dom is loaded
$(document).ready(function(){

  var loginOptions = {
    type: 'POST',
    target: '#contentmain',
    url: 'lib/login.php',
    clearForm: true,
    success: function(html) {
      $('#contentmain').html(html);
    }
  };

  $('#loginForm').submit(function() {
    $(this).ajaxSubmit(loginOptions);

    $.ajax({
      method: "GET", url: 'rightpanel.php',
      success: function(html){$("#right").html(html);}
    });
    $.ajax({
      method: "GET", url: 'leftpanel.php',
      success: function(html){$("#left").html(html);}
    });
    return false;

  }); 
});

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-913495
Share on other sites

here's my complete javascript file:

/*function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(err2) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(err3) {
	req = false;
}
  }
}
return req;
}

var http = getXMLHTTPRequest();
*/

function changeTitle(title) { document.title = title; }

$(document).ready(function(){
$('#loading').hide();

$.ajax({
	method: "GET", url: 'lib/home.php',
	success: function(html){$("#contentmain").html(html);}
});

$('a.liveload').livequery('click', function() {
	$("#contentmain").slideUp();

	var replacement = $(this).attr("title");
	var content_show = $(this).attr("id");

	$.ajax({
		type: "GET", url: "lib/"+replacement+".php", data: content_show,
		async: true,
		beforeSend: function(){$("#loading").show("fast");},
		complete: function(){$("#loading").hide("slow");},
		success: function(html){ //so, if data is retrieved, store it in html
			changeTitle("Conflicting Forces - "+replacement);
			$("#contentmain").slideDown("slow"); //animation
			$("#contentmain").html(html); //show the html inside .content div
	 	},
		error: function(xhr, ajaxOptions, thrownError){
			$("#contentmain").fadeIn("slow");
			$("#contentmain").html('<p style="color: red; background-color: black; border: 1px solid red; padding: 2px;">ERROR: Page does not exist!</p>');
            }    
	});

	$.ajax({
		method: "GET", url: 'rightpanel.php',
		success: function(html){$("#right").html(html);}
	});
	$.ajax({
		method: "GET", url: 'leftpanel.php',
		success: function(html){$("#left").html(html);}
	});

});


  var loginOptions = {
    type: 'POST',
    target: '#contentmain',
    url: 'lib/login.php',
    clearForm: true,
    success: function(html) {
      $('#contentmain').html(html);
    }
  };

  $('#loginForm').submit(function() {
    $(this).ajaxSubmit(loginOptions);

    $.ajax({
      method: "GET", url: 'rightpanel.php',
      success: function(html){$("#right").html(html);}
    });
    $.ajax({
      method: "GET", url: 'leftpanel.php',
      success: function(html){$("#left").html(html);}
    });
    return false;

  }); 

/*
var username = $("input#username").val();  
var password = $("input#password").val();  
var dataString = 'username='+ username + '&password=' + password;  
$('#login').submit(function() {
		$.ajax({  
			type: "POST",  
			url: "lib/login.php",  
			data: dataString,
			dataType: "html",
			success: function(html) {  
				$('#contentmain').html(html);
			}	  
		});
	return false;
});
*/

});

 

all of it works besides the form.

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-913513
Share on other sites

I've actually changed my Form stuff around again... and omg, it works. kinda...

	$('#login').livequery('click', function() {
	$("#contentmain").slideUp();

	var username = $("input#username").val();  
	var password = $("input#password").val();  
	var dataString = 'username='+ username + '&password=' + password;

	$.ajax({
		type: "GET", url: "lib/login.php", data: dataString,
		async: true,
		beforeSend: function(){$("#loading").show("fast");},
		complete: function(){$("#loading").hide("slow");},
		success: function(html){ //so, if data is retrieved, store it in html
			changeTitle("Conflicting Forces - base");
			$("#contentmain").slideDown("slow"); //animation
			$("#contentmain").html(html); //show the html inside .content div
	 	},
		error: function(xhr, ajaxOptions, thrownError){
			$("#contentmain").fadeIn("slow");
			$("#contentmain").html('<p style="color: red; background-color: black; border: 1px solid red; padding: 2px;">ERROR: Page does not exist!</p>');
            }    
	});

	$.ajax({
		method: "GET", url: 'rightpanel.php',
		success: function(html){$("#right").html(html);}
	});
	$.ajax({
		method: "GET", url: 'leftpanel.php',
		success: function(html){$("#left").html(html);}
	});

});	

 

It works how it should. But... it shows the login.php content for less then a second. Then #contentmain goes blank.

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-913517
Share on other sites

You can change the submit but to a Link using Text or Images and it should be able to bring up the same effect WITHOUT submitting the actual event because if you use the .click event with live query you are causing the page to pull up the info via .ajax and after that you are getting the result of the submit which was submitted after the click... So Try changing the button into an image that have say <a href="#" id="submit_me"><img src="image.jpg></a> and have it use the click event to pull up the info you need... ;) I have used that before for an attack script... :P

Link to comment
https://forums.phpfreaks.com/topic/173069-load-basephp-content/#findComment-916413
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.