Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have one html page and i have second html page . i want to redirect myself from page one to page two but before i am redirected to page two i need to show somthing in html of page 2 . Can anyone tell me how to do that ?
In first page i have some form and when form is complited and presed button i am dont .ajax call and in the success function i want to do some changes before i redirect myself to second html. Problem is that i redirect myself to the second page but nothing is changed.... my element which i want to show is not showing ...

What I have tried:

JavaScript
$(document).ready(function(){
	
	$('#logovanje').submit(function(event){
		event.preventDefault();
		let korisnickoIme = $('#korisnickoIme').val();
		let lozinka = $('#lozinka').val();
		$.post({
			url: '../rest/logovanje',
			data: JSON.stringify({korisnickoIme: korisnickoIme, lozinka: lozinka}),
			contentType: 'application/json',
			success: function(data) {
				alert("Uspesno ulogovan korisnik")
                                $("#korisnik").hide();
				$("#korisnik1").show();
				window.location.replace( '../Pocetna.html' );
			},
			error: function(message) {
				alert("Neuspesno ulogovan korisnik")
			}
		});
	});
});

here:
JavaScript
$("#korisnik").hide();
$("#korisnik1").show();

korisnik and korisnik1 is id form second page of div element and i try to show it and hide thos div element... pls can anyone tell me where i am doing wrong here i do not know what to do...
Posted
Updated 3-Aug-18 1:46am
v2
Comments
Nathan Minier 2-Aug-18 13:50pm    
It there a reason that you need to perform a postback? Just update the contents of a div or other container, which makes the handling of data that needs to span requests simple.
Member 13810592 2-Aug-18 16:27pm    
Can you tell me how to do that ? i dont think i understand what you tell me coz i am new in this :D
Member 13810592 2-Aug-18 16:41pm    
if i learn it right now ,i do not need post back to the first page.I just need after user click on button i check all needed things if thats all is right i send him to second page but i need to show the second page whit changes of one div , one div is need to be show second to be hide... Actualy i have index page and on index page i have login when he click login then he is transfer to a second page whit login and then when he do it and click login on login.html i need to transfer him to the first page (index.html) but i do not need to show him again login , i need too show him div whit his user name , to give him information that he is login.... (i hope you understand me :D)

1 solution

Okay, what you're trying to do just won't work because you're calling changes to the current page immediately before changing the current page. What you're trying to do is a pretty common action in modern web applications, but the approach to these is completely different than the one you're taking.

Let me give you a very basic example of how a modern page might be structured with the jQuery you're using (if you do look at frameworks, Vue.js is actually pretty good and well-documented).

This doesn't do exactly what you're asking; it is just to present the ideas.

index.html:
HTML
<!DOCTYPE html>
<html>
   <head>
      <!-- SEO, metadata, etc here -->
      <script type="text/javascript" src=jQuery.js"></script>
      <script type="text/javascript" src="test.js"></script>
   </head>
   <body>
      <div id="header">
         <button id="loadButton">Clicky</button>
         <button id="showButton">Show</button>
         <button id="hideButton">Hide</button>
         <p>This is the standard header!</p>
      </div>
      <div id="content">
         <p>This will be immediately replaced</p>
      </div>
      <div id="footer">
         <p>This is the standard footer!</p>
      </div>
   </body>
</html>


loaded.html
HTML
<div>
   <p>This will be inserted into the #content div</p>
   <p id="dataPlaceholder">This will be changed by the script on the main page!</p>
</div>


Okay, we now have a template with some placeholder data. Next up is to insert the placeholder into the div and get your data into it:

test.js
JavaScript
$('showButton').click(function(e){
   e.preventDefault();
   $('#content').show();
}

$('hideButton').click(function(e){
   e.preventDefault();
   $('#content').hide();
}

$(document).ready(function(){
   $.get({
      url: 'loaded.html',
      contentType: 'text/html',
      success: function(result){
         $('#content').html(result);
      }
   });

   $('loadButton').click(function(e){
      e.preventdefault();
      $.post({
         url: '/data/url/of/some/kind',
         contentType: 'application/json',
         success: function(data){
            $('#dataPlaceholder').text(data);
         }
      });
   });
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900